From 47a1aa6396565b7f5db5bd04a5ee5d36b4d399d4 Mon Sep 17 00:00:00 2001 From: andreimarcu Date: Tue, 14 Jun 2016 23:21:39 -0700 Subject: [PATCH] Infer sitename from Host if empty --- display.go | 4 ++-- pages.go | 18 ++++++++++-------- server.go | 5 +---- templates.go | 14 ++++++++++++++ templates/API.html | 4 ++-- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/display.go b/display.go index 9105119..4d3896b 100644 --- a/display.go +++ b/display.go @@ -112,7 +112,7 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) { tpl = Templates["display/file.html"] } - err = tpl.ExecuteWriter(pongo2.Context{ + err = renderTemplate(tpl, pongo2.Context{ "mime": metadata.Mimetype, "filename": fileName, "size": sizeHuman, @@ -120,7 +120,7 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) { "extra": extra, "lines": lines, "files": metadata.ArchiveFiles, - }, w) + }, r, w) if err != nil { oopsHandler(c, w, r, RespHTML, "") diff --git a/pages.go b/pages.go index 597e5a4..abd2cbd 100644 --- a/pages.go +++ b/pages.go @@ -20,23 +20,25 @@ const ( ) func indexHandler(c web.C, w http.ResponseWriter, r *http.Request) { - err := Templates["index.html"].ExecuteWriter(pongo2.Context{ + err := renderTemplate(Templates["index.html"], pongo2.Context{ "maxsize": Config.maxSize, - }, w) + }, r, w) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } func pasteHandler(c web.C, w http.ResponseWriter, r *http.Request) { - err := Templates["paste.html"].ExecuteWriter(pongo2.Context{}, w) + err := renderTemplate(Templates["paste.html"], pongo2.Context{}, r, w) if err != nil { oopsHandler(c, w, r, RespHTML, "") } } func apiDocHandler(c web.C, w http.ResponseWriter, r *http.Request) { - err := Templates["API.html"].ExecuteWriter(pongo2.Context{"siteurl": getSiteURL(r)}, w) + err := renderTemplate(Templates["API.html"], pongo2.Context{ + "siteurl": getSiteURL(r), + }, r, w) if err != nil { oopsHandler(c, w, r, RespHTML, "") } @@ -44,7 +46,7 @@ func apiDocHandler(c web.C, w http.ResponseWriter, r *http.Request) { func notFoundHandler(c web.C, w http.ResponseWriter, r *http.Request) { w.WriteHeader(404) - err := Templates["404.html"].ExecuteWriter(pongo2.Context{}, w) + err := renderTemplate(Templates["404.html"], pongo2.Context{}, r, w) if err != nil { oopsHandler(c, w, r, RespHTML, "") } @@ -57,7 +59,7 @@ func oopsHandler(c web.C, w http.ResponseWriter, r *http.Request, rt RespType, m if rt == RespHTML { w.WriteHeader(500) - Templates["oops.html"].ExecuteWriter(pongo2.Context{"msg": msg}, w) + renderTemplate(Templates["oops.html"], pongo2.Context{"msg": msg}, r, w) return } else if rt == RespPLAIN { @@ -86,7 +88,7 @@ func oopsHandler(c web.C, w http.ResponseWriter, r *http.Request, rt RespType, m func badRequestHandler(c web.C, w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusBadRequest) - err := Templates["400.html"].ExecuteWriter(pongo2.Context{}, w) + err := renderTemplate(Templates["400.html"], pongo2.Context{}, r, w) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } @@ -94,7 +96,7 @@ func badRequestHandler(c web.C, w http.ResponseWriter, r *http.Request) { func unauthorizedHandler(c web.C, w http.ResponseWriter, r *http.Request) { w.WriteHeader(401) - err := Templates["401.html"].ExecuteWriter(pongo2.Context{}, w) + err := renderTemplate(Templates["401.html"], pongo2.Context{}, r, w) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } diff --git a/server.go b/server.go index 2d2e3d4..d43f941 100644 --- a/server.go +++ b/server.go @@ -131,9 +131,6 @@ func setup() *web.Mux { log.Fatal("Error: could not load templates", err) } TemplateSet := pongo2.NewSet("templates", p2l) - TemplateSet.Globals["sitename"] = Config.siteName - TemplateSet.Globals["sitepath"] = Config.sitePath - TemplateSet.Globals["using_auth"] = Config.authFile != "" err = populateTemplatesMap(TemplateSet, Templates) if err != nil { log.Fatal("Error: could not load templates", err) @@ -201,7 +198,7 @@ func main() { "remove stdout output for each request") flag.BoolVar(&Config.allowHotlink, "allowhotlink", false, "Allow hotlinking of files") - flag.StringVar(&Config.siteName, "sitename", "linx", + flag.StringVar(&Config.siteName, "sitename", "", "name of the site") flag.StringVar(&Config.siteURL, "siteurl", "", "site base url (including trailing slash)") diff --git a/templates.go b/templates.go index aa4652a..04b8436 100644 --- a/templates.go +++ b/templates.go @@ -3,8 +3,10 @@ package main import ( "bytes" "io" + "net/http" "path" "path/filepath" + "strings" "github.com/GeertJohan/go.rice" "github.com/flosch/pongo2" @@ -71,3 +73,15 @@ func populateTemplatesMap(tSet *pongo2.TemplateSet, tMap map[string]*pongo2.Temp return nil } + +func renderTemplate(tpl *pongo2.Template, context pongo2.Context, r *http.Request, writer io.Writer) error { + if Config.siteName == "" { + parts := strings.Split(r.Host, ":") + context["sitename"] = parts[0] + } + + context["sitepath"] = Config.sitePath + context["using_auth"] = Config.authFile != "" + + return tpl.ExecuteWriter(context, writer) +} diff --git a/templates/API.html b/templates/API.html index aab73b1..045f4c2 100644 --- a/templates/API.html +++ b/templates/API.html @@ -78,7 +78,7 @@ {"delete_key":"...","expiry":"0","filename":"f34h4iu.jpg","mimetype":"image/jpeg", "sha256sum":"...","size":"...","url":"{{ siteurl }}f34h4iu.jpg"} {% else %} -
$ curl -H "Accept: application/json" -H "Linx-Randomize: yes" -T myphoto.jpg {{ siteurl }}/upload/  
+			
$ curl -H "Accept: application/json" -H "Linx-Randomize: yes" -T myphoto.jpg {{ siteurl }}upload/  
 {"delete_key":"...","expiry":"0","filename":"f34h4iu.jpg","mimetype":"image/jpeg",
 "sha256sum":"...","size":"...","url":"{{ siteurl }}f34h4iu.jpg"}
{% endif %} @@ -130,7 +130,7 @@ DELETED

Example

-
$ curl -H "Accept: application/json" {{ siteurl }}/myphoto.jpg
+			
$ curl -H "Accept: application/json" {{ siteurl }}myphoto.jpg
 {"expiry":"0","filename":"myphoto.jpg","mimetype":"image/jpeg","sha256sum":"...","size":"..."}