2019-11-09 15:50:12 +01:00
|
|
|
package rushlink
|
2019-09-19 21:29:25 +02:00
|
|
|
|
|
|
|
import (
|
2019-10-15 23:19:45 +02:00
|
|
|
"fmt"
|
2019-09-19 21:29:25 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2019-12-15 12:18:50 +01:00
|
|
|
"net/url"
|
2019-11-10 19:03:57 +01:00
|
|
|
"runtime/debug"
|
2019-12-08 21:56:02 +01:00
|
|
|
"strconv"
|
2019-09-19 21:29:25 +02:00
|
|
|
"time"
|
|
|
|
|
2019-12-03 23:08:58 +01:00
|
|
|
"gitea.hashru.nl/dsprenkels/rushlink/internal/db"
|
2019-09-19 21:29:25 +02:00
|
|
|
"github.com/gorilla/mux"
|
2019-12-15 12:18:50 +01:00
|
|
|
"github.com/pkg/errors"
|
2019-09-19 21:29:25 +02:00
|
|
|
)
|
|
|
|
|
2019-12-16 11:51:41 +01:00
|
|
|
const staticFilenameExpr = "[A-Za-z0-9-_.]+"
|
2019-12-15 07:49:32 +01:00
|
|
|
const urlKeyExpr = "{key:[A-Za-z0-9-_]{4,}}"
|
|
|
|
const urlKeyWithExtExpr = urlKeyExpr + "{ext:\\.[A-Za-z0-9-_]+}"
|
|
|
|
|
2019-12-03 23:08:58 +01:00
|
|
|
type rushlink struct {
|
2019-12-17 11:13:32 +01:00
|
|
|
db *db.Database
|
|
|
|
fs *db.FileStore
|
|
|
|
rootURL *url.URL
|
2019-12-03 23:08:58 +01:00
|
|
|
}
|
|
|
|
|
2019-12-17 11:13:32 +01:00
|
|
|
func (rl *rushlink) RootURL() *url.URL {
|
|
|
|
return rl.rootURL
|
2019-12-15 12:18:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rl *rushlink) recoveryMiddleware(next http.Handler) http.Handler {
|
2019-10-15 23:19:45 +02:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-05-30 17:36:18 +02:00
|
|
|
logRequestInfo := func() {
|
|
|
|
log.Printf("in request: %v - %v %q %v", r.RemoteAddr, r.Method, r.RequestURI, r.Proto)
|
|
|
|
}
|
2019-10-15 23:19:45 +02:00
|
|
|
defer func() {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
2020-05-30 17:49:44 +02:00
|
|
|
w.WriteHeader(500)
|
2019-10-15 23:19:45 +02:00
|
|
|
log.Printf("error: panic while recovering from another panic: %v\n", err)
|
2020-05-30 17:36:18 +02:00
|
|
|
logRequestInfo()
|
2019-11-10 19:03:57 +01:00
|
|
|
debug.PrintStack()
|
2019-10-15 23:19:45 +02:00
|
|
|
fmt.Fprintf(w, "internal server error: %v\n", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := recover(); err != nil {
|
2020-05-30 17:49:44 +02:00
|
|
|
w.WriteHeader(500)
|
2019-10-15 23:19:45 +02:00
|
|
|
log.Printf("error: %v\n", err)
|
2020-05-30 17:36:18 +02:00
|
|
|
logRequestInfo()
|
2019-11-10 19:03:57 +01:00
|
|
|
debug.PrintStack()
|
2019-12-15 12:18:50 +01:00
|
|
|
rl.renderInternalServerError(w, r, err)
|
2019-10-15 23:19:45 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-15 12:18:50 +01:00
|
|
|
func (rl *rushlink) metricsMiddleware(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2021-05-16 20:24:00 +02:00
|
|
|
tick := time.Now()
|
2019-12-15 12:18:50 +01:00
|
|
|
srw := statusResponseWriter{Inner: w}
|
|
|
|
next.ServeHTTP(&srw, r)
|
2021-05-16 20:24:00 +02:00
|
|
|
tock := time.Now()
|
2021-05-16 20:21:44 +02:00
|
|
|
|
2019-12-15 12:18:50 +01:00
|
|
|
status := strconv.Itoa(srw.StatusCode)
|
2021-05-16 20:21:44 +02:00
|
|
|
labels := map[string]string{"code": status, "method": r.Method}
|
|
|
|
// Update requests counter metric
|
|
|
|
metricRequestsTotalCounter.With(labels).Inc()
|
2021-05-16 20:24:00 +02:00
|
|
|
// Update request latency metric
|
|
|
|
elapsed := tock.Sub(tick)
|
|
|
|
metricRequestsLatencyNanoSeconds.With(labels).Observe(elapsed.Seconds())
|
2019-12-15 12:18:50 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:56:02 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-07-27 14:22:43 +02:00
|
|
|
// InitMainRouter creates the main Gorilla router for the application.
|
2020-05-30 17:53:46 +02:00
|
|
|
//
|
|
|
|
// This function will not populate the router with an error-recovery and
|
|
|
|
// metrics-reporting middleware. If these middleware are required, then the
|
|
|
|
// caller should encapsulate this router inside of another router and register
|
|
|
|
// the middlewares on the encapsulating router.
|
2020-07-27 14:22:43 +02:00
|
|
|
func InitMainRouter(r *mux.Router, rl *rushlink) {
|
|
|
|
r.HandleFunc("/{path:img/"+staticFilenameExpr+"}", rl.staticGetHandler).Methods("GET", "HEAD")
|
|
|
|
r.HandleFunc("/{path:css/"+staticFilenameExpr+"}", rl.staticGetHandler).Methods("GET", "HEAD")
|
|
|
|
r.HandleFunc("/{path:js/"+staticFilenameExpr+"}", rl.staticGetHandler).Methods("GET", "HEAD")
|
|
|
|
r.HandleFunc("/", rl.indexGetHandler).Methods("GET", "HEAD")
|
|
|
|
r.HandleFunc("/", rl.newPasteHandler).Methods("POST")
|
|
|
|
r.HandleFunc("/"+urlKeyExpr, rl.viewPasteHandler).Methods("GET", "HEAD")
|
|
|
|
r.HandleFunc("/"+urlKeyWithExtExpr, rl.viewPasteHandler).Methods("GET", "HEAD")
|
|
|
|
r.HandleFunc("/"+urlKeyExpr+"/nr", rl.viewPasteHandlerNoRedirect).Methods("GET", "HEAD")
|
|
|
|
r.HandleFunc("/"+urlKeyWithExtExpr+"/nr", rl.viewPasteHandlerNoRedirect).Methods("GET", "HEAD")
|
|
|
|
r.HandleFunc("/"+urlKeyExpr+"/meta", rl.viewPasteHandlerMeta).Methods("GET", "HEAD")
|
|
|
|
r.HandleFunc("/"+urlKeyWithExtExpr+"/meta", rl.viewPasteHandlerMeta).Methods("GET", "HEAD")
|
|
|
|
r.HandleFunc("/"+urlKeyExpr, rl.deletePasteHandler).Methods("DELETE")
|
|
|
|
r.HandleFunc("/"+urlKeyWithExtExpr, rl.deletePasteHandler).Methods("DELETE")
|
|
|
|
r.HandleFunc("/"+urlKeyExpr+"/delete", rl.deletePasteHandler).Methods("POST")
|
|
|
|
r.HandleFunc("/"+urlKeyWithExtExpr+"/delete", rl.deletePasteHandler).Methods("POST")
|
2020-04-22 18:26:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
}
|
2019-09-19 21:29:25 +02:00
|
|
|
|
2020-05-30 17:53:46 +02:00
|
|
|
router := mux.NewRouter()
|
|
|
|
router.Use(rl.metricsMiddleware)
|
|
|
|
router.Use(rl.recoveryMiddleware)
|
2020-07-27 14:22:43 +02:00
|
|
|
InitMainRouter(router, &rl)
|
2020-05-30 17:53:46 +02:00
|
|
|
|
2019-09-19 21:29:25 +02:00
|
|
|
srv := &http.Server{
|
2020-05-30 17:53:46 +02:00
|
|
|
Handler: router,
|
2019-11-09 21:43:51 +01:00
|
|
|
Addr: addr,
|
2019-09-19 21:29:25 +02:00
|
|
|
WriteTimeout: 15 * time.Second,
|
|
|
|
ReadTimeout: 15 * time.Second,
|
|
|
|
}
|
|
|
|
log.Fatal(srv.ListenAndServe())
|
|
|
|
}
|