Browse Source

Infer sitename from Host if empty

pull/87/head
andreimarcu 9 years ago
parent
commit
47a1aa6396
  1. 4
      display.go
  2. 18
      pages.go
  3. 5
      server.go
  4. 14
      templates.go
  5. 4
      templates/API.html

4
display.go

@ -112,7 +112,7 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
tpl = Templates["display/file.html"] tpl = Templates["display/file.html"]
} }
err = tpl.ExecuteWriter(pongo2.Context{
err = renderTemplate(tpl, pongo2.Context{
"mime": metadata.Mimetype, "mime": metadata.Mimetype,
"filename": fileName, "filename": fileName,
"size": sizeHuman, "size": sizeHuman,
@ -120,7 +120,7 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
"extra": extra, "extra": extra,
"lines": lines, "lines": lines,
"files": metadata.ArchiveFiles, "files": metadata.ArchiveFiles,
}, w)
}, r, w)
if err != nil { if err != nil {
oopsHandler(c, w, r, RespHTML, "") oopsHandler(c, w, r, RespHTML, "")

18
pages.go

@ -20,23 +20,25 @@ const (
) )
func indexHandler(c web.C, w http.ResponseWriter, r *http.Request) { 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, "maxsize": Config.maxSize,
}, w)
}, r, w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
} }
} }
func pasteHandler(c web.C, w http.ResponseWriter, r *http.Request) { 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 { if err != nil {
oopsHandler(c, w, r, RespHTML, "") oopsHandler(c, w, r, RespHTML, "")
} }
} }
func apiDocHandler(c web.C, w http.ResponseWriter, r *http.Request) { 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 { if err != nil {
oopsHandler(c, w, r, RespHTML, "") 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) { func notFoundHandler(c web.C, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404) w.WriteHeader(404)
err := Templates["404.html"].ExecuteWriter(pongo2.Context{}, w)
err := renderTemplate(Templates["404.html"], pongo2.Context{}, r, w)
if err != nil { if err != nil {
oopsHandler(c, w, r, RespHTML, "") 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 { if rt == RespHTML {
w.WriteHeader(500) w.WriteHeader(500)
Templates["oops.html"].ExecuteWriter(pongo2.Context{"msg": msg}, w)
renderTemplate(Templates["oops.html"], pongo2.Context{"msg": msg}, r, w)
return return
} else if rt == RespPLAIN { } 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) { func badRequestHandler(c web.C, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
err := Templates["400.html"].ExecuteWriter(pongo2.Context{}, w)
err := renderTemplate(Templates["400.html"], pongo2.Context{}, r, w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) 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) { func unauthorizedHandler(c web.C, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(401) w.WriteHeader(401)
err := Templates["401.html"].ExecuteWriter(pongo2.Context{}, w)
err := renderTemplate(Templates["401.html"], pongo2.Context{}, r, w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
} }

5
server.go

@ -131,9 +131,6 @@ func setup() *web.Mux {
log.Fatal("Error: could not load templates", err) log.Fatal("Error: could not load templates", err)
} }
TemplateSet := pongo2.NewSet("templates", p2l) 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) err = populateTemplatesMap(TemplateSet, Templates)
if err != nil { if err != nil {
log.Fatal("Error: could not load templates", err) log.Fatal("Error: could not load templates", err)
@ -201,7 +198,7 @@ func main() {
"remove stdout output for each request") "remove stdout output for each request")
flag.BoolVar(&Config.allowHotlink, "allowhotlink", false, flag.BoolVar(&Config.allowHotlink, "allowhotlink", false,
"Allow hotlinking of files") "Allow hotlinking of files")
flag.StringVar(&Config.siteName, "sitename", "linx",
flag.StringVar(&Config.siteName, "sitename", "",
"name of the site") "name of the site")
flag.StringVar(&Config.siteURL, "siteurl", "", flag.StringVar(&Config.siteURL, "siteurl", "",
"site base url (including trailing slash)") "site base url (including trailing slash)")

14
templates.go

@ -3,8 +3,10 @@ package main
import ( import (
"bytes" "bytes"
"io" "io"
"net/http"
"path" "path"
"path/filepath" "path/filepath"
"strings"
"github.com/GeertJohan/go.rice" "github.com/GeertJohan/go.rice"
"github.com/flosch/pongo2" "github.com/flosch/pongo2"
@ -71,3 +73,15 @@ func populateTemplatesMap(tSet *pongo2.TemplateSet, tMap map[string]*pongo2.Temp
return nil 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)
}

4
templates/API.html

@ -78,7 +78,7 @@
{"delete_key":"...","expiry":"0","filename":"f34h4iu.jpg","mimetype":"image/jpeg", {"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> &#34;sha256sum&#34;:&#34;...&#34;,&#34;size&#34;:&#34;...&#34;,&#34;url&#34;:&#34;{{ siteurl }}f34h4iu.jpg&#34;}</code></pre>
{% else %} {% 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;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> &#34;sha256sum&#34;:&#34;...&#34;,&#34;size&#34;:&#34;...&#34;,&#34;url&#34;:&#34;{{ siteurl }}f34h4iu.jpg&#34;}</code></pre>
{% endif %} {% endif %}
@ -130,7 +130,7 @@ DELETED</code></pre>
<p><strong>Example</strong></p> <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> {&#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>
</div> </div>

Loading…
Cancel
Save