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 0000000..16ed400 Binary files /dev/null and b/static/images/404.jpg differ 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 }