Skip to content

Commit

Permalink
feat: template system
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Nov 26, 2019
1 parent d324fc2 commit bded3c9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
32 changes: 32 additions & 0 deletions internal/dvserver/pages.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
3 changes: 3 additions & 0 deletions internal/dvserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<title>DepViz Web</title>
<title>{{.Realm}}</title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,600,700&display=swap" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
Expand Down Expand Up @@ -37,7 +37,6 @@
<input type="submit" id="submit" value="generate" />
</form>
<div id="cy"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.10.2/cytoscape.min.js" integrity="sha256-6h61EhH/gL/t32vHghM/70U5k4hiB8XMnMQ2lCYV0k0=" crossorigin="anonymous"></script>
<script src="https://unpkg.com/cytoscape-node-html-label@1.1.5/dist/cytoscape-node-html-label.min.js"></script>
Expand Down

0 comments on commit bded3c9

Please sign in to comment.