From 4a83136970e44f7f85d9f1acbc1bfc17ead8cbec Mon Sep 17 00:00:00 2001 From: emile Date: Fri, 11 Sep 2015 18:47:54 +0200 Subject: [PATCH] go-bindata static files and templates --- docker.go | 6 +++++- generate.go | 10 ++++++++++ marathon.go | 6 +++++- docker.tmpl => providerTemplates/docker.tmpl | 0 .../marathon.tmpl | 0 web.go | 19 ++++++++++++------- 6 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 generate.go rename docker.tmpl => providerTemplates/docker.tmpl (100%) rename marathon.tmpl => providerTemplates/marathon.tmpl (100%) diff --git a/docker.go b/docker.go index c2a1b99d1..bd92b068b 100644 --- a/docker.go +++ b/docker.go @@ -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 diff --git a/generate.go b/generate.go new file mode 100644 index 000000000..69c4e6adf --- /dev/null +++ b/generate.go @@ -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 \ No newline at end of file diff --git a/marathon.go b/marathon.go index 05e28068a..83c302dff 100644 --- a/marathon.go +++ b/marathon.go @@ -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 diff --git a/docker.tmpl b/providerTemplates/docker.tmpl similarity index 100% rename from docker.tmpl rename to providerTemplates/docker.tmpl diff --git a/marathon.tmpl b/providerTemplates/marathon.tmpl similarity index 100% rename from marathon.tmpl rename to providerTemplates/marathon.tmpl diff --git a/web.go b/web.go index 947807969..8162c62fa 100644 --- a/web.go +++ b/web.go @@ -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) }