From d98b63e8bd385ff8418e052e5ab8de0d9e7f2a6c Mon Sep 17 00:00:00 2001 From: andreimarcu Date: Fri, 25 Sep 2015 12:00:14 -0400 Subject: [PATCH] Performance improvements, custom 404+500, -nologs, PUT uploads fix --- display.go | 18 +++++++++++------- fileserve.go | 2 +- pages.go | 21 +++++++++++++++++++-- server.go | 14 ++++++++++++-- static/images/404.jpg | Bin 0 -> 18323 bytes templates/404.html | 5 +++++ templates/display/file.html | 4 ++-- templates/oops.html | 10 ++++++++++ upload.go | 11 +++++------ 9 files changed, 65 insertions(+), 20 deletions(-) create mode 100644 static/images/404.jpg create mode 100644 templates/404.html create mode 100644 templates/oops.html diff --git a/display.go b/display.go index b26a89a..8bd69ac 100644 --- a/display.go +++ b/display.go @@ -11,36 +11,40 @@ import ( "github.com/zenazn/goji/web" ) +var imageTpl = pongo2.Must(pongo2.FromCache("templates/display/image.html")) +var videoTpl = pongo2.Must(pongo2.FromCache("templates/display/video.html")) +var fileTpl = pongo2.Must(pongo2.FromCache("templates/display/file.html")) + func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) { fileName := c.URLParams["name"] filePath := path.Join(Config.filesDir, fileName) fileInfo, err := os.Stat(filePath) if os.IsNotExist(err) { - http.Error(w, http.StatusText(404), 404) + notFoundHandler(c, w, r) return } if err := magicmime.Open(magicmime.MAGIC_MIME_TYPE | magicmime.MAGIC_SYMLINK | magicmime.MAGIC_ERROR); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + oopsHandler(c, w, r) } defer magicmime.Close() mimetype, err := magicmime.TypeByFile(filePath) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + oopsHandler(c, w, r) } var tpl *pongo2.Template if strings.HasPrefix(mimetype, "image/") { - tpl = pongo2.Must(pongo2.FromCache("templates/display/image.html")) + tpl = imageTpl } else if strings.HasPrefix(mimetype, "video/") { - tpl = pongo2.Must(pongo2.FromCache("templates/display/video.html")) + tpl = videoTpl } else { - tpl = pongo2.Must(pongo2.FromCache("templates/display/file.html")) + tpl = fileTpl } err = tpl.ExecuteWriter(pongo2.Context{ @@ -50,6 +54,6 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) { }, w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + oopsHandler(c, w, r) } } diff --git a/fileserve.go b/fileserve.go index b34020c..ba99455 100644 --- a/fileserve.go +++ b/fileserve.go @@ -14,7 +14,7 @@ func fileServeHandler(c web.C, w http.ResponseWriter, r *http.Request) { _, err := os.Stat(filePath) if os.IsNotExist(err) { - http.Error(w, http.StatusText(404), 404) + notFoundHandler(c, w, r) return } diff --git a/pages.go b/pages.go index bf222c7..9808e81 100644 --- a/pages.go +++ b/pages.go @@ -7,11 +7,28 @@ import ( "github.com/zenazn/goji/web" ) -func indexHandler(c web.C, w http.ResponseWriter, r *http.Request) { - indexTpl := pongo2.Must(pongo2.FromCache("templates/index.html")) +var indexTpl = pongo2.Must(pongo2.FromCache("templates/index.html")) +var notFoundTpl = pongo2.Must(pongo2.FromCache("templates/404.html")) +var oopsTpl = pongo2.Must(pongo2.FromCache("templates/oops.html")) +func indexHandler(c web.C, w http.ResponseWriter, r *http.Request) { err := indexTpl.ExecuteWriter(pongo2.Context{}, w) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } + +func notFoundHandler(c web.C, w http.ResponseWriter, r *http.Request) { + w.WriteHeader(404) + err := notFoundTpl.ExecuteWriter(pongo2.Context{}, w) + if err != nil { + oopsHandler(c, w, r) + } +} + +func oopsHandler(c web.C, w http.ResponseWriter, r *http.Request) { + err := oopsTpl.ExecuteWriter(pongo2.Context{}, w) + if err != nil { + oopsHandler(c, w, r) + } +} diff --git a/server.go b/server.go index 0d0ea39..9d179a6 100644 --- a/server.go +++ b/server.go @@ -9,11 +9,13 @@ import ( "github.com/flosch/pongo2" "github.com/zenazn/goji" + "github.com/zenazn/goji/web/middleware" ) var Config struct { bind string filesDir string + noLogs bool siteName string siteURL string } @@ -23,14 +25,17 @@ func main() { "host to bind to (default: 127.0.0.1:8080)") flag.StringVar(&Config.filesDir, "filespath", "files/", "path to files directory (default: files/)") + flag.BoolVar(&Config.noLogs, "nologs", false, + "remove stdout output for each request") flag.StringVar(&Config.siteName, "sitename", "linx", "name of the site") flag.StringVar(&Config.siteURL, "siteurl", "http://"+Config.bind+"/", "site base url (including trailing slash)") flag.Parse() - // Disable template caching -- keep until out of pre-alpha - pongo2.DefaultSet.Debug = true // will keep this until out of pre-alpha + if Config.noLogs { + goji.Abandon(middleware.Logger) + } // Template Globals pongo2.DefaultSet.Globals["sitename"] = Config.siteName @@ -40,12 +45,17 @@ func main() { selifRe := regexp.MustCompile(`^/selif/(?P[a-z0-9-\.]+)$`) goji.Get("/", indexHandler) + goji.Post("/upload", uploadPostHandler) + goji.Post("/upload/", http.RedirectHandler("/upload", 301)) goji.Put("/upload", uploadPutHandler) + goji.Put("/upload/:name", uploadPutHandler) + goji.Get("/static/*", http.StripPrefix("/static/", http.FileServer(http.Dir("static/")))) goji.Get(nameRe, fileDisplayHandler) goji.Get(selifRe, fileServeHandler) + goji.NotFound(notFoundHandler) listener, err := net.Listen("tcp", Config.bind) if err != nil { diff --git a/static/images/404.jpg b/static/images/404.jpg new file mode 100644 index 0000000000000000000000000000000000000000..16ed400d7844ca4485db66802be08e112162f184 GIT binary patch literal 18323 zcmeFYXHZj7*e)6bjDmoTB7}efA|N0rJpmCR(nNX(kzPXY9h43Mg7l{JUPB8Vl+Zhb z7K-%V37y0DoqO*2=FFWr_x`*y=iQSvYtLSLt#?-T-s^dv_gOa+H}impGE&k~00II6 zz^B^-aB~e1kx-HpS5Z(BW;8Y8w6HZcWpw7@dc*k2$-&&(lu=p+#whbe62|Dv&&&3j z(ay=h%G}Y75y~jUM{+X_kO17d?TdimKgv7*VMK)gQ4$jo5fa@czI*pSf57|q?gH-v z@7}#fdhb5Tf7os90V&CY|2X-NPX7C?cZdlIiAjKWf&ba$|5I_(0(f+fK!@Onkl+R2 z&LaZCM+7&20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y;c3YI>w9D}Q7ul5+ew8Rwyx zx;>&}e>C~p{?p;fN69FQwik?IFGt=_%C+4EMq~15g%%HWEte4k$SI50n#37N=+J_< z^ztl5WJop*uc?*VhYKgqT|x-EKD&WQa;Ql5Axnk0&sV9N2my4rSG!fZBbJ^IrK10$ zR>$s>8hgowxd%Ihujgv|hRF9{=)8?v)al!sG_wt-j8?51Tj@v90zGZG2 z(Toi4ztUvrd~G@t*JrvYYW$^7zVz5KRF1^!b1yPY;lK3B{W0`wJyMekT!#*)0V!fw zjx`x=CUL7fxoOKs?Sf;_mvxh3nlV*A!m2v2P7s|>*CN+uhIk4sy848l+n;Ja#yH>FA7yUk|UHBnFbFUk6 zo>)zrlWAbHGFTiAv28p%mR{!DSu(1Jw>YuNI*NcsuJlF~ep#2c$+ zrYywuqen1lY~_^^#iT(rk_J}QdY6w*<0`y_bV3Fp83>%zXz{!`aJUoL%K9G4H?ud#VwUqz5X zRxeG<*fX7}t=i9D5v!sqoFNmh@gi|HGj&GR>;2A97lfdNfw3kw2V+{U2!mHlv8|gg z*jRlT`7qA&eT{Yyzs^grm8$3(&6B^06Xwsw=pCyyU*<6U+{1VH zoX1ZHSV{v?n%#-}L+dFUj%XX1FH2L+~D#xD7=aqLa zna0g)E%W%Rit(gPv$CwRETa9rePw(S9qkI0EnVH35h6jv?K0T-vsE|>5=`ZpODR~PwzCT3LPJFao c`vrEuUHk>MKSmGxAiDX86#7GgS^xhg0G4d3+W-In literal 0 HcmV?d00001 diff --git a/templates/404.html b/templates/404.html new file mode 100644 index 0000000..0999393 --- /dev/null +++ b/templates/404.html @@ -0,0 +1,5 @@ +{% extends "base.html" %} + +{% block content %} + +{% endblock %} diff --git a/templates/display/file.html b/templates/display/file.html index d323fa6..9872298 100644 --- a/templates/display/file.html +++ b/templates/display/file.html @@ -1,7 +1,7 @@ {% extends "base.html" %} {% block main %} -
-

You are requesting {{ filename }}, >click here to download.

+
+

You are requesting {{ filename }}, click here to download.

{% endblock %} diff --git a/templates/oops.html b/templates/oops.html new file mode 100644 index 0000000..9627bba --- /dev/null +++ b/templates/oops.html @@ -0,0 +1,10 @@ +{% extends "base.html" %} + +{% block content %} +
+
+

{{ error_message|default:"Oops! Something went wrong." }}

+
+
+{% endblock %} + diff --git a/upload.go b/upload.go index f75220d..6cd9582 100644 --- a/upload.go +++ b/upload.go @@ -2,7 +2,6 @@ package main import ( "encoding/json" - "errors" "fmt" "io" "net/http" @@ -39,6 +38,7 @@ func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) { } else { file, headers, err := r.FormFile("file") if err != nil { + oopsHandler(c, w, r) return } defer file.Close() @@ -49,7 +49,7 @@ func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) { upload, err := processUpload(upReq) if err != nil { - fmt.Fprintf(w, "Failed to upload: %v", err) + oopsHandler(c, w, r) return } @@ -72,15 +72,16 @@ func uploadPutHandler(c web.C, w http.ResponseWriter, r *http.Request) { upReq := UploadRequest{} defer r.Body.Close() + upReq.filename = c.URLParams["name"] upReq.src = r.Body upload, err := processUpload(upReq) if err != nil { - fmt.Fprintf(w, "Failed to upload") + oopsHandler(c, w, r) return } - fmt.Fprintf(w, "File %s uploaded successfully.", upload.Filename) + fmt.Fprintf(w, Config.siteURL+upload.Filename) } func processUpload(upReq UploadRequest) (upload Upload, err error) { @@ -106,12 +107,10 @@ func processUpload(upReq UploadRequest) (upload Upload, err error) { if err != nil { return } else if bytes == 0 { - err = errors.New("Empty file") return } upload.Size = bytes - return }