package web import ( "html/template" "net/http" "git.snrd.de/Spaenny/steamServer" "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" ) func NewHandler(store steamServer.Store) *Handler { h := &Handler { Mux: chi.NewMux(), store: store, } h.Use(middleware.Logger) h.Get("/", h.Home()) h.Get("/europe", h.Europe()) h.Get("/america", h.America()) return h } type Handler struct { *chi.Mux store steamServer.Store } func (h *Handler) Home() http.HandlerFunc { type data struct { Servers []steamServer.Server Gamemodes []steamServer.Gamemode } tmpl := template.Must(template.ParseFiles("templates/home.html")) return func(w http.ResponseWriter, r *http.Request) { ss, err := h.store.Servers() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } gg, err := h.store.Gamemodes() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } tmpl.Execute(w, data { Servers: ss, Gamemodes: gg, }) } } func (h *Handler) Europe() http.HandlerFunc { type data struct { Region string Servers []steamServer.Server Gamemodes []steamServer.Gamemode } tmpl := template.Must(template.ParseFiles("templates/region.html")) return func(w http.ResponseWriter, r *http.Request) { reg, err := h.store.Region("Europe") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } gg, err := h.store.Gamemodes() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } ss, err := h.store.ServersByRegion(reg.ID) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } tmpl.Execute(w, data { Region: "Europe", Servers: ss, Gamemodes: gg, }) } } func (h *Handler) America() http.HandlerFunc { type data struct { Region string Servers []steamServer.Server Gamemodes []steamServer.Gamemode } tmpl := template.Must(template.ParseFiles("templates/region.html")) return func(w http.ResponseWriter, r *http.Request) { reg, err := h.store.Region("North America") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } gg, err := h.store.Gamemodes() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } ss, err := h.store.ServersByRegion(reg.ID) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } tmpl.Execute(w, data { Region: "North America", Servers: ss, Gamemodes: gg, }) } }