rushlink/router.go
2020-05-30 17:49:44 +02:00

133 lines
4.0 KiB
Go

package rushlink
import (
"fmt"
"log"
"net/http"
"net/url"
"runtime/debug"
"strconv"
"time"
"gitea.hashru.nl/dsprenkels/rushlink/internal/db"
"github.com/gorilla/mux"
"github.com/pkg/errors"
)
const staticFilenameExpr = "[A-Za-z0-9-_.]+"
const urlKeyExpr = "{key:[A-Za-z0-9-_]{4,}}"
const urlKeyWithExtExpr = urlKeyExpr + "{ext:\\.[A-Za-z0-9-_]+}"
type rushlink struct {
db *db.Database
fs *db.FileStore
rootURL *url.URL
}
func (rl *rushlink) RootURL() *url.URL {
return rl.rootURL
}
func (rl *rushlink) recoveryMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
defer func() {
if err := recover(); err != nil {
w.WriteHeader(500)
log.Printf("error: panic while recovering from another panic: %v\n", err)
debug.PrintStack()
fmt.Fprintf(w, "internal server error: %v\n", err)
}
}()
if err := recover(); err != nil {
w.WriteHeader(500)
log.Printf("error: %v\n", err)
debug.PrintStack()
rl.renderInternalServerError(w, r, err)
}
}()
next.ServeHTTP(w, r)
})
}
func (rl *rushlink) metricsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
srw := statusResponseWriter{Inner: w}
next.ServeHTTP(&srw, r)
status := strconv.Itoa(srw.StatusCode)
metricRequestsTotalCounter.WithLabelValues(status, r.Method).Inc()
})
}
type statusResponseWriter struct {
Inner http.ResponseWriter
StatusCode int
}
func (w *statusResponseWriter) Header() http.Header {
return w.Inner.Header()
}
func (w *statusResponseWriter) Write(buf []byte) (int, error) {
if w.StatusCode == 0 {
w.WriteHeader(http.StatusOK)
}
return w.Inner.Write(buf)
}
func (w *statusResponseWriter) WriteHeader(statusCode int) {
w.StatusCode = statusCode
w.Inner.WriteHeader(statusCode)
}
// CreateMainRouter creates the main Gorilla router for the application.
func CreateMainRouter(rl *rushlink, debug bool) *mux.Router {
router := mux.NewRouter()
if !debug {
router.Use(rl.recoveryMiddleware)
router.Use(rl.metricsMiddleware)
}
router.HandleFunc("/{path:img/"+staticFilenameExpr+"}", rl.staticGetHandler).Methods("GET", "HEAD")
router.HandleFunc("/{path:css/"+staticFilenameExpr+"}", rl.staticGetHandler).Methods("GET", "HEAD")
router.HandleFunc("/{path:js/"+staticFilenameExpr+"}", rl.staticGetHandler).Methods("GET", "HEAD")
router.HandleFunc("/", rl.indexGetHandler).Methods("GET", "HEAD")
router.HandleFunc("/", rl.newPasteHandler).Methods("POST")
router.HandleFunc("/"+urlKeyExpr, rl.viewPasteHandler).Methods("GET", "HEAD")
router.HandleFunc("/"+urlKeyWithExtExpr, rl.viewPasteHandler).Methods("GET", "HEAD")
router.HandleFunc("/"+urlKeyExpr+"/nr", rl.viewPasteHandlerNoRedirect).Methods("GET", "HEAD")
router.HandleFunc("/"+urlKeyWithExtExpr+"/nr", rl.viewPasteHandlerNoRedirect).Methods("GET", "HEAD")
router.HandleFunc("/"+urlKeyExpr+"/meta", rl.viewPasteHandlerMeta).Methods("GET", "HEAD")
router.HandleFunc("/"+urlKeyWithExtExpr+"/meta", rl.viewPasteHandlerMeta).Methods("GET", "HEAD")
router.HandleFunc("/"+urlKeyExpr, rl.deletePasteHandler).Methods("DELETE")
router.HandleFunc("/"+urlKeyWithExtExpr, rl.deletePasteHandler).Methods("DELETE")
router.HandleFunc("/"+urlKeyExpr+"/delete", rl.deletePasteHandler).Methods("POST")
router.HandleFunc("/"+urlKeyWithExtExpr+"/delete", rl.deletePasteHandler).Methods("POST")
return router
}
// StartMainServer starts the main http server listening on addr.
func StartMainServer(addr string, db *db.Database, fs *db.FileStore, rawRootURL string) {
var rootURL *url.URL
if rawRootURL != "" {
var err error
rootURL, err = url.Parse(rawRootURL)
if err != nil {
log.Fatalln(errors.Wrap(err, "could not parse rootURL flag"))
}
}
rl := rushlink{
db: db,
fs: fs,
rootURL: rootURL,
}
srv := &http.Server{
Handler: CreateMainRouter(&rl, false),
Addr: addr,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}