Compare commits

...

10 Commits

Author SHA1 Message Date
b31c00ed70 Added swagger support 2023-07-22 15:50:06 +02:00
d49ca4fa68 Added request ID middleware to api/v1 2023-07-22 14:31:55 +02:00
d113f02d90 Fix wrong go types 2023-07-22 14:28:49 +02:00
f36146731b After template creation init git repo 2023-07-22 14:28:13 +02:00
416eb415a9 Added directory structure for routing 2023-07-22 14:19:12 +02:00
2fba1066f8 Fixed typo in cookiecutter.json 2023-07-22 14:16:45 +02:00
9f64aa5a5f Added .gitignore to template 2023-07-22 13:51:46 +02:00
b3525967af Updated go version 2023-07-22 13:50:50 +02:00
34177c2e4d Added justfile 2023-07-22 13:50:41 +02:00
74587d8c32 Added Dockerfile & .dockerignore 2023-07-22 13:26:33 +02:00
11 changed files with 212 additions and 22 deletions

View File

@ -1,4 +1,5 @@
{ {
"project_name": "optic-fiber", "project_name": "fiber-optic",
"module_path": "git.cesium.pw/niku/optic-fiber" "module_path": "git.cesium.pw/niku/fiber-optic",
"image_name": "{{cookiecutter.project_name}}"
} }

View File

@ -1,12 +1,25 @@
import subprocess from subprocess import run
GO_PACKAGES = [ 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: def main() -> int:
run(["git", "init"])
for package in GO_PACKAGES: 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 return 0
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -0,0 +1 @@
*_test.go

View 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

View 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", "--"]

View File

@ -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"))
}

View File

@ -0,0 +1,13 @@
package main
import (
"log"
"{{cookiecutter.module_path}}/internal/app"
)
func main() {
if err := app.Run(); err != nil {
log.Fatal(err)
}
}

View 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
}

View 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
}

View 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")
}

View 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 %}