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 d9221087d0
All checks were successful
continuous-integration/drone/push Build is passing
change ul to proper table, added table sorting
2021-10-16 15:03:27 +02:00

288 lines
7.8 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("/favicon.ico", faviconHandler)
h.Get("/style.css", maincssHandler)
h.Get("/css/bootstrap.css", bootstrapHandler)
h.Get("/js/sorttable.js", sorttableHandler)
h.Get("/images/01.png", arrowHandler)
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 faviconHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/favicon.ico")
}
func maincssHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/css/style.css")
}
func bootstrapHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/css/bootstrap.css")
}
func sorttableHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/js/sorttable.js")
}
func arrowHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/images/01.png")
}
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,
})
}
}