Files
golias/handlers/handlers.go
2025-05-02 14:18:19 +03:00

70 lines
1.4 KiB
Go

package handlers
import (
"golias/config"
"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",
}
func HandlePing(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("pong"))
}
func HandleShowColor(w http.ResponseWriter, r *http.Request) {
word := r.URL.Query().Get("word")
color, exists := roundWords[word]
if !exists {
http.Error(w, "word not found", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(
fmt.Sprintf(`<div style="
background-color: %s;
padding: 1rem;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
min-width: 100px;
text-align: center;
color: white;
text-shadow: 0 2px 4px rgba(0,0,0,0.8);
cursor: pointer;
">%s</div>`, color, word)))
}
func HandleHome(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseGlob("components/*.html")
if err != nil {
abortWithError(w, err.Error())
return
}
tmpl.ExecuteTemplate(w, "main", roundWords)
}