This repository has been archived on 2019-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
scoreboard/main.go

69 lines
1.7 KiB
Go
Raw Permalink Normal View History

2019-05-26 19:32:16 +00:00
package main
import (
"fmt"
2019-05-27 21:46:09 +00:00
"html/template"
2019-05-26 19:32:16 +00:00
"log"
"net/http"
"time"
2019-05-27 21:46:09 +00:00
"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"
2019-05-26 19:32:16 +00:00
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
)
2019-05-27 21:46:09 +00:00
func Routes(s *service.DataService, t *template.Template) *chi.Mux {
2019-05-26 19:32:16 +00:00
router := chi.NewRouter()
2019-05-26 19:45:33 +00:00
2019-05-26 19:32:16 +00:00
router.Use(
middleware.Logger,
middleware.Timeout(5*time.Second),
middleware.RedirectSlashes,
middleware.Recoverer,
)
router.Route("/", func(r chi.Router) {
2019-05-27 21:46:09 +00:00
r.Mount("/", index.Routes(s, t))
r.Mount("/groups", group.Routes(s, t))
r.Mount("/games", game.Routes(s, t))
2019-05-26 19:32:16 +00:00
})
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...")
2019-05-27 21:46:09 +00:00
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()
2019-05-26 19:32:16 +00:00
if err != nil {
log.Fatal(err)
}
2019-05-27 21:46:09 +00:00
log.Println("All templates loaded!")
2019-05-26 19:32:16 +00:00
2019-05-27 21:46:09 +00:00
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)
})
2019-05-26 19:32:16 +00:00
2019-05-27 21:46:09 +00:00
log.Println("Starting the web server locally on port", config.Port)
log.Fatal("Handler: ", http.ListenAndServe(fmt.Sprintf(":%d", config.Port), router))
2019-05-26 19:32:16 +00:00
}