diff options
author | Grail Finder <wohilas@gmail.com> | 2025-03-29 11:12:53 +0300 |
---|---|---|
committer | Grail Finder <wohilas@gmail.com> | 2025-03-29 11:12:53 +0300 |
commit | 3921db6166e2da895257496bb76dd115556699d3 (patch) | |
tree | 1be4f739121761085f69cb7706c60dbbe98a93e9 /internal/handlers/main.go |
init
Diffstat (limited to 'internal/handlers/main.go')
-rw-r--r-- | internal/handlers/main.go | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/internal/handlers/main.go b/internal/handlers/main.go new file mode 100644 index 0000000..960b26d --- /dev/null +++ b/internal/handlers/main.go @@ -0,0 +1,64 @@ +package handlers + +import ( + "demoon/config" + "demoon/internal/database/repos" + "demoon/internal/models" + "demoon/pkg/cache" + "html/template" + "log/slog" + "net/http" + "os" +) + +var defUS = models.UserScore{} + +// Handlers structure +type Handlers struct { + cfg config.Config + log *slog.Logger + repo repos.FullRepo + mc cache.Cache +} + +// NewHandlers constructor +func NewHandlers( + cfg config.Config, l *slog.Logger, repo repos.FullRepo, +) *Handlers { + if l == nil { + l = slog.New(slog.NewJSONHandler(os.Stdout, nil)) + } + h := &Handlers{ + cfg: cfg, + log: l, + repo: repo, + mc: cache.MemCache, + } + return h +} + +func (h *Handlers) Ping(w http.ResponseWriter, r *http.Request) { + h.log.Info("got ping request") + w.Write([]byte("pong")) +} + +func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) { + tmpl, err := template.ParseGlob("components/*.html") + if err != nil { + abortWithError(w, err.Error()) + return + } + // get recommendations + usernameRaw := r.Context().Value("username") + h.log.Info("got mainpage request", "username", usernameRaw) + if usernameRaw == nil { + tmpl.ExecuteTemplate(w, "main", defUS) + return + } + username := usernameRaw.(string) + if username == "" { + tmpl.ExecuteTemplate(w, "main", defUS) + return + } + tmpl.ExecuteTemplate(w, "main", nil) +} |