added templating
This commit is contained in:
parent
119f9c4915
commit
58761d011e
5 changed files with 88 additions and 2 deletions
12
features/index/handler.go
Normal file
12
features/index/handler.go
Normal file
|
@ -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)
|
||||||
|
})
|
||||||
|
}
|
15
features/index/routes.go
Normal file
15
features/index/routes.go
Normal file
|
@ -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
|
||||||
|
}
|
35
gui/template.go
Normal file
35
gui/template.go
Normal file
|
@ -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
|
||||||
|
}
|
16
main.go
16
main.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
@ -8,13 +9,15 @@ import (
|
||||||
"git.cliffbreak.de/Cliffbreak/tsviewer/config"
|
"git.cliffbreak.de/Cliffbreak/tsviewer/config"
|
||||||
"git.cliffbreak.de/Cliffbreak/tsviewer/features/channel"
|
"git.cliffbreak.de/Cliffbreak/tsviewer/features/channel"
|
||||||
"git.cliffbreak.de/Cliffbreak/tsviewer/features/client"
|
"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/features/server"
|
||||||
|
"git.cliffbreak.de/Cliffbreak/tsviewer/gui"
|
||||||
"git.cliffbreak.de/Cliffbreak/tsviewer/service"
|
"git.cliffbreak.de/Cliffbreak/tsviewer/service"
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
"github.com/go-chi/chi/middleware"
|
"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 := chi.NewRouter()
|
||||||
router.Use(
|
router.Use(
|
||||||
middleware.Logger,
|
middleware.Logger,
|
||||||
|
@ -30,6 +33,10 @@ func Routes(s service.Service) *chi.Mux {
|
||||||
r.Mount("/server", server.APIRoutes(s))
|
r.Mount("/server", server.APIRoutes(s))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
router.Route("/", func(r chi.Router) {
|
||||||
|
r.Mount("/", index.GUIRoutes(t))
|
||||||
|
})
|
||||||
|
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +52,12 @@ func main() {
|
||||||
}
|
}
|
||||||
defer service.TSClient.Close()
|
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))
|
log.Fatal("Handler: ", http.ListenAndServe(":8080", router))
|
||||||
}
|
}
|
||||||
|
|
12
templates/index.html
Normal file
12
templates/index.html
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>TS3 Viewer</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<h1>Index page!</h1>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
Loading…
Reference in a new issue