From 4a7b2d84c870a276d7b7a148e0da503506a7d7d8 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Wed, 5 Jul 2023 11:06:49 +0200 Subject: [PATCH 1/5] Add go cache --- .gitlab/_common.gitlab-ci.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.gitlab/_common.gitlab-ci.yml b/.gitlab/_common.gitlab-ci.yml index fb6d21b..2cd2c34 100644 --- a/.gitlab/_common.gitlab-ci.yml +++ b/.gitlab/_common.gitlab-ci.yml @@ -12,3 +12,13 @@ image: docker:$DOCKER_VERSION-git .login_registry: before_script: - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY + +.go-cache: + variables: + GOPATH: $CI_PROJECT_DIR/.go + before_script: + - mkdir -p .go + - export PATH=$PATH:$GOROOT/bin:$GOPATH/bin + cache: + paths: + - .go/pkg/mod/ From be890e2e75ba2508de1d04d94f194e4469d88fdf Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Wed, 5 Jul 2023 11:09:24 +0200 Subject: [PATCH 2/5] Add swagger-types for unit-tests --- .gitlab/test.gitlab-ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitlab/test.gitlab-ci.yml b/.gitlab/test.gitlab-ci.yml index 78333f8..a2eb497 100644 --- a/.gitlab/test.gitlab-ci.yml +++ b/.gitlab/test.gitlab-ci.yml @@ -5,6 +5,8 @@ unit_tests: extends: - .go-cache script: + - ./swagger.sh install + - ./swagger.sh init - go install gotest.tools/gotestsum@latest - gotestsum --junitfile report.xml --format testname -- ./... -coverprofile=profile.cov - go tool cover -func profile.cov From 1629523d8453556a5fbda427a3033e2641beb22e Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Wed, 5 Jul 2023 11:41:13 +0200 Subject: [PATCH 3/5] Fix html rendering --- .gitlab/test.gitlab-ci.yml | 4 ++-- docker-compose.yml | 1 + docker/Dockerfile | 2 +- internal/env/env.go | 2 +- internal/router/middleware.go | 7 +++++++ internal/router/router.go | 22 ++++++++++++++++++++++ internal/router/templates.go | 22 ++++++++++++++++++++++ 7 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 internal/router/templates.go diff --git a/.gitlab/test.gitlab-ci.yml b/.gitlab/test.gitlab-ci.yml index a2eb497..41c6216 100644 --- a/.gitlab/test.gitlab-ci.yml +++ b/.gitlab/test.gitlab-ci.yml @@ -5,8 +5,8 @@ unit_tests: extends: - .go-cache script: - - ./swagger.sh install - - ./swagger.sh init + - ./scripts/swagger.sh install + - ./scripts/swagger.sh init - go install gotest.tools/gotestsum@latest - gotestsum --junitfile report.xml --format testname -- ./... -coverprofile=profile.cov - go tool cover -func profile.cov diff --git a/docker-compose.yml b/docker-compose.yml index e796d39..962ba90 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -115,6 +115,7 @@ services: environment: - PUID=1000 - PGID=1000 + - SWAGGER_HOST=https://cafe.test labels: - 'traefik.enable=true' - 'traefik.http.routers.backend.rule=Host(`cafe.test`) && PathPrefix(`/api`)' diff --git a/docker/Dockerfile b/docker/Dockerfile index ef281f2..74adbbc 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -39,7 +39,7 @@ WORKDIR /app COPY ./scripts/entrypoint.sh . COPY --from=logo /logo.txt . -COPY --from=nodeBuilder /app/dist/ ./templates/ +COPY --from=nodeBuilder /app/dist/ ./web/ COPY --from=goBuilder /app/cafe-plaetschwiesle . ARG VERSION diff --git a/internal/env/env.go b/internal/env/env.go index 07e8f06..bc9f229 100644 --- a/internal/env/env.go +++ b/internal/env/env.go @@ -15,7 +15,7 @@ type Config struct { Port int `env:"PORT" envDefault:"8080" validate:"min=1024,max=49151"` LogLevel string `env:"LOG_LEVEL" envDefault:"info" validate:"oneof=debug info warn error panic fatal"` Version string `env:"VERSION" envDefault:"v0.0.0"` - SwaggerHost string `env:"SWAGGER_HOST" envDefault:"https://cafe.test"` + SwaggerHost string `env:"SWAGGER_HOST"` DB_Host string `env:"DB_HOST"` DB_User string `env:"DB_USER"` DB_Password string `env:"DB_PASSWORD"` diff --git a/internal/router/middleware.go b/internal/router/middleware.go index 475bc03..2831f0f 100644 --- a/internal/router/middleware.go +++ b/internal/router/middleware.go @@ -4,6 +4,13 @@ import ( "github.com/labstack/echo/v4" ) +func longCacheLifetime(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) error { + c.Response().Header().Set(echo.HeaderCacheControl, "public, max-age=31536000") + return next(c) + } +} + func authHeader(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { c.Response().Header().Set("Remote-Groups", c.Request().Header.Get("Remote-Groups")) diff --git a/internal/router/router.go b/internal/router/router.go index ce5bebe..c96579f 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -29,11 +29,26 @@ func InitRouter() *echo.Echo { e.Pre(middleware.RemoveTrailingSlash()) e.Validator = &CustomValidator{Validator: newValidator()} + e.Renderer = initTemplates() return e } func SetupRoutes(e *echo.Echo, ctrl *controller.Controller, env *env.Config) { + favicon := e.Group("/favicon", longCacheLifetime) + favicon.Static("/", "web/favicon") + fonts := e.Group("/fonts", longCacheLifetime) + fonts.Static("/", "web/fonts") + img := e.Group("/img", longCacheLifetime) + img.Static("/", "web/img") + + e.Static("/css", "web/css") + e.Static("/js", "web/js") + + e.GET("/", func(c echo.Context) error { + return c.Render(http.StatusOK, "index.html", nil) + }) + api := e.Group("/api") { tableGroup := api.Group("/tables") @@ -79,6 +94,13 @@ func SetupRoutes(e *echo.Echo, ctrl *controller.Controller, env *env.Config) { }) } + e.GET("/robots.txt", func(ctx echo.Context) error { + return ctx.String(http.StatusOK, "User-agent: *\nDisallow: /") + }) + e.RouteNotFound("*", func(ctx echo.Context) error { + return ctx.Redirect(http.StatusTemporaryRedirect, "/") + }) + if env.SwaggerHost != "" { docs.SwaggerInfo.Title = "Café Plätschwiesle" docs.SwaggerInfo.Description = "This is the backend of Café Plätschwiesle" diff --git a/internal/router/templates.go b/internal/router/templates.go new file mode 100644 index 0000000..b99a22e --- /dev/null +++ b/internal/router/templates.go @@ -0,0 +1,22 @@ +package router + +import ( + "html/template" + "io" + + "github.com/labstack/echo/v4" +) + +type Template struct { + templates *template.Template +} + +func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error { + return t.templates.ExecuteTemplate(w, name, data) +} + +func initTemplates() *Template { + return &Template{ + templates: template.Must(template.ParseGlob("web/*.html")), + } +} From 34ca6f0f7e6dcdecfdd11fe237bdc5d078e6d981 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Wed, 5 Jul 2023 13:45:38 +0200 Subject: [PATCH 4/5] Fix version env --- .gitlab/build.gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab/build.gitlab-ci.yml b/.gitlab/build.gitlab-ci.yml index d34188e..35d56e0 100644 --- a/.gitlab/build.gitlab-ci.yml +++ b/.gitlab/build.gitlab-ci.yml @@ -15,7 +15,7 @@ build_release: --build-arg GOLANG_VERSION=$GOLANG_VERSION --build-arg NODE_VERSION=$NODE_VERSION --build-arg ALPINE_VERSION=$ALPINE_VERSION - --build-arg APP_VERSION=$CI_COMMIT_TAG + --build-arg VERSION=$CI_COMMIT_TAG --build-arg BUILD_TIME=$CI_JOB_STARTED_AT --tag $CURRENT_IMAGE --tag $LATEST_IMAGE From 7a3780f3e1c448570d119062b1d05ccc1cff5e02 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Wed, 5 Jul 2023 13:50:53 +0200 Subject: [PATCH 5/5] Fix redirecting --- internal/router/router.go | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/internal/router/router.go b/internal/router/router.go index c96579f..2ca4409 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -45,10 +45,6 @@ func SetupRoutes(e *echo.Echo, ctrl *controller.Controller, env *env.Config) { e.Static("/css", "web/css") e.Static("/js", "web/js") - e.GET("/", func(c echo.Context) error { - return c.Render(http.StatusOK, "index.html", nil) - }) - api := e.Group("/api") { tableGroup := api.Group("/tables") @@ -94,13 +90,6 @@ func SetupRoutes(e *echo.Echo, ctrl *controller.Controller, env *env.Config) { }) } - e.GET("/robots.txt", func(ctx echo.Context) error { - return ctx.String(http.StatusOK, "User-agent: *\nDisallow: /") - }) - e.RouteNotFound("*", func(ctx echo.Context) error { - return ctx.Redirect(http.StatusTemporaryRedirect, "/") - }) - if env.SwaggerHost != "" { docs.SwaggerInfo.Title = "Café Plätschwiesle" docs.SwaggerInfo.Description = "This is the backend of Café Plätschwiesle" @@ -112,5 +101,12 @@ func SetupRoutes(e *echo.Echo, ctrl *controller.Controller, env *env.Config) { api.GET("/swagger/*", echoSwagger.WrapHandler) zap.L().Info("swagger running", zap.String("url", env.SwaggerHost+"/api/swagger/index.html")) } + + e.GET("/robots.txt", func(ctx echo.Context) error { + return ctx.String(http.StatusOK, "User-agent: *\nDisallow: /") + }) + e.RouteNotFound("*", func(c echo.Context) error { + return c.Render(http.StatusOK, "index.html", nil) + }) } }