Infer sitename from Host if empty
This commit is contained in:
parent
9c6088cfe5
commit
47a1aa6396
@ -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, "")
|
||||
|
18
pages.go
18
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)
|
||||
}
|
||||
|
@ -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)")
|
||||
|
14
templates.go
14
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)
|
||||
}
|
||||
|
@ -78,7 +78,7 @@
|
||||
{"delete_key":"...","expiry":"0","filename":"f34h4iu.jpg","mimetype":"image/jpeg",
|
||||
"sha256sum":"...","size":"...","url":"{{ siteurl }}f34h4iu.jpg"}</code></pre>
|
||||
{% else %}
|
||||
<pre><code>$ curl -H "Accept: application/json" -H "Linx-Randomize: yes" -T myphoto.jpg {{ siteurl }}/upload/
|
||||
<pre><code>$ 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"}</code></pre>
|
||||
{% endif %}
|
||||
@ -130,7 +130,7 @@ DELETED</code></pre>
|
||||
|
||||
<p><strong>Example</strong></p>
|
||||
|
||||
<pre><code>$ curl -H "Accept: application/json" {{ siteurl }}/myphoto.jpg
|
||||
<pre><code>$ curl -H "Accept: application/json" {{ siteurl }}myphoto.jpg
|
||||
{"expiry":"0","filename":"myphoto.jpg","mimetype":"image/jpeg","sha256sum":"...","size":"..."}</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
|
Loading…
Reference in New Issue
Block a user