2015-09-24 07:44:49 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-10-04 18:47:20 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-09-24 07:44:49 +02:00
|
|
|
"net/http"
|
2015-10-04 18:47:20 +02:00
|
|
|
"strings"
|
2015-09-24 07:44:49 +02:00
|
|
|
|
2015-09-24 08:08:44 +02:00
|
|
|
"github.com/flosch/pongo2"
|
2015-09-24 07:44:49 +02:00
|
|
|
"github.com/zenazn/goji/web"
|
|
|
|
)
|
|
|
|
|
2015-10-04 18:47:20 +02:00
|
|
|
type RespType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
RespPLAIN RespType = iota
|
|
|
|
RespJSON
|
|
|
|
RespHTML
|
|
|
|
RespAUTO
|
|
|
|
)
|
|
|
|
|
2015-09-25 18:00:14 +02:00
|
|
|
func indexHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
2016-06-15 08:21:39 +02:00
|
|
|
err := renderTemplate(Templates["index.html"], pongo2.Context{
|
2019-01-26 11:04:32 +01:00
|
|
|
"maxsize": Config.maxSize,
|
|
|
|
"expirylist": listExpirationTimes(),
|
|
|
|
"forcerandom": Config.forceRandomFilename,
|
2016-06-15 08:21:39 +02:00
|
|
|
}, r, w)
|
2015-09-24 07:44:49 +02:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 18:00:14 +02:00
|
|
|
|
2015-09-30 06:56:51 +02:00
|
|
|
func pasteHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
2016-09-19 06:45:00 +02:00
|
|
|
err := renderTemplate(Templates["paste.html"], pongo2.Context{
|
2019-01-26 11:04:32 +01:00
|
|
|
"expirylist": listExpirationTimes(),
|
|
|
|
"forcerandom": Config.forceRandomFilename,
|
2016-09-19 06:45:00 +02:00
|
|
|
}, r, w)
|
2015-09-30 06:56:51 +02:00
|
|
|
if err != nil {
|
2015-10-04 18:47:20 +02:00
|
|
|
oopsHandler(c, w, r, RespHTML, "")
|
2015-09-30 06:56:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-09 03:48:06 +02:00
|
|
|
func apiDocHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
2016-06-15 08:21:39 +02:00
|
|
|
err := renderTemplate(Templates["API.html"], pongo2.Context{
|
2019-01-26 11:04:32 +01:00
|
|
|
"siteurl": getSiteURL(r),
|
|
|
|
"forcerandom": Config.forceRandomFilename,
|
2016-06-15 08:21:39 +02:00
|
|
|
}, r, w)
|
2015-10-09 03:48:06 +02:00
|
|
|
if err != nil {
|
|
|
|
oopsHandler(c, w, r, RespHTML, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 21:32:35 +01:00
|
|
|
func makeCustomPageHandler(fileName string) func(c web.C, w http.ResponseWriter, r *http.Request) {
|
|
|
|
return func(c web.C, w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := renderTemplate(Templates["custom_page.html"], pongo2.Context{
|
|
|
|
"siteurl": getSiteURL(r),
|
|
|
|
"forcerandom": Config.forceRandomFilename,
|
|
|
|
"contents": customPages[fileName],
|
|
|
|
"filename": fileName,
|
|
|
|
"pagename": customPagesNames[fileName],
|
|
|
|
}, r, w)
|
|
|
|
if err != nil {
|
|
|
|
oopsHandler(c, w, r, RespHTML, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 18:00:14 +02:00
|
|
|
func notFoundHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(404)
|
2016-06-15 08:21:39 +02:00
|
|
|
err := renderTemplate(Templates["404.html"], pongo2.Context{}, r, w)
|
2015-09-25 18:00:14 +02:00
|
|
|
if err != nil {
|
2015-10-04 18:47:20 +02:00
|
|
|
oopsHandler(c, w, r, RespHTML, "")
|
2015-09-25 18:00:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-04 18:47:20 +02:00
|
|
|
func oopsHandler(c web.C, w http.ResponseWriter, r *http.Request, rt RespType, msg string) {
|
|
|
|
if msg == "" {
|
|
|
|
msg = "Oops! Something went wrong..."
|
|
|
|
}
|
|
|
|
|
|
|
|
if rt == RespHTML {
|
|
|
|
w.WriteHeader(500)
|
2016-06-15 08:21:39 +02:00
|
|
|
renderTemplate(Templates["oops.html"], pongo2.Context{"msg": msg}, r, w)
|
2015-10-04 18:47:20 +02:00
|
|
|
return
|
|
|
|
} else if rt == RespPLAIN {
|
|
|
|
w.WriteHeader(500)
|
2015-10-07 09:00:03 +02:00
|
|
|
fmt.Fprintf(w, "%s", msg)
|
2015-10-04 18:47:20 +02:00
|
|
|
return
|
|
|
|
} else if rt == RespJSON {
|
|
|
|
js, _ := json.Marshal(map[string]string{
|
|
|
|
"error": msg,
|
|
|
|
})
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write(js)
|
|
|
|
return
|
|
|
|
} else if rt == RespAUTO {
|
|
|
|
if strings.EqualFold("application/json", r.Header.Get("Accept")) {
|
|
|
|
oopsHandler(c, w, r, RespJSON, msg)
|
|
|
|
} else {
|
|
|
|
oopsHandler(c, w, r, RespHTML, msg)
|
|
|
|
}
|
2015-09-25 18:00:14 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-29 05:46:43 +02:00
|
|
|
|
2019-01-25 08:33:11 +01:00
|
|
|
func badRequestHandler(c web.C, w http.ResponseWriter, r *http.Request, rt RespType, msg string) {
|
|
|
|
if rt == RespHTML {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
err := renderTemplate(Templates["400.html"], pongo2.Context{"msg": msg}, r, w)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
} else if rt == RespPLAIN {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
fmt.Fprintf(w, "%s", msg)
|
|
|
|
return
|
|
|
|
} else if rt == RespJSON {
|
|
|
|
js, _ := json.Marshal(map[string]string{
|
|
|
|
"error": msg,
|
|
|
|
})
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write(js)
|
|
|
|
return
|
|
|
|
} else if rt == RespAUTO {
|
|
|
|
if strings.EqualFold("application/json", r.Header.Get("Accept")) {
|
|
|
|
badRequestHandler(c, w, r, RespJSON, msg)
|
|
|
|
} else {
|
|
|
|
badRequestHandler(c, w, r, RespHTML, msg)
|
|
|
|
}
|
2015-10-12 10:03:02 +02:00
|
|
|
}
|
2015-10-09 05:27:04 +02:00
|
|
|
}
|
|
|
|
|
2015-09-29 05:46:43 +02:00
|
|
|
func unauthorizedHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(401)
|
2016-06-15 08:21:39 +02:00
|
|
|
err := renderTemplate(Templates["401.html"], pongo2.Context{}, r, w)
|
2015-09-29 05:46:43 +02:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|