package main
import (
"fmt"
"html/template"
"log"
"net/http"
"time"
"git.cliffbreak.de/haveachin/scoreboard/config"
"git.cliffbreak.de/haveachin/scoreboard/features/game"
"git.cliffbreak.de/haveachin/scoreboard/features/group"
"git.cliffbreak.de/haveachin/scoreboard/features/index"
"git.cliffbreak.de/haveachin/scoreboard/gui"
"git.cliffbreak.de/haveachin/scoreboard/service"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
)
func Routes(s *service.DataService, t *template.Template) *chi.Mux {
router := chi.NewRouter()
router.Use(
middleware.Logger,
middleware.Timeout(5*time.Second),
middleware.RedirectSlashes,
middleware.Recoverer,
)
router.Route("/", func(r chi.Router) {
r.Mount("/", index.Routes(s, t))
r.Mount("/groups", group.Routes(s, t))
r.Mount("/games", game.Routes(s, t))
})
return router
}
func main() {
log.Println("Loading configurations...")
config, err := config.New()
if err != nil {
log.Fatal(err)
}
log.Println("Configurations loaded!")
log.Println("Starting query service...")
service, err := service.New(config.DataService)
if err != nil {
log.Fatal(err)
}
log.Println("Query service connected to", fmt.Sprintf("%s:%d!", config.DataService.IP, config.DataService.Port))
log.Println("Loading templates...")
templates, err := gui.LoadTemplates()
if err != nil {
log.Fatal(err)
}
log.Println("All templates loaded!")
router := Routes(service, templates)
router.Get("/static/*", func(w http.ResponseWriter, r *http.Request) {
http.StripPrefix("/static", http.FileServer(http.Dir("./static"))).ServeHTTP(w, r)
})
log.Println("Starting the web server locally on port", config.Port)
log.Fatal("Handler: ", http.ListenAndServe(fmt.Sprintf(":%d", config.Port), router))
}