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
Philipp 48d9621adf
All checks were successful
continuous-integration/drone/push Build is passing
make use of template engine, add footer
2021-10-01 22:49:47 +02:00

262 lines
7 KiB
Go

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())
h.Get("/australia", h.Australia())
h.Get("/asia", h.Asia())
return h
}
type Handler struct {
*chi.Mux
store steamServer.Store
}
func (h *Handler) Home() http.HandlerFunc {
type data struct {
Region string
Servers []steamServer.Server
Gamemodes []steamServer.Gamemode
Flags []steamServer.Flag
}
tmpl := template.Must(template.ParseFiles("templates/layout.html", "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)
}
ff, err := h.store.Flags()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
tmpl.Execute(w, data {
Region: "All Regions",
Servers: ss,
Gamemodes: gg,
Flags: ff,
})
}
}
func (h *Handler) Europe() http.HandlerFunc {
type data struct {
Region string
Servers []steamServer.Server
Gamemodes []steamServer.Gamemode
GamemodeCount []steamServer.GamemodeCount
Flags []steamServer.Flag
}
tmpl := template.Must(template.ParseFiles("templates/layout.html", "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
}
ff, err := h.store.Flags()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
c, err := h.store.GamemodeCount(reg.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
tmpl.Execute(w, data {
Region: "Europe",
Servers: ss,
Gamemodes: gg,
GamemodeCount: c,
Flags: ff,
})
}
}
func (h *Handler) America() http.HandlerFunc {
type data struct {
Region string
Servers []steamServer.Server
Gamemodes []steamServer.Gamemode
GamemodeCount []steamServer.GamemodeCount
Flags []steamServer.Flag
}
tmpl := template.Must(template.ParseFiles("templates/layout.html", "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
}
ff, err := h.store.Flags()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
c, err := h.store.GamemodeCount(reg.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
tmpl.Execute(w, data {
Region: "North America",
Servers: ss,
Gamemodes: gg,
GamemodeCount: c,
Flags: ff,
})
}
}
func (h *Handler) Asia() http.HandlerFunc {
type data struct {
Region string
Servers []steamServer.Server
Gamemodes []steamServer.Gamemode
GamemodeCount []steamServer.GamemodeCount
Flags []steamServer.Flag
}
tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/region.html"))
return func(w http.ResponseWriter, r *http.Request) {
reg, err := h.store.Region("Asia")
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
}
ff, err := h.store.Flags()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
c, err := h.store.GamemodeCount(reg.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
tmpl.Execute(w, data {
Region: "Asia",
Servers: ss,
Gamemodes: gg,
GamemodeCount: c,
Flags: ff,
})
}
}
func (h *Handler) Australia() http.HandlerFunc {
type data struct {
Region string
Servers []steamServer.Server
Gamemodes []steamServer.Gamemode
GamemodeCount []steamServer.GamemodeCount
Flags []steamServer.Flag
}
tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/region.html"))
return func(w http.ResponseWriter, r *http.Request) {
reg, err := h.store.Region("Australia")
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
}
ff, err := h.store.Flags()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
c, err := h.store.GamemodeCount(reg.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
tmpl.Execute(w, data {
Region: "Australia",
Servers: ss,
Gamemodes: gg,
GamemodeCount: c,
Flags: ff,
})
}
}