This repository has been archived on 2024-05-18. You can view files and clone it, but cannot push or open issues or pull requests.
steamServer/web/handler.go

129 lines
3 KiB
Go
Raw Normal View History

2021-09-20 14:57:24 +02:00
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())
2021-09-20 22:55:12 +02:00
h.Get("/europe", h.Europe())
h.Get("/america", h.America())
2021-09-20 14:57:24 +02:00
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
2021-09-20 14:57:24 +02:00
}
tmpl := template.Must(template.ParseFiles("templates/home.html"))
2021-09-20 14:57:24 +02:00
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)
}
2021-09-20 14:57:24 +02:00
tmpl.Execute(w, data {
Servers: ss,
Gamemodes: gg,
2021-09-20 14:57:24 +02:00
})
}
}
2021-09-20 22:55:12 +02:00
func (h *Handler) Europe() http.HandlerFunc {
type data struct {
Region string
2021-09-20 22:55:12 +02:00
Servers []steamServer.Server
Gamemodes []steamServer.Gamemode
2021-09-20 22:55:12 +02:00
}
tmpl := template.Must(template.ParseFiles("templates/region.html"))
2021-09-20 22:55:12 +02:00
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)
}
2021-09-20 22:55:12 +02:00
ss, err := h.store.ServersByRegion(reg.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2021-09-20 22:55:12 +02:00
tmpl.Execute(w, data {
Region: "Europe",
2021-09-20 22:55:12 +02:00
Servers: ss,
Gamemodes: gg,
2021-09-20 22:55:12 +02:00
})
}
}
2021-09-20 22:55:12 +02:00
func (h *Handler) America() http.HandlerFunc {
type data struct {
Region string
2021-09-20 22:55:12 +02:00
Servers []steamServer.Server
Gamemodes []steamServer.Gamemode
2021-09-20 22:55:12 +02:00
}
tmpl := template.Must(template.ParseFiles("templates/region.html"))
2021-09-20 22:55:12 +02:00
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)
}
2021-09-20 22:55:12 +02:00
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",
2021-09-20 22:55:12 +02:00
Servers: ss,
Gamemodes: gg,
2021-09-20 22:55:12 +02:00
})
}
}