From 58761d011ee81e5f824fcc9102072f7bcdc3e4a9 Mon Sep 17 00:00:00 2001 From: Hendrik Schlehlein Date: Thu, 17 Jan 2019 22:09:41 +0100 Subject: [PATCH] added templating --- features/index/handler.go | 12 ++++++++++++ features/index/routes.go | 15 +++++++++++++++ gui/template.go | 35 +++++++++++++++++++++++++++++++++++ main.go | 16 ++++++++++++++-- templates/index.html | 12 ++++++++++++ 5 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 features/index/handler.go create mode 100644 features/index/routes.go create mode 100644 gui/template.go create mode 100644 templates/index.html 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