1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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)
}
|