diff options
author | GrailFinder <wohilas@gmail.com> | 2024-04-28 07:03:36 +0300 |
---|---|---|
committer | GrailFinder <wohilas@gmail.com> | 2024-04-28 07:03:36 +0300 |
commit | 8d66ec58e2256412a2fd50ad9e651c09af1ea8cc (patch) | |
tree | 23ad5c78ba2b2da32628e9004fe932e6fa63e26b /internal/handlers/main.go | |
parent | b33be53ea9c0be523988a9412fd8e3f6a24782b3 (diff) |
Feat: auth middleware; login [wip]
Diffstat (limited to 'internal/handlers/main.go')
-rw-r--r-- | internal/handlers/main.go | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/internal/handlers/main.go b/internal/handlers/main.go index d779f42..aa9db4f 100644 --- a/internal/handlers/main.go +++ b/internal/handlers/main.go @@ -4,6 +4,7 @@ import ( "apjournal/config" "apjournal/internal/database/repos" "apjournal/internal/models" + "apjournal/pkg/cache" "html/template" "log/slog" "net/http" @@ -18,12 +19,12 @@ import ( type Handlers struct { cfg config.Config log *slog.Logger - repo *repos.Provider + repo repos.FullRepo + mc cache.Cache } // NewHandlers constructor func NewHandlers( - // cfg config.Config, s *service.Service, l *slog.Logger, cfg config.Config, l *slog.Logger, conn *sqlx.DB, ) *Handlers { if l == nil { @@ -33,6 +34,7 @@ func NewHandlers( cfg: cfg, log: l, repo: repos.NewProvider(conn), + mc: cache.MemCache, } return h } @@ -50,13 +52,22 @@ func (h *Handlers) Ping(w http.ResponseWriter, r *http.Request) { } func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) { - h.log.Info("got mainpage request") - // tmpl := template.Must(template.ParseFiles("components/index.html")) tmpl, err := template.ParseGlob("components/*.html") if err != nil { panic(err) } - userScore, err := h.repo.DBUserScoreGet("test") + usernameRaw := r.Context().Value("username") + h.log.Info("got mainpage request", "username", usernameRaw) + if usernameRaw == nil { + tmpl.ExecuteTemplate(w, "main", nil) + return + } + username := usernameRaw.(string) + if username == "" { + tmpl.ExecuteTemplate(w, "main", nil) + return + } + userScore, err := h.repo.DBUserScoreGet(username) if err != nil { h.log.Warn("got db err", "err", err) if err := h.repo.DBUserScoreCreate(&us); err != nil { @@ -65,13 +76,10 @@ func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) { tmpl.ExecuteTemplate(w, "main", us) return } - userScore.Actions, err = h.repo.DBActionList("test") + userScore.Actions, err = h.repo.DBActionList(username) if err != nil { panic(err) } - // tmpl.Execute(w, us) - // us.Username = "test" - // us.BurnTime = time.Now().Add(time.Duration(24) * time.Hour) tmpl.ExecuteTemplate(w, "main", userScore) } |