first commit
This commit is contained in:
commit
7e62f366e7
8 changed files with 224 additions and 0 deletions
48
web/handler.go
Normal file
48
web/handler.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
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())
|
||||
|
||||
return h
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
*chi.Mux
|
||||
|
||||
store steamServer.Store
|
||||
}
|
||||
|
||||
func (h *Handler) Home() http.HandlerFunc {
|
||||
type data struct {
|
||||
Servers []steamServer.Servers
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
tmpl.Execute(w, data {
|
||||
Servers: ss,
|
||||
})
|
||||
}
|
||||
}
|
Reference in a new issue