From ccb28d0d74723af96a4ac7cfeeb3147cd42df7bb Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Fri, 12 May 2017 16:06:01 +0200 Subject: [PATCH] commit embedded templates --- .gitignore | 1 + Makefile | 5 +- router/router.go | 7 +- server/template/template.go | 27 +----- server/template/template_gen.go | 127 ++++++++++++++++++++++++++++ store/datastore/sql/postgres/sql.go | 2 +- store/datastore/sql/sqlite/sql.go | 2 +- 7 files changed, 134 insertions(+), 37 deletions(-) create mode 100644 server/template/template_gen.go diff --git a/.gitignore b/.gitignore index f722aaba0..9c4844020 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ drone/drone !store/datastore/sql/sqlite/sql_gen.go !store/datastore/sql/mysql/sql_gen.go !store/datastore/sql/postgres/sql_gen.go +!server/template/template_gen.go #*.css *.txt *.zip diff --git a/Makefile b/Makefile index 7851584f2..c7c095e67 100644 --- a/Makefile +++ b/Makefile @@ -20,10 +20,7 @@ deps_backend: go get -u github.com/jteeuwen/go-bindata/... go get -u github.com/elazarl/go-bindata-assetfs/... -gen: gen_template gen_migrations - -gen_template: - go generate github.com/drone/drone/server/template +gen: gen_migrations gen_migrations: go generate github.com/drone/drone/store/datastore/ddl diff --git a/router/router.go b/router/router.go index 761c0dee6..cc293e43e 100644 --- a/router/router.go +++ b/router/router.go @@ -22,12 +22,7 @@ func Load(middleware ...gin.HandlerFunc) http.Handler { e := gin.New() e.Use(gin.Recovery()) - - if pattern := os.Getenv("DRONE_TEMPLATE_GLOB"); pattern == "" { - e.SetHTMLTemplate(template.Load()) - } else { - e.SetHTMLTemplate(template.Glob(pattern)) - } + e.SetHTMLTemplate(template.T) if dir := os.Getenv("DRONE_STATIC_DIR"); dir == "" { fs := http.FileServer(dist.AssetFS()) diff --git a/server/template/template.go b/server/template/template.go index 7bafbf8a2..6b2d993c3 100644 --- a/server/template/template.go +++ b/server/template/template.go @@ -1,36 +1,13 @@ package template -//go:generate go-bindata -pkg template -o template_gen.go files/ +//go:generate togo tmpl -package template -func funcMap -format html -input files/*.html import ( "encoding/json" "html/template" - "path/filepath" ) -// Load loads the templates from the embedded file map. This function will not -// compile if go generate is not executed before. -func Load() *template.Template { - dir, _ := AssetDir("files") - tmpl := template.New("_").Funcs(template.FuncMap{"json": marshal}) - for _, name := range dir { - path := filepath.Join("files", name) - src := MustAsset(path) - tmpl = template.Must( - tmpl.New(name).Parse(string(src)), - ) - } - - return tmpl -} - -// Glob loads the templates matching the given pattern. This function -// will not compile if go generate is not executed before. -func Glob(pattern string) *template.Template { - return template.Must( - template.New("_").Funcs(template.FuncMap{"json": marshal}).ParseGlob(pattern), - ) -} +var funcMap = template.FuncMap{"json": marshal} // marshal is a helper function to render data as JSON inside the template. func marshal(v interface{}) template.JS { diff --git a/server/template/template_gen.go b/server/template/template_gen.go new file mode 100644 index 000000000..625f7bf49 --- /dev/null +++ b/server/template/template_gen.go @@ -0,0 +1,127 @@ +package template + +import "html/template" + +// list of embedded template files. +var files = []struct { + name string + data string +}{ + { + name: "error.html", + data: error, + }, { + name: "index.html", + data: index, + }, { + name: "login.html", + data: login, + }, { + name: "logout.html", + data: logout, + }, +} + +// T exposes the embedded templates. +var T *template.Template + +func init() { + T = template.New("_").Funcs(funcMap) + for _, file := range files { + T = template.Must( + T.New(file.name).Parse(file.data), + ) + } +} + +// +// embedded template files. +// + +// files/error.html +var error = ` + + + + + + + + + + + error | drone + + + {{ .error }} + + +` + +// files/index.html +var index = ` + + + + + + {{ if .csrf }}{{ end }} + + + + + + + +
+ + + + + +` + +// files/login.html +var login = ` + + + + + + + + + + + login | drone + + +
+
+
+
+
+ + +
+
+ + +
+
+ +
+
+
+
+
+ + + +` + +// files/logout.html +var logout = `LOGOUT +` diff --git a/store/datastore/sql/postgres/sql.go b/store/datastore/sql/postgres/sql.go index 2b0b64887..08b773451 100644 --- a/store/datastore/sql/postgres/sql.go +++ b/store/datastore/sql/postgres/sql.go @@ -1,3 +1,3 @@ package postgres -//go:generate sqlbin sql --package=postgres +//go:generate togo sql --package=postgres diff --git a/store/datastore/sql/sqlite/sql.go b/store/datastore/sql/sqlite/sql.go index 60cd5f241..9b8267f68 100644 --- a/store/datastore/sql/sqlite/sql.go +++ b/store/datastore/sql/sqlite/sql.go @@ -1,3 +1,3 @@ package sqlite -//go:generate sqlbin sql --package=sqlite +//go:generate togo sql --package=sqlite