Files
golias/handlers/handlers.go
2025-05-07 06:56:17 +03:00

76 lines
1.5 KiB
Go

package handlers
import (
"golias/config"
"golias/models"
"golias/pkg/cache"
"html/template"
"log/slog"
"net/http"
"os"
)
var (
log *slog.Logger
cfg *config.Config
memcache cache.Cache
)
func init() {
log = slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelDebug,
AddSource: true,
}))
memcache = cache.MemCache
cfg = config.LoadConfigOrDefault("")
}
var roundWords = map[string]string{
"hamster": "blue",
"child": "red",
"wheel": "white",
"condition": "black",
"test": "white",
"ball": "blue",
"violin": "red",
"rat": "white",
"perplexity": "blue",
"notion": "red",
"guitar": "blue",
"ocean": "blue",
"moon": "blue",
"coffee": "blue",
"mountain": "blue",
"book": "blue",
"camera": "blue",
"apple": "red",
"fire": "red",
"rose": "red",
"sun": "red",
"cherry": "red",
"heart": "red",
"tomato": "red",
"cloud": "white",
}
func HandlePing(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("pong"))
}
func HandleHome(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseGlob("components/*.html")
if err != nil {
abortWithError(w, err.Error())
return
}
userState, _ := getStateByCtx(r.Context())
if userState == nil {
userState = &models.UserState{}
}
// if err != nil {
// abortWithError(w, err.Error())
// return
// }
tmpl.ExecuteTemplate(w, "base", userState)
}