summaryrefslogtreecommitdiff
path: root/boardvoting.go
diff options
context:
space:
mode:
Diffstat (limited to 'boardvoting.go')
-rw-r--r--boardvoting.go36
1 files changed, 23 insertions, 13 deletions
diff --git a/boardvoting.go b/boardvoting.go
index c1fa28f..a0d6359 100644
--- a/boardvoting.go
+++ b/boardvoting.go
@@ -10,7 +10,7 @@ import (
"encoding/pem"
"flag"
"fmt"
-
+ "git.cacert.org/cacert-boardvoting/boardvoting"
"github.com/Masterminds/sprig"
"github.com/gorilla/sessions"
_ "github.com/mattn/go-sqlite3"
@@ -35,21 +35,31 @@ var log *logging.Logger
const sessionCookieName = "votesession"
-func getTemplateFilenames(templates []string) (result []string) {
- result = make([]string, len(templates))
- for i := range templates {
- result[i] = fmt.Sprintf("templates/%s", templates[i])
- }
- return result
-}
-
func renderTemplate(w http.ResponseWriter, templates []string, context interface{}) {
funcMaps := sprig.FuncMap()
funcMaps["nl2br"] = func(text string) template.HTML {
return template.HTML(strings.Replace(template.HTMLEscapeString(text), "\n", "<br>", -1))
}
- t := template.Must(template.New(templates[0]).Funcs(funcMaps).ParseFiles(getTemplateFilenames(templates)...))
- if err := t.Execute(w, context); err != nil {
+
+ var baseTemplate *template.Template
+
+ for count, t := range templates {
+ if assetBytes, err := boardvoting.Asset(fmt.Sprintf("templates/%s", t)); err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ } else {
+ if count == 0 {
+ if baseTemplate, err = template.New(t).Funcs(funcMaps).Parse(string(assetBytes)); err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ }
+ } else {
+ if _, err := baseTemplate.New(t).Funcs(funcMaps).Parse(string(assetBytes)); err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ }
+ }
+ }
+ }
+
+ if err := baseTemplate.Execute(w, context); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
@@ -733,7 +743,7 @@ func readConfig() {
}
func setupDbConfig(ctx context.Context) {
- database, err := sql.Open("sqlite3", config.DatabaseFile)
+ database, err := sql.Open("sqlite3", config.DatabaseFile)
if err != nil {
log.Panicf("Opening database failed: %v", err)
}
@@ -777,7 +787,7 @@ func setupHandlers() {
http.Handle("/newmotion/", motionsHandler{})
http.Handle("/proxy/", &decisionVoteHandler{})
http.Handle("/vote/", &decisionVoteHandler{})
- http.Handle("/static/", http.FileServer(http.Dir(".")))
+ http.Handle("/static/", http.FileServer(boardvoting.GetAssetFS()))
http.Handle("/", http.RedirectHandler("/motions/", http.StatusMovedPermanently))
}