From bded3c99d7a622ce9d35a74294630521a22e3bc0 Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Tue, 26 Nov 2019 17:04:57 +0100 Subject: [PATCH] feat: template system --- internal/dvserver/pages.go | 32 ++++++++++++++++++++++++++++++++ internal/dvserver/server.go | 3 +++ web/index.html | 3 +-- 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 internal/dvserver/pages.go diff --git a/internal/dvserver/pages.go b/internal/dvserver/pages.go new file mode 100644 index 000000000..c880e3b4b --- /dev/null +++ b/internal/dvserver/pages.go @@ -0,0 +1,32 @@ +package dvserver + +import ( + "fmt" + "html/template" + "net/http" + + packr "github.com/gobuffalo/packr/v2" +) + +func homepage(box *packr.Box, opts Opts) func(w http.ResponseWriter, r *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + // performance can be improved by computing the template only once, but it makes development harder + content, err := box.FindString("index.html") + if err != nil { + http.Error(w, fmt.Sprintf("500: %+v", err), 500) + return + } + + tmpl, err := template.New("home").Parse(content) + if err != nil { + http.Error(w, fmt.Sprintf("500: %+v", err), 500) + return + } + + data := opts + err = tmpl.Execute(w, data) + if err != nil { + http.Error(w, fmt.Sprintf("500: %+v", err), 500) + } + } +} diff --git a/internal/dvserver/server.go b/internal/dvserver/server.go index 30379bd07..1e966cb68 100644 --- a/internal/dvserver/server.go +++ b/internal/dvserver/server.go @@ -207,6 +207,9 @@ func New(ctx context.Context, h *cayley.Handle, schema *schema.Config, opts Opts box := packr.New("web", "../../web") chiutil.FileServer(r, "/", box) + // pages + r.Get("/", homepage(box, opts)) + http.DefaultServeMux = http.NewServeMux() // disables default handlers registere by importing net/http/pprof for security reasons listener, err := net.Listen("tcp", opts.HTTPBind) if err != nil { diff --git a/web/index.html b/web/index.html index 97d7ca684..cfa10d7a4 100644 --- a/web/index.html +++ b/web/index.html @@ -2,7 +2,7 @@ - DepViz Web + {{.Realm}} @@ -37,7 +37,6 @@
-