Compare commits
10 Commits
92ee82fc0e
...
b31c00ed70
Author | SHA1 | Date | |
---|---|---|---|
b31c00ed70 | |||
d49ca4fa68 | |||
d113f02d90 | |||
f36146731b | |||
416eb415a9 | |||
2fba1066f8 | |||
9f64aa5a5f | |||
b3525967af | |||
34177c2e4d | |||
74587d8c32 |
@ -1,4 +1,5 @@
|
||||
{
|
||||
"project_name": "optic-fiber",
|
||||
"module_path": "git.cesium.pw/niku/optic-fiber"
|
||||
"project_name": "fiber-optic",
|
||||
"module_path": "git.cesium.pw/niku/fiber-optic",
|
||||
"image_name": "{{cookiecutter.project_name}}"
|
||||
}
|
||||
|
@ -1,12 +1,25 @@
|
||||
import subprocess
|
||||
from subprocess import run
|
||||
|
||||
GO_PACKAGES = [
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2",
|
||||
"github.com/gofiber/swagger",
|
||||
]
|
||||
|
||||
GO_INSTALL = [
|
||||
"github.com/swaggo/swag/cmd/swag@latest"
|
||||
]
|
||||
|
||||
def main() -> int:
|
||||
run(["git", "init"])
|
||||
|
||||
for package in GO_PACKAGES:
|
||||
subprocess.run(["go", "get", "-u", package])
|
||||
run(["go", "get", "-u", package])
|
||||
|
||||
for package in GO_INSTALL:
|
||||
run(["go", "install", package])
|
||||
|
||||
run(["swag", "init", "-d", "internal/app"])
|
||||
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
1
{{cookiecutter.project_name}}/.dockerignore
Normal file
1
{{cookiecutter.project_name}}/.dockerignore
Normal file
@ -0,0 +1 @@
|
||||
*_test.go
|
27
{{cookiecutter.project_name}}/.gitignore
vendored
Normal file
27
{{cookiecutter.project_name}}/.gitignore
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
# If you prefer the allow list template instead of the deny list, see community template:
|
||||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
||||
#
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
|
||||
# Go workspace file
|
||||
go.work
|
||||
|
||||
# Air
|
||||
tmp
|
||||
|
||||
# secrets
|
||||
.env
|
31
{{cookiecutter.project_name}}/Dockerfile
Normal file
31
{{cookiecutter.project_name}}/Dockerfile
Normal file
@ -0,0 +1,31 @@
|
||||
# Building the binary of the App
|
||||
FROM docker.io/golang:1.20 AS build
|
||||
|
||||
WORKDIR /go/src/{{cookiecutter.project_name}}
|
||||
|
||||
# Copy all the Code and stuff to compile everything
|
||||
COPY . .
|
||||
|
||||
# Downloads all the dependencies in advance (could be left out, but it's more clear this way)
|
||||
RUN go mod download
|
||||
|
||||
# Builds the application as a staticly linked one, to allow it to run on alpine
|
||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o app cmd/{{cookiecutter.project_name}}/main.go
|
||||
|
||||
|
||||
# Moving the binary to the 'final Image' to make it smaller
|
||||
FROM docker.io/alpine:latest AS release
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY --from=build /go/src/{{cookiecutter.project_name}}/app .
|
||||
|
||||
# Add packages
|
||||
RUN apk -U upgrade \
|
||||
&& apk add --no-cache dumb-init ca-certificates \
|
||||
&& chmod +x /app/app
|
||||
|
||||
# Exposes port 3000 because our program listens on that port
|
||||
EXPOSE 3000
|
||||
|
||||
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
|
@ -1,17 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := fiber.New()
|
||||
|
||||
app.Get("/", func (c *fiber.Ctx) error {
|
||||
return c.SendString("Hello, World!")
|
||||
})
|
||||
|
||||
log.Fatal(app.Listen(":3000"))
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"{{cookiecutter.module_path}}/internal/app"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := app.Run(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
20
{{cookiecutter.project_name}}/internal/app/api/router.go
Normal file
20
{{cookiecutter.project_name}}/internal/app/api/router.go
Normal file
@ -0,0 +1,20 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/helmet"
|
||||
v1 "{{cookiecutter.module_path}}/internal/app/api/v1"
|
||||
)
|
||||
|
||||
func ConfigureRoutes(app *fiber.App) error {
|
||||
api := app.Group("/api")
|
||||
|
||||
api.Use(helmet.New())
|
||||
|
||||
err := v1.ConfigureRoutes(api)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
25
{{cookiecutter.project_name}}/internal/app/api/v1/router.go
Normal file
25
{{cookiecutter.project_name}}/internal/app/api/v1/router.go
Normal file
@ -0,0 +1,25 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/requestid"
|
||||
)
|
||||
|
||||
// @Summary Endpoint for testing latency
|
||||
// @Description Endpoint for testing latency
|
||||
// @Tags Health
|
||||
// @Success 200 {string} string
|
||||
// @Router /api/v1/ping [get]
|
||||
func Ping(c *fiber.Ctx) error {
|
||||
return c.SendString("pong")
|
||||
}
|
||||
|
||||
func ConfigureRoutes(router fiber.Router) error {
|
||||
v1 := router.Group("/v1")
|
||||
|
||||
v1.Use(requestid.New())
|
||||
|
||||
v1.Get("/ping", Ping)
|
||||
|
||||
return nil
|
||||
}
|
36
{{cookiecutter.project_name}}/internal/app/main.go
Normal file
36
{{cookiecutter.project_name}}/internal/app/main.go
Normal file
@ -0,0 +1,36 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/compress"
|
||||
"github.com/gofiber/fiber/v2/middleware/helmet"
|
||||
"github.com/gofiber/fiber/v2/middleware/logger"
|
||||
"github.com/gofiber/swagger"
|
||||
_ "{{cookiecutter.module_path}}/docs"
|
||||
"{{cookiecutter.module_path}}/internal/app/api"
|
||||
)
|
||||
|
||||
// @title {{cookiecutter.project_name}} API
|
||||
// @version 1.0
|
||||
// @description Documentation for the {{cookiecutter.project_name}} API.
|
||||
|
||||
// @securityDefinitions.apikey ApiKeyAuth
|
||||
// @in header
|
||||
// @name Authorization
|
||||
// @description Token used to authenticate against the API.
|
||||
func Run() error {
|
||||
app := fiber.New()
|
||||
|
||||
app.Use(logger.New())
|
||||
app.Use(compress.New())
|
||||
app.Use(helmet.New())
|
||||
|
||||
// Prefork is only enabled in prod-mode.
|
||||
if !app.Config().Prefork {
|
||||
app.Get("/swagger/*", swagger.HandlerDefault)
|
||||
}
|
||||
|
||||
api.ConfigureRoutes(app)
|
||||
|
||||
return app.Listen(":3000")
|
||||
}
|
40
{{cookiecutter.project_name}}/justfile
Normal file
40
{{cookiecutter.project_name}}/justfile
Normal file
@ -0,0 +1,40 @@
|
||||
project_name := "{{cookiecutter.project_name}}"
|
||||
image_name := "{{cookiecutter.image_name}}"
|
||||
|
||||
run: ## Run the app
|
||||
go run cmd/{{cookiecutter.project_name}}/main.go
|
||||
|
||||
{% raw -%}
|
||||
tidy: ## Generate go.mod & go.sum files
|
||||
go mod tidy
|
||||
|
||||
clean: ## Clean packages
|
||||
go clean -modcache
|
||||
|
||||
build: ## Generate docker image
|
||||
podman build -t {{image_name}} .
|
||||
|
||||
build-no-cache: ## Generate docker image with no cache
|
||||
podman build --no-cache -t {{image_name}} .
|
||||
|
||||
up: ## Run local container in background
|
||||
just delete-container-if-exist
|
||||
podman run -d -p 3000:3000 --name {{project_name}} {{image_name}} ./app
|
||||
|
||||
up-prefork: ## Run local container in background with prefork
|
||||
just delete-container-if-exist
|
||||
podman run -d -p 3000:3000 --name {{project_name}} {{image_name}} ./app -prod
|
||||
|
||||
delete-container-if-exist: ## Delete container if it exists
|
||||
podman stop {{project_name}} || true && podman rm {{project_name}} || true
|
||||
|
||||
stop: ## Stop the container
|
||||
podman stop {{project_name}}
|
||||
|
||||
start: ## Start the container
|
||||
podman start {{project_name}}
|
||||
|
||||
swagger: ## Generate swagger documentation
|
||||
swag init -d internal/app
|
||||
|
||||
{%- endraw %}
|
Loading…
x
Reference in New Issue
Block a user