From 671836280f1af1c53470f87a3e763133efb48b57 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Tue, 19 Mar 2024 11:36:09 +0100 Subject: [PATCH] Make auth optional --- compose.yml | 48 +++++++++++++++++++++++------------- handlers/auth.handlers.go | 6 ++--- handlers/routes.go | 13 +++++++--- internal/env/env.go | 10 ++++---- main.go | 8 +++--- services/weather.services.go | 5 ++-- 6 files changed, 56 insertions(+), 34 deletions(-) diff --git a/compose.yml b/compose.yml index ae95b3a..1204e23 100644 --- a/compose.yml +++ b/compose.yml @@ -1,21 +1,15 @@ -services: - godash: - profiles: [build] - image: ${CURRENT_IMAGE} - build: - context: . - dockerfile: Dockerfile - args: - - V_GOLANG=${V_GOLANG} - - V_NODE=${V_NODE} - - V_ALPINE=${V_ALPINE} - - APP_VERSION=${CI_COMMIT_TAG} +networks: + net: + external: false + proxy: + external: false +services: logto: depends_on: logto-db: condition: service_healthy - image: svhd/logto:latest + image: svhd/logto:${V_LOGTO} entrypoint: ['sh', '-c', 'npm run cli db seed -- --swe && npm start'] ports: - 3001:3001 @@ -23,9 +17,13 @@ services: environment: - TRUST_PROXY_HEADER=1 - DB_URL=postgres://postgres:p0stgr3s@logto-db:5432/logto + - ENDPOINT=http://0.0.0.0:3001 + networks: + - net + - proxy logto-db: - image: postgres:14-alpine + image: postgres:${V_POSTGRES}-alpine user: postgres environment: POSTGRES_USER: postgres @@ -35,6 +33,8 @@ services: interval: 10s timeout: 5s retries: 5 + networks: + - net backend: build: @@ -58,12 +58,11 @@ services: command: air -c .air.toml environment: - TZ=Europe/Berlin - - PUBLIC_URL=http://godash:4000 + - PUBLIC_URL=http://localhost:4000 - LOG_LEVEL=debug - TITLE=DEV - APP_VERSION=v0.0.1-DEV - WEATHER_KEY=${WEATHER_KEY} - - LOGTO_ENDPOINT=${LOGTO_ENDPOINT} - LOGTO_APP_ID=${LOGTO_APP_ID} - LOGTO_APP_SECRET=${LOGTO_APP_SECRET} - SESSION_KEY=super-secure @@ -71,9 +70,11 @@ services: - .:/app/ ports: - 4000:4000 - - 2345:2345 + networks: + - proxy templ: + profiles: [dev] build: context: . dockerfile_inline: | @@ -89,6 +90,7 @@ services: - .:/app/ tailwind: + profiles: [dev] image: node:${V_NODE}-alpine working_dir: /app command: yarn tw:dev @@ -96,6 +98,18 @@ services: volumes: - .:/app/ + godash: + profiles: [build] + image: ${CURRENT_IMAGE} + build: + context: . + dockerfile: Dockerfile + args: + - V_GOLANG=${V_GOLANG} + - V_NODE=${V_NODE} + - V_ALPINE=${V_ALPINE} + - APP_VERSION=${CI_COMMIT_TAG} + golang: profiles: [install] image: golang:${V_GOLANG}-alpine diff --git a/handlers/auth.handlers.go b/handlers/auth.handlers.go index eb5e442..62608cc 100644 --- a/handlers/auth.handlers.go +++ b/handlers/auth.handlers.go @@ -45,7 +45,7 @@ func (authHandler *AuthHandler) signInHandler(c echo.Context) error { ) signInUri, err := logtoClient.SignIn(authHandler.env.PublicUrl + "/sign-in-callback") if err != nil { - slog.Error(err.Error()) + slog.Error("cannot process sign in request", "err", err) return echo.ErrInternalServerError } return c.Redirect(http.StatusTemporaryRedirect, signInUri) @@ -58,7 +58,7 @@ func (authHandler *AuthHandler) signInCallbackHandler(c echo.Context) error { ) err := logtoClient.HandleSignInCallback(c.Request()) if err != nil { - slog.Error(err.Error()) + slog.Error("cannot process sign in callback", "err", err) return echo.ErrInternalServerError } return c.Redirect(http.StatusTemporaryRedirect, "/") @@ -71,7 +71,7 @@ func (authHandler *AuthHandler) signOutCallbackHandler(c echo.Context) error { ) signOutUri, err := logtoClient.SignOut(authHandler.env.PublicUrl) if err != nil { - slog.Error(err.Error()) + slog.Error("cannot process sign out", "err", err) return echo.ErrInternalServerError } return c.Redirect(http.StatusTemporaryRedirect, signOutUri) diff --git a/handlers/routes.go b/handlers/routes.go index ed8b6ef..cea6811 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -9,11 +9,16 @@ import ( ) func SetupRoutes(e *echo.Echo, sse *sse.Server, appHandler *AppHandler, authHandler *AuthHandler) { - e.GET("/sign-in", authHandler.signInHandler) - e.GET("/sign-in-callback", authHandler.signInCallbackHandler) + if authHandler.env.LogtoEndpoint != "" { + e.GET("/sign-in", authHandler.signInHandler) + e.GET("/sign-in-callback", authHandler.signInCallbackHandler) + e.GET("/sign-out", authHandler.signOutCallbackHandler) + } - secure := e.Group("/", authHandler.logtoMiddleware) - secure.GET("sign-out", authHandler.signOutCallbackHandler) + secure := e.Group("/") + if authHandler.env.LogtoEndpoint != "" { + secure = e.Group("/", authHandler.logtoMiddleware) + } secure.GET("", appHandler.appHandler) secure.GET("sse", echo.WrapHandler(http.HandlerFunc(sse.ServeHTTP))) diff --git a/internal/env/env.go b/internal/env/env.go index 3942029..af1e3bd 100644 --- a/internal/env/env.go +++ b/internal/env/env.go @@ -13,13 +13,13 @@ type Config struct { Title string `env:"TITLE" envDefault:"goDash"` Port int `env:"PORT" envDefault:"4000" validate:"min=1024,max=49151"` Version string `env:"APP_VERSION"` - LocationLatitude float32 `env:"LOCATION_LATITUDE" envDefault:"48.780331609463815"` - LocationLongitude float32 `env:"LOCATION_LONGITUDE" envDefault:"9.177968320179422"` + LocationLatitude float32 `env:"LOCATION_LATITUDE" envDefault:"48.780331609463815" validate:"latitude"` + LocationLongitude float32 `env:"LOCATION_LONGITUDE" envDefault:"9.177968320179422" validate:"longitude"` WeatherKey string `env:"WEATHER_KEY"` WeatherUnits string `env:"WEATHER_UNITS" envDefault:"metric"` - WeatherLanguage string `env:"WEATHER_LANG" envDefault:"en"` - WeatherDigits bool `env:"WEATHER_DIGITS" envDefault:"true"` - LogtoEndpoint string `env:"LOGTO_ENDPOINT" validate:"url"` + WeatherLanguage string `env:"WEATHER_LANG" envDefault:"en" validate:"bcp47_language_tag"` + WeatherDigits bool `env:"WEATHER_DIGITS" envDefault:"false"` + LogtoEndpoint string `env:"LOGTO_ENDPOINT" default:"" validate:"omitempty,url"` LogtoAppId string `env:"LOGTO_APP_ID,unset"` LogtoAppSecret string `env:"LOGTO_APP_SECRET,unset"` SessionKey string `env:"SESSION_KEY,unset"` diff --git a/main.go b/main.go index 88eb8e3..276d529 100644 --- a/main.go +++ b/main.go @@ -28,14 +28,16 @@ func main() { e.HideBanner = true e.HidePort = true - e.Pre(middleware.RemoveTrailingSlash()) e.Use(middleware.Recover()) + e.Use(middleware.Logger()) e.Use(middleware.GzipWithConfig(middleware.GzipConfig{ Skipper: func(c echo.Context) bool { return strings.Contains(c.Path(), "sse") || strings.Contains(c.Path(), "sign") }, })) - e.Use(session.Middleware(memstore.NewMemStore([]byte(env.SessionKey)))) + if env.LogtoEndpoint != "" { + e.Use(session.Middleware(memstore.NewMemStore([]byte(env.SessionKey)))) + } sse := sse.New() sse.AutoReplay = false @@ -49,7 +51,7 @@ func main() { handlers.SetupRoutes(e, sse, appHandler, authHandler) slog.Info("starting server", "url", env.PublicUrl) - if err := e.Start(fmt.Sprintf(":%d", env.Port)); err != http.ErrServerClosed { + if err := e.Start(fmt.Sprintf("0.0.0.0:%d", env.Port)); err != http.ErrServerClosed { slog.Error("cannot start server", "err", err) os.Exit(1) } diff --git a/services/weather.services.go b/services/weather.services.go index 69d6de3..65d015d 100644 --- a/services/weather.services.go +++ b/services/weather.services.go @@ -17,7 +17,9 @@ func NewWeatherService(sse *sse.Server, env *env.Config) *WeatherService { var w = WeatherService{sse: sse, env: env} w.setWeatherUnits() sse.CreateStream("weather") - go w.updateWeather(time.Second * 90) + if env.WeatherKey != "" { + go w.updateWeather(time.Second * 90) + } return &w } @@ -66,7 +68,6 @@ func (w *WeatherService) updateWeather(interval time.Duration) { slog.Error("weather cannot be processed") } else { w.copyWeatherValues(&weatherResponse) - slog.Info("weather updated", "temp", w.CurrentWeather.Temp) } resp.Body.Close() json, _ := json.Marshal(w.CurrentWeather)