commit embedded templates

This commit is contained in:
Brad Rydzewski 2017-05-12 16:06:01 +02:00
parent 697cb0ea0e
commit ccb28d0d74
7 changed files with 134 additions and 37 deletions

1
.gitignore vendored
View file

@ -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

View file

@ -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

View file

@ -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())

View file

@ -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 {

View file

@ -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 = `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta content="width=device-width, initial-scale=1" name="viewport"/>
<meta content="ie=edge" http-equiv="x-ua-compatible"/>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"/>
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet"/>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"/>
<link href="/static/favicon.ico" rel="icon" type="image/x-icon"/>
<link rel="stylesheet" href="/static/app.css" />
<title>error | drone</title>
</head>
<body>
{{ .error }}
</body>
</html>
`
// files/index.html
var index = `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta content="width=device-width, initial-scale=1" name="viewport"/>
<meta content="ie=edge" http-equiv="x-ua-compatible"/>
{{ if .csrf }}<meta name="csrf-token" content="{{ .csrf }}" />{{ end }}
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"/>
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet"/>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"/>
<link href="/static/app.css" rel="stylesheet"/>
<link href="/static/favicon.ico" rel="icon" type="image/x-icon"/>
</head>
<body>
<div id="app"></div>
<script>
window.STATE_FROM_SERVER={{ . | json }};
</script>
<script src="https://code.getmdl.io/1.1.3/material.min.js"></script>
<script src="/static/app.js"></script>
</body>
</html>
`
// files/login.html
var login = `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta content="width=device-width, initial-scale=1" name="viewport"/>
<meta content="ie=edge" http-equiv="x-ua-compatible"/>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"/>
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet"/>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"/>
<link href="/static/favicon.ico" rel="icon" type="image/x-icon"/>
<link rel="stylesheet" href="/static/app.css" />
<title>login | drone</title>
</head>
<body>
<div class="mdl-grid">
<div class="mdl-layout-spacer"></div>
<div class="mdl-card">
<form action="/authorize" method="post">
<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="text" id="username" name="username" />
<label class="mdl-textfield__label" for="username">Username</label>
</div>
<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="password" id="userpass" name="password" />
<label class="mdl-textfield__label" for="userpass">Password</label>
</div>
<div class="mdl-dialog__actions">
<input type="submit" class="mdl-button mdl-button--colored mdl-js-button" value="Login" />
</div>
</form>
</div>
<div class="mdl-layout-spacer"></div>
</div>
<script src="https://code.getmdl.io/1.1.3/material.min.js"></script>
</body>
</html>
`
// files/logout.html
var logout = `LOGOUT
`

View file

@ -1,3 +1,3 @@
package postgres
//go:generate sqlbin sql --package=postgres
//go:generate togo sql --package=postgres

View file

@ -1,3 +1,3 @@
package sqlite
//go:generate sqlbin sql --package=sqlite
//go:generate togo sql --package=sqlite