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

82 lines
2.1 KiB
Go
Raw Normal View History

2019-05-26 19:32:16 +00:00
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"time"
"git.cliffbreak.de/Cliffbreak/tsviewer/config"
"git.cliffbreak.de/Cliffbreak/tsviewer/features/api/channel"
"git.cliffbreak.de/Cliffbreak/tsviewer/features/api/client"
"git.cliffbreak.de/Cliffbreak/tsviewer/features/api/server"
"git.cliffbreak.de/Cliffbreak/tsviewer/features/web/index"
"git.cliffbreak.de/Cliffbreak/tsviewer/gui"
"git.cliffbreak.de/Cliffbreak/tsviewer/service"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
)
func Routes(s service.Service, t template.Template) *chi.Mux {
router := chi.NewRouter()
cors := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"Get"},
})
router.Use(
middleware.Logger,
middleware.Timeout(5*time.Second),
middleware.DefaultCompress,
middleware.RedirectSlashes,
middleware.Recoverer,
cors.Handler,
)
router.Route("/v1", func(r chi.Router) {
r.Mount("/channels", channel.APIRoutes(s))
r.Mount("/clients", client.APIRoutes(s))
r.Mount("/server", server.APIRoutes(s))
})
router.Route("/", func(r chi.Router) {
r.Mount("/", index.GUIRoutes(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)
if err != nil {
log.Fatal(err)
}
defer service.TSClient.Close()
log.Println("Query service connected to", fmt.Sprintf("%s:%d!", config.ServerTS.IP, config.ServerTS.PortQuery))
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.ServerWeb.Port)
log.Fatal("Handler: ", http.ListenAndServe(fmt.Sprintf(":%d", config.ServerWeb.Port), router))
}