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