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:
|
||||
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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)))
|
||||
|
|
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"`
|
||||
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"`
|
||||
|
|
8
main.go
8
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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue