Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Embed web view templates to the binary
Browse files Browse the repository at this point in the history
  • Loading branch information
agis committed Apr 19, 2018
1 parent 0deaac7 commit d2aee8b
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 15 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
*.prof
*.pyc

config.json
fabfile.py

/mistry
/mistryd

cmd/mistryd/statik
config.json
fabfile.py
vendor/
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ install: fmt test

build: mistryd mistry

mistryd:
mistryd: generate
$(BUILDCMD) -o $(SERVER) cmd/mistryd/*.go

mistry:
$(BUILDCMD) -o $(CLIENT) cmd/mistry/*.go

test: mistry
test: generate mistry
$(TESTCMD) --filesystem plain

testall: test
Expand All @@ -34,3 +34,6 @@ fmt:

clean:
go clean ./...

generate:
go generate ./...
11 changes: 6 additions & 5 deletions cmd/mistryd/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions cmd/mistryd/Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@
[[constraint]]
name = "github.com/urfave/cli"
version = "1.20.0"

[[constraint]]
name = "github.com/rakyll/statik"
version = "0.1.1"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
36 changes: 31 additions & 5 deletions cmd/mistryd/server.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:generate statik -src=./public -f
package main

import (
Expand All @@ -17,8 +18,8 @@ import (

"sync"

"github.com/alecthomas/template"

"github.com/rakyll/statik/fs"
_ "github.com/skroutz/mistry/cmd/mistryd/statik"
"github.com/skroutz/mistry/pkg/broker"
"github.com/skroutz/mistry/pkg/tailer"
"github.com/skroutz/mistry/pkg/types"
Expand All @@ -41,12 +42,16 @@ type Server struct {
// matches a job id.
// The stored map type is [string]bool.
tq *sync.Map

fs http.FileSystem
}

// NewServer accepts a non-nil configuration and an optional logger, and
// returns a new Server.
// If logger is nil, server logs are disabled.
func NewServer(cfg *Config, logger *log.Logger) (*Server, error) {
var err error

if cfg == nil {
return nil, errors.New("config cannot be nil")
}
Expand All @@ -57,7 +62,13 @@ func NewServer(cfg *Config, logger *log.Logger) (*Server, error) {

s := new(Server)
mux := http.NewServeMux()
mux.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("public"))))

s.fs, err = fs.New()
if err != nil {
logger.Fatal(err)
}

mux.Handle("/", http.StripPrefix("/", http.FileServer(s.fs)))
mux.HandleFunc("/jobs", s.HandleNewJob)
mux.HandleFunc("/index/", s.HandleIndex)
mux.HandleFunc("/job/", s.HandleShowJob)
Expand Down Expand Up @@ -326,13 +337,28 @@ func (s *Server) HandleShowJob(w http.ResponseWriter, r *http.Request) {
}
}

t, err := template.ParseFiles("./public/templates/show.html")
f, err := s.fs.Open("/templates/show.html")
if err != nil {
s.Log.Print(err)
w.WriteHeader(http.StatusInternalServerError)
return
}

tmplBody, err := ioutil.ReadAll(f)
if err != nil {
s.Log.Print(err)
w.WriteHeader(http.StatusInternalServerError)
return
}

tmpl := template.New("jobshow")
tmpl, err = tmpl.Parse(string(tmplBody))
if err != nil {
s.Log.Print(err)
w.WriteHeader(http.StatusBadRequest)
return
}
t.Execute(w, ji)
tmpl.Execute(w, ji)
}

// HandleServerPush handles the server push logic.
Expand Down

0 comments on commit d2aee8b

Please sign in to comment.