Infer sitename from Host if empty

This commit is contained in:
andreimarcu 2016-06-14 23:21:39 -07:00
parent 9c6088cfe5
commit 47a1aa6396
5 changed files with 29 additions and 16 deletions

View File

@ -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, "")

View File

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

View File

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

View File

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

View File

@ -78,7 +78,7 @@
{"delete_key":"...","expiry":"0","filename":"f34h4iu.jpg","mimetype":"image/jpeg",
&#34;sha256sum&#34;:&#34;...&#34;,&#34;size&#34;:&#34;...&#34;,&#34;url&#34;:&#34;{{ siteurl }}f34h4iu.jpg&#34;}</code></pre>
{% else %}
<pre><code>$ curl -H &#34;Accept: application/json&#34; -H &#34;Linx-Randomize: yes&#34; -T myphoto.jpg {{ siteurl }}/upload/
<pre><code>$ curl -H &#34;Accept: application/json&#34; -H &#34;Linx-Randomize: yes&#34; -T myphoto.jpg {{ siteurl }}upload/
{&#34;delete_key&#34;:&#34;...&#34;,&#34;expiry&#34;:&#34;0&#34;,&#34;filename&#34;:&#34;f34h4iu.jpg&#34;,&#34;mimetype&#34;:&#34;image/jpeg&#34;,
&#34;sha256sum&#34;:&#34;...&#34;,&#34;size&#34;:&#34;...&#34;,&#34;url&#34;:&#34;{{ siteurl }}f34h4iu.jpg&#34;}</code></pre>
{% endif %}
@ -130,7 +130,7 @@ DELETED</code></pre>
<p><strong>Example</strong></p>
<pre><code>$ curl -H &#34;Accept: application/json&#34; {{ siteurl }}/myphoto.jpg
<pre><code>$ curl -H &#34;Accept: application/json&#34; {{ siteurl }}myphoto.jpg
{&#34;expiry&#34;:&#34;0&#34;,&#34;filename&#34;:&#34;myphoto.jpg&#34;,&#34;mimetype&#34;:&#34;image/jpeg&#34;,&#34;sha256sum&#34;:&#34;...&#34;,&#34;size&#34;:&#34;...&#34;}</code></pre>
</div>
</div>