From 8d3b522ff0c1c00d7915d0e8a8a166f27b75112b Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Mon, 25 Mar 2024 20:31:51 +0100 Subject: [PATCH] Include user information --- components/system.templ | 39 +++++++++++++++++++++++++++++++++ components/user.templ | 40 ++++++++++++++++++++++++++++++++++ components/weather.templ | 32 --------------------------- compose.yml | 35 +---------------------------- handlers/app.handlers.go | 28 +++++++++++++----------- handlers/auth.handlers.go | 13 +++++++++++ main.go | 3 +-- views/home/home.templ | 9 ++++++-- views/layout/base.layout.templ | 1 + 9 files changed, 117 insertions(+), 83 deletions(-) create mode 100644 components/user.templ diff --git a/components/system.templ b/components/system.templ index 5a6b1e2..86d4449 100644 --- a/components/system.templ +++ b/components/system.templ @@ -58,3 +58,42 @@ templ Uptime(extraInfo string, id string, uptime services.Uptime) { } + +templ SystemScript() { + +} diff --git a/components/user.templ b/components/user.templ new file mode 100644 index 0000000..899ee49 --- /dev/null +++ b/components/user.templ @@ -0,0 +1,40 @@ +package components + +import ( + "github.com/logto-io/go/core" +) + +templ User(user *core.UserInfoResponse) { +
+ + +
+} diff --git a/components/weather.templ b/components/weather.templ index f634f1c..05bf43b 100644 --- a/components/weather.templ +++ b/components/weather.templ @@ -63,17 +63,10 @@ templ Weather(weather *services.OpenWeather) { templ WeatherScript() { } diff --git a/compose.yml b/compose.yml index 1204e23..95f73ed 100644 --- a/compose.yml +++ b/compose.yml @@ -1,40 +1,8 @@ networks: net: external: false - proxy: - external: false services: - logto: - depends_on: - logto-db: - condition: service_healthy - image: svhd/logto:${V_LOGTO} - entrypoint: ['sh', '-c', 'npm run cli db seed -- --swe && npm start'] - ports: - - 3001:3001 - - 3002:3002 - 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:${V_POSTGRES}-alpine - user: postgres - environment: - POSTGRES_USER: postgres - POSTGRES_PASSWORD: p0stgr3s - healthcheck: - test: ['CMD-SHELL', 'pg_isready'] - interval: 10s - timeout: 5s - retries: 5 - networks: - - net backend: build: @@ -63,6 +31,7 @@ services: - 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 @@ -70,8 +39,6 @@ services: - .:/app/ ports: - 4000:4000 - networks: - - proxy templ: profiles: [dev] diff --git a/handlers/app.handlers.go b/handlers/app.handlers.go index 24e8fcc..7214789 100644 --- a/handlers/app.handlers.go +++ b/handlers/app.handlers.go @@ -20,30 +20,32 @@ type WeatherService interface { GetCurrentWeather() *services.OpenWeather } -func NewAppHandler(env *env.Config, s SystemService, w WeatherService, b BookmarkService) *AppHandler { - +func NewAppHandler(env *env.Config, authHandler *AuthHandler, s SystemService, w WeatherService, b BookmarkService) *AppHandler { return &AppHandler{ env: env, - SystemService: s, - WeatherService: w, - BookmarkService: b, + authHandler: authHandler, + systemService: s, + weatherService: w, + bookmarkService: b, } } type AppHandler struct { env *env.Config - SystemService SystemService - WeatherService WeatherService - BookmarkService BookmarkService + authHandler *AuthHandler + systemService SystemService + weatherService WeatherService + bookmarkService BookmarkService } func (bh *AppHandler) appHandler(c echo.Context) error { - bookmarks := bh.BookmarkService.GetAllBookmarks() - staticSystem := bh.SystemService.GetStaticInformation() - liveSystem := bh.SystemService.GetLiveInformation() - weather := bh.WeatherService.GetCurrentWeather() + bookmarks := bh.bookmarkService.GetAllBookmarks() + staticSystem := bh.systemService.GetStaticInformation() + liveSystem := bh.systemService.GetLiveInformation() + weather := bh.weatherService.GetCurrentWeather() + user := bh.authHandler.GetUserInfo() titlePage := bh.env.Title - return renderView(c, home.HomeIndex(titlePage, bh.env.Version, home.Home(titlePage, bookmarks, staticSystem, liveSystem, weather))) + return renderView(c, home.HomeIndex(titlePage, bh.env.Version, home.Home(titlePage, user, bookmarks, staticSystem, liveSystem, weather))) } diff --git a/handlers/auth.handlers.go b/handlers/auth.handlers.go index 62608cc..5dfe9fd 100644 --- a/handlers/auth.handlers.go +++ b/handlers/auth.handlers.go @@ -6,6 +6,7 @@ import ( "github.com/labstack/echo/v4" "github.com/logto-io/go/client" + "github.com/logto-io/go/core" "gitlab.unjx.de/flohoss/godash/internal/env" ) @@ -23,6 +24,11 @@ func NewAuthHandler(env *env.Config) *AuthHandler { type AuthHandler struct { env *env.Config logtoConfig *client.LogtoConfig + userInfo *core.UserInfoResponse +} + +func (authHandler *AuthHandler) GetUserInfo() *core.UserInfoResponse { + return authHandler.userInfo } func (authHandler *AuthHandler) logtoMiddleware(next echo.HandlerFunc) echo.HandlerFunc { @@ -34,6 +40,13 @@ func (authHandler *AuthHandler) logtoMiddleware(next echo.HandlerFunc) echo.Hand if !logtoClient.IsAuthenticated() { return c.Redirect(http.StatusTemporaryRedirect, "/sign-in") } + if authHandler.userInfo == nil { + info, err := logtoClient.FetchUserInfo() + if err != nil { + return echo.ErrInternalServerError + } + authHandler.userInfo = &info + } return next(c) } } diff --git a/main.go b/main.go index 276d529..508e134 100644 --- a/main.go +++ b/main.go @@ -29,7 +29,6 @@ func main() { e.HidePort = true 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") @@ -46,8 +45,8 @@ func main() { w := services.NewWeatherService(sse, env) b := services.NewBookmarkService() - appHandler := handlers.NewAppHandler(env, s, w, b) authHandler := handlers.NewAuthHandler(env) + appHandler := handlers.NewAppHandler(env, authHandler, s, w, b) handlers.SetupRoutes(e, sse, appHandler, authHandler) slog.Info("starting server", "url", env.PublicUrl) diff --git a/views/home/home.templ b/views/home/home.templ index 3cbd574..aee4948 100644 --- a/views/home/home.templ +++ b/views/home/home.templ @@ -5,11 +5,15 @@ import ( "gitlab.unjx.de/flohoss/godash/views/layout" "fmt" "gitlab.unjx.de/flohoss/godash/components" + "github.com/logto-io/go/core" ) -templ Home(title string, bookmarks *services.Bookmarks, static *services.StaticInformation, live *services.LiveInformation, weather *services.OpenWeather) { +templ Home(title string, user *core.UserInfoResponse, bookmarks *services.Bookmarks, static *services.StaticInformation, live *services.LiveInformation, weather *services.OpenWeather) {
- @components.Weather(weather) +
+ @components.Weather(weather) + @components.User(user) +
@components.System("icon-[bi--cpu]",static.CPU.Name,"",static.CPU.Threads,"systemCpuPercentage","",live.CPU) @components.System("icon-[bi--nvme]",live.Disk.Value,fmt.Sprintf(" | %s", static.Disk.Total),static.Disk.Partitions,"systemDiskPercentage","systemDiskValue",live.Disk.Percentage) @@ -45,6 +49,7 @@ templ Home(title string, bookmarks *services.Bookmarks, static *services.StaticI }
@components.WeatherScript() + @components.SystemScript()
} diff --git a/views/layout/base.layout.templ b/views/layout/base.layout.templ index df0d581..b515ea6 100644 --- a/views/layout/base.layout.templ +++ b/views/layout/base.layout.templ @@ -18,6 +18,7 @@ templ Base(title, version string) { +