go-tsviewer/main.go

75 lines
2 KiB
Go
Raw Normal View History

2019-01-14 20:07:11 +00:00
package main
import (
2019-01-31 18:37:04 +00:00
"fmt"
2019-01-17 21:09:41 +00:00
"html/template"
2019-01-14 20:07:11 +00:00
"log"
"net/http"
"time"
2019-01-17 00:35:51 +00:00
"git.cliffbreak.de/Cliffbreak/tsviewer/config"
2019-01-21 17:53:33 +00:00
"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"
2019-01-17 21:09:41 +00:00
"git.cliffbreak.de/Cliffbreak/tsviewer/gui"
2019-01-17 00:35:51 +00:00
"git.cliffbreak.de/Cliffbreak/tsviewer/service"
2019-01-14 20:07:11 +00:00
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
)
2019-01-17 21:09:41 +00:00
func Routes(s service.Service, t template.Template) *chi.Mux {
2019-01-14 20:07:11 +00:00
router := chi.NewRouter()
router.Use(
middleware.Logger,
middleware.Timeout(5*time.Second),
middleware.DefaultCompress,
middleware.RedirectSlashes,
middleware.Recoverer,
)
router.Route("/v1", func(r chi.Router) {
2019-01-16 21:01:48 +00:00
r.Mount("/channels", channel.APIRoutes(s))
r.Mount("/clients", client.APIRoutes(s))
r.Mount("/server", server.APIRoutes(s))
2019-01-14 20:07:11 +00:00
})
2019-01-17 21:09:41 +00:00
router.Route("/", func(r chi.Router) {
2019-01-21 17:53:33 +00:00
r.Mount("/", index.GUIRoutes(s, t))
2019-01-17 21:09:41 +00:00
})
2019-01-14 20:07:11 +00:00
return router
}
func main() {
2019-02-03 20:00:14 +00:00
log.Println("Loading configurations...")
2019-01-14 20:07:11 +00:00
config, err := config.New()
if err != nil {
log.Fatal(err)
}
2019-02-03 20:00:14 +00:00
log.Println("Configurations loaded!")
2019-01-14 20:07:11 +00:00
2019-02-03 20:00:14 +00:00
log.Println("Starting query service...")
2019-01-14 20:07:11 +00:00
service, err := service.New(*config)
if err != nil {
log.Fatal(err)
}
2019-01-15 00:46:39 +00:00
defer service.TSClient.Close()
2019-02-03 20:00:14 +00:00
log.Println("Query service connected to", fmt.Sprintf("%s:%d!", config.ServerTS.IP, config.ServerTS.PortQuery))
2019-01-14 20:07:11 +00:00
2019-02-03 20:00:14 +00:00
log.Println("Loading templates...")
2019-01-17 21:09:41 +00:00
templates, err := gui.LoadTemplates()
if err != nil {
log.Fatal(err)
}
2019-02-03 20:00:14 +00:00
log.Println("All templates loaded!")
2019-01-17 21:09:41 +00:00
router := Routes(*service, *templates)
2019-01-21 17:53:33 +00:00
router.Get("/static/*", func(w http.ResponseWriter, r *http.Request) {
http.StripPrefix("/static", http.FileServer(http.Dir("./static"))).ServeHTTP(w, r)
})
2019-01-14 20:07:11 +00:00
2019-02-03 20:00:14 +00:00
log.Println("Starting the web server locally on port", config.ServerWeb.Port)
2019-01-31 18:37:04 +00:00
log.Fatal("Handler: ", http.ListenAndServe(fmt.Sprintf(":%d", config.ServerWeb.Port), router))
2019-01-14 20:07:11 +00:00
}