diff --git a/features/index/handler.go b/features/index/handler.go new file mode 100644 index 0000000..f93cd8c --- /dev/null +++ b/features/index/handler.go @@ -0,0 +1,12 @@ +package index + +import ( + "html/template" + "net/http" +) + +func IndexGUIHandler(t template.Template) http.HandlerFunc { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + t.Lookup("index.html").Execute(w, nil) + }) +} diff --git a/features/index/routes.go b/features/index/routes.go new file mode 100644 index 0000000..837c53e --- /dev/null +++ b/features/index/routes.go @@ -0,0 +1,15 @@ +package index + +import ( + "html/template" + + "github.com/go-chi/chi" +) + +func GUIRoutes(t template.Template) *chi.Mux { + router := chi.NewRouter() + + router.Get("/", IndexGUIHandler(t)) + + return router +} diff --git a/gui/template.go b/gui/template.go new file mode 100644 index 0000000..17f6cd5 --- /dev/null +++ b/gui/template.go @@ -0,0 +1,35 @@ +package gui + +import ( + "fmt" + "html/template" + "io/ioutil" + "strings" +) + +func LoadTemplates() (*template.Template, error) { + return loadTemplates("templates/") +} + +func loadTemplates(path string) (*template.Template, error) { + dir, err := ioutil.ReadDir(path) + if err != nil { + return nil, err + } + + var ff []string + + for _, file := range dir { + filename := file.Name() + if strings.HasSuffix(filename, ".html") { + ff = append(ff, fmt.Sprintf("%s%s", path, filename)) + } + } + + templates, err := template.ParseFiles(ff...) + if err != nil { + return nil, err + } + + return templates, nil +} diff --git a/main.go b/main.go index f86ede9..65df4ee 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "html/template" "log" "net/http" "time" @@ -8,13 +9,15 @@ import ( "git.cliffbreak.de/Cliffbreak/tsviewer/config" "git.cliffbreak.de/Cliffbreak/tsviewer/features/channel" "git.cliffbreak.de/Cliffbreak/tsviewer/features/client" + "git.cliffbreak.de/Cliffbreak/tsviewer/features/index" "git.cliffbreak.de/Cliffbreak/tsviewer/features/server" + "git.cliffbreak.de/Cliffbreak/tsviewer/gui" "git.cliffbreak.de/Cliffbreak/tsviewer/service" "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" ) -func Routes(s service.Service) *chi.Mux { +func Routes(s service.Service, t template.Template) *chi.Mux { router := chi.NewRouter() router.Use( middleware.Logger, @@ -30,6 +33,10 @@ func Routes(s service.Service) *chi.Mux { r.Mount("/server", server.APIRoutes(s)) }) + router.Route("/", func(r chi.Router) { + r.Mount("/", index.GUIRoutes(t)) + }) + return router } @@ -45,7 +52,12 @@ func main() { } defer service.TSClient.Close() - router := Routes(*service) + templates, err := gui.LoadTemplates() + if err != nil { + log.Fatal(err) + } + + router := Routes(*service, *templates) log.Fatal("Handler: ", http.ListenAndServe(":8080", router)) } diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..9fadccd --- /dev/null +++ b/templates/index.html @@ -0,0 +1,12 @@ + + + + + TS3 Viewer + + + +

Index page!

+ + + \ No newline at end of file