Skip to content

Commit

Permalink
Use 'esc' and 'go generate' instead of 'go-bindata'
Browse files Browse the repository at this point in the history
  • Loading branch information
abh committed Jun 5, 2015
1 parent 6ce4b5f commit d13f81a
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 149 deletions.
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
all: templates.go
go build

all: status.html.go
templates.go: templates/*.html monitor.go
go generate

status.html.go: templates/*.html
go-bindata -o status.html.go.tmp templates/
@echo "// +build !devel\n" > status.html.go
@cat status.html.go.tmp >> status.html.go
@rm status.html.go.tmp
devel:
go build -tags devel
4 changes: 4 additions & 0 deletions geodns.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ var VERSION string = "2.4.4"
var buildTime string
var gitVersion string

// Set development with the 'devel' build flag to load
// templates from disk instead of from the binary.
var development bool

var serverID string
var serverIP string
var serverGroups []string
Expand Down
8 changes: 5 additions & 3 deletions monitor.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

//go:generate esc -o templates.go templates/

import (
"encoding/json"
"fmt"
Expand Down Expand Up @@ -281,13 +283,13 @@ func StatusServer(zones Zones) func(http.ResponseWriter, *http.Request) {
}
}

statusTemplate, err := templates_status_html()
statusTemplate, err := FSString(development, "/templates/status.html")
if err != nil {
log.Println("Could not read template", err)
log.Println("Could not read template:", err)
w.WriteHeader(500)
return
}
tmpl, err := template.New("status_html").Parse(string(statusTemplate))
tmpl, err := template.New("status_html").Parse(statusTemplate)

if err != nil {
str := fmt.Sprintf("Could not parse template: %s", err)
Expand Down
128 changes: 0 additions & 128 deletions status.html.go

This file was deleted.

192 changes: 192 additions & 0 deletions templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package main

import (
"bytes"
"compress/gzip"
"io/ioutil"
"net/http"
"os"
"path"
"sync"
"time"
)

type _escLocalFS struct{}

var _escLocal _escLocalFS

type _escStaticFS struct{}

var _escStatic _escStaticFS

type _escFile struct {
compressed string
size int64
local string
isDir bool

data []byte
once sync.Once
name string
}

func (_escLocalFS) Open(name string) (http.File, error) {
f, present := _escData[path.Clean(name)]
if !present {
return nil, os.ErrNotExist
}
return os.Open(f.local)
}

func (_escStaticFS) prepare(name string) (*_escFile, error) {
f, present := _escData[path.Clean(name)]
if !present {
return nil, os.ErrNotExist
}
var err error
f.once.Do(func() {
f.name = path.Base(name)
if f.size == 0 {
return
}
var gr *gzip.Reader
gr, err = gzip.NewReader(bytes.NewBufferString(f.compressed))
if err != nil {
return
}
f.data, err = ioutil.ReadAll(gr)
})
if err != nil {
return nil, err
}
return f, nil
}

func (fs _escStaticFS) Open(name string) (http.File, error) {
f, err := fs.prepare(name)
if err != nil {
return nil, err
}
return f.File()
}

func (f *_escFile) File() (http.File, error) {
type httpFile struct {
*bytes.Reader
*_escFile
}
return &httpFile{
Reader: bytes.NewReader(f.data),
_escFile: f,
}, nil
}

func (f *_escFile) Close() error {
return nil
}

func (f *_escFile) Readdir(count int) ([]os.FileInfo, error) {
return nil, nil
}

func (f *_escFile) Stat() (os.FileInfo, error) {
return f, nil
}

func (f *_escFile) Name() string {
return f.name
}

func (f *_escFile) Size() int64 {
return f.size
}

func (f *_escFile) Mode() os.FileMode {
return 0
}

func (f *_escFile) ModTime() time.Time {
return time.Time{}
}

func (f *_escFile) IsDir() bool {
return f.isDir
}

func (f *_escFile) Sys() interface{} {
return f
}

// FS returns a http.Filesystem for the embedded assets. If useLocal is true,
// the filesystem's contents are instead used.
func FS(useLocal bool) http.FileSystem {
if useLocal {
return _escLocal
}
return _escStatic
}

// FSByte returns the named file from the embedded assets. If useLocal is
// true, the filesystem's contents are instead used.
func FSByte(useLocal bool, name string) ([]byte, error) {
if useLocal {
f, err := _escLocal.Open(name)
if err != nil {
return nil, err
}
return ioutil.ReadAll(f)
}
f, err := _escStatic.prepare(name)
if err != nil {
return nil, err
}
return f.data, nil
}

// FSMustByte is the same as FSByte, but panics if name is not present.
func FSMustByte(useLocal bool, name string) []byte {
b, err := FSByte(useLocal, name)
if err != nil {
panic(err)
}
return b
}

// FSString is the string version of FSByte.
func FSString(useLocal bool, name string) (string, error) {
b, err := FSByte(useLocal, name)
return string(b), err
}

// FSMustString is the string version of FSMustByte.
func FSMustString(useLocal bool, name string) string {
return string(FSMustByte(useLocal, name))
}

var _escData = map[string]*_escFile{

"/templates/status.html": {
local: "templates/status.html",
size: 3181,
compressed: "" +
"\x1f\x8b\b\x00\x00\tn\x88\x00\xff\xb4WMS\xeb6\x14]'\xbfB\xe3)\xbb\"\x13Jf\x1a\xeax\x03\f]4\x14\x9a\xb6\x8b\xb7S\xac\x9b\xd8\xf3\x1c)OR\b\xbcL\xfe\xfb\xbb\x92?b;&\x04\xbfa\x83\xe5#\x9d+\x9d{O\xaeL\x10\x9be\x1a\x0610\x1e\x06&1)\x84\xf7 o\x1f\xa6d\xbb%\xf4\u007fP:\x91\x82" +
"\xecv\x81\x9fM\xf6\x834\x11_I\xac`>\xf6|_\x80\xe1\x82љ\x94F\x1b\xc5V\x11\x174\x92K\xdfl\x12c@\x9d\x97\x13\xfe%\xfd\x8d\x0e\xfcHk\xbf\xc4\xceq\xe5,\x11\xc0\xe92A\x9a\xd6\x1eQ\x90\x8e=m^S\xd01\x80\xf1N\xdd\xcf\x01\x1bf\xa2\xb8\xd8\b\xd4:\x05&\xf6\xbb\x1d\xddĽ\x85\xfd\x9e\xe1\xf4\xbb\x14" +
" \xd8\x12~űM\v(\xb2%s)\xcc\xf9\x06\x92El\xae\xc9L\xa6\xfc\x0f\xb2\xeb\a~N\vf\x92\xbf\x86\xfd~\xc0\x93g\x12\xa5L\xeb\xb1\x17!\x83\xa18\xe5ىx\x10ާr\xc6\xd2\xc0\xc7a}\xa5\x92\x1b\xbb\xa6W\xc5\xf4\x8a\x89\xdf\x1d\xda\v\f\x9b\xa5PLd/\xee/&W\xe1\xe9\x80篸#\a\xa1\x81#" +
"\xafgy\xca=q\xc0\vv\"\xe6\x92d\xa2\xbc\xfc@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A:\xb5\x9f\x1a֍7ق\x8aM\x9d\v\x91\x9b\xccK:B\xbdVc֝\x19\xbaк\xf9+.l^\x8bָG\xf3\x10W\xd6\xcb.J\xae&o\x11\xee\x94-\x17\xfav\v\x82\xdbx\xc5 W\x9d&Д}\xe3\xb0Su\xe7\x01N\x15\x9e\x05?\xa2\xbc\x1a\xf7\xcbѾo\xee\xdbf\xf6a\x8c\xdd\xd2\xfe\x97\xd1" +
"\xff\x11\x00\x00\xff\xff\xb0\x9dR\xc9m\f\x00\x00",
},

"/": {
isDir: true,
local: "/",
},

"/templates": {
isDir: true,
local: "/templates",
},
}
15 changes: 3 additions & 12 deletions templates_devel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@

package main

import (
"io/ioutil"
"log"
)

func templates_status_html() ([]byte, error) {
data, err := ioutil.ReadFile("templates/status.html")
if err != nil {
log.Println("Could not open status.html", err)
return nil, err
}
return data, nil
func init() {
// load templates from disk
development = true
}

0 comments on commit d13f81a

Please sign in to comment.