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

289 lines
7.8 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)
2021-10-04 16:23:27 +02:00
h.Get("/favicon.ico", faviconHandler)
2021-10-13 21:23:08 +02:00
h.Get("/style.css", maincssHandler)
h.Get("/css/bootstrap.css", bootstrapHandler)
h.Get("/js/sorttable.js", sorttableHandler)
2021-10-13 21:23:08 +02:00
h.Get("/images/01.png", arrowHandler)
2021-09-20 14:57:24 +02:00
h.Get("/", h.Home())
2021-09-20 22:55:12 +02:00
h.Get("/europe", h.Europe())
h.Get("/america", h.America())
2021-09-24 13:19:44 +02:00
h.Get("/australia", h.Australia())
h.Get("/asia", h.Asia())
2021-09-20 14:57:24 +02:00
return h
}
type Handler struct {
*chi.Mux
store steamServer.Store
}
2021-10-04 16:23:27 +02:00
func faviconHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/favicon.ico")
}
2021-10-13 21:23:08 +02:00
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")
}
2021-10-13 21:23:08 +02:00
func arrowHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/images/01.png")
}
2021-09-20 14:57:24 +02:00
func (h *Handler) Home() http.HandlerFunc {
type data struct {
2021-09-23 15:58:56 +02:00
Region string
Servers []steamServer.Server
Gamemodes []steamServer.Gamemode
2021-09-23 15:58:56 +02:00
Flags []steamServer.Flag
2021-09-20 14:57:24 +02:00
}
tmpl := template.Must(template.ParseFiles("templates/layout.html", "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-23 15:58:56 +02:00
ff, err := h.store.Flags()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
2021-09-20 14:57:24 +02:00
tmpl.Execute(w, data {
2021-09-24 13:19:44 +02:00
Region: "All Regions",
2021-09-20 14:57:24 +02:00
Servers: ss,
Gamemodes: gg,
2021-09-23 15:58:56 +02:00
Flags: ff,
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-24 13:19:44 +02:00
GamemodeCount []steamServer.GamemodeCount
2021-09-23 15:58:56 +02:00
Flags []steamServer.Flag
2021-09-20 22:55:12 +02:00
}
tmpl := template.Must(template.ParseFiles("templates/layout.html", "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-23 15:58:56 +02:00
ff, err := h.store.Flags()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
2021-09-24 13:19:44 +02:00
c, err := h.store.GamemodeCount(reg.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
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-24 13:19:44 +02:00
GamemodeCount: c,
2021-09-23 15:58:56 +02:00
Flags: ff,
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-24 13:19:44 +02:00
GamemodeCount []steamServer.GamemodeCount
2021-09-23 15:58:56 +02:00
Flags []steamServer.Flag
2021-09-20 22:55:12 +02:00
}
tmpl := template.Must(template.ParseFiles("templates/layout.html", "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
}
2021-09-23 15:58:56 +02:00
ff, err := h.store.Flags()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
2021-09-24 13:19:44 +02:00
c, err := h.store.GamemodeCount(reg.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
2021-09-20 22:55:12 +02:00
tmpl.Execute(w, data {
Region: "North America",
2021-09-20 22:55:12 +02:00
Servers: ss,
Gamemodes: gg,
2021-09-24 13:19:44 +02:00
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"))
2021-09-24 13:19:44 +02:00
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"))
2021-09-24 13:19:44 +02:00
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,
2021-09-23 15:58:56 +02:00
Flags: ff,
2021-09-20 22:55:12 +02:00
})
}
}