go-bindata static files and templates

This commit is contained in:
emile 2015-09-11 18:47:54 +02:00
parent 79e5ec435a
commit 4a83136970
6 changed files with 32 additions and 9 deletions

View file

@ -123,7 +123,11 @@ func (provider *DockerProvider) loadDockerConfig() *Configuration {
provider.Domain,
}
gtf.Inject(DockerFuncMap)
tmpl, err := template.New(provider.Filename).Funcs(DockerFuncMap).ParseFiles(provider.Filename)
buf, err := Asset("providerTemplates/docker.tmpl")
if err != nil {
log.Error("Error reading file", err)
}
tmpl, err := template.New(provider.Filename).Funcs(DockerFuncMap).Parse(string(buf))
if err != nil {
log.Error("Error reading file", err)
return nil

10
generate.go Normal file
View file

@ -0,0 +1,10 @@
/*
Copyright
*/
//go:generate go get github.com/jteeuwen/go-bindata/...
//go:generate go get github.com/elazarl/go-bindata-assetfs/...
//go:generate rm -vf gen.go
//go:generate go-bindata -o gen.go static/... templates/... providerTemplates/...
package main

View file

@ -138,7 +138,11 @@ func (provider *MarathonProvider) loadMarathonConfig() *Configuration {
}
gtf.Inject(MarathonFuncMap)
tmpl, err := template.New(provider.Filename).Funcs(MarathonFuncMap).ParseFiles(provider.Filename)
buf, err := Asset("providerTemplates/marathon.tmpl")
if err != nil {
log.Error("Error reading file", err)
}
tmpl, err := template.New(provider.Filename).Funcs(MarathonFuncMap).ParseFiles(string(buf))
if err != nil {
log.Error("Error reading file:", err)
return nil

19
web.go
View file

@ -7,9 +7,14 @@ import (
"fmt"
"io/ioutil"
"encoding/json"
"github.com/elazarl/go-bindata-assetfs"
)
var renderer = render.New()
var renderer = render.New(render.Options{
Directory: "templates",
Asset: Asset,
AssetNames: AssetNames,
})
type WebProvider struct {
Address string
@ -19,24 +24,24 @@ type Page struct {
Configuration Configuration
}
func (provider *WebProvider) Provide(configurationChan chan<- *Configuration){
func (provider *WebProvider) Provide(configurationChan chan <- *Configuration) {
systemRouter := mux.NewRouter()
systemRouter.Methods("GET").PathPrefix("/web/").Handler(http.HandlerFunc(GetHtmlConfigHandler))
systemRouter.Methods("GET").PathPrefix("/api/").Handler(http.HandlerFunc(GetConfigHandler))
systemRouter.Methods("POST").PathPrefix("/api/").Handler(http.HandlerFunc(
func(rw http.ResponseWriter, r *http.Request){
func(rw http.ResponseWriter, r *http.Request) {
configuration := new(Configuration)
b, _ := ioutil.ReadAll(r.Body)
err:= json.Unmarshal(b, configuration)
err := json.Unmarshal(b, configuration)
if (err == nil) {
configurationChan <- configuration
GetConfigHandler(rw, r)
}else{
}else {
log.Error("Error parsing configuration %+v\n", err)
http.Error(rw, fmt.Sprintf("%+v", err), http.StatusBadRequest)
}
}))
systemRouter.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
}))
systemRouter.PathPrefix("/static").Handler(http.StripPrefix("/static/", http.FileServer(&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, Prefix: "static"})))
go http.ListenAndServe(provider.Address, systemRouter)
}