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-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-03 23:08:58 +01:00
|
|
|
type rushlink struct {
|
|
|
|
db *db.Database
|
|
|
|
fs *db.FileStore
|
|
|
|
}
|
|
|
|
|
2019-10-15 23:19:45 +02:00
|
|
|
func 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 {
|
|
|
|
log.Printf("error: panic while recovering from another panic: %v\n", err)
|
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 {
|
|
|
|
log.Printf("error: %v\n", err)
|
2019-11-10 19:03:57 +01:00
|
|
|
debug.PrintStack()
|
2019-10-15 23:19:45 +02:00
|
|
|
renderInternalServerError(w, r, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func 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()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartMainServer starts the main http server listening on addr.
|
2019-12-03 23:08:58 +01:00
|
|
|
func StartMainServer(addr string, db *db.Database, fs *db.FileStore) {
|
|
|
|
rl := rushlink{
|
|
|
|
db: db,
|
|
|
|
fs: fs,
|
|
|
|
}
|
|
|
|
|
2019-09-19 21:29:25 +02:00
|
|
|
// Initialize Gorilla router
|
|
|
|
router := mux.NewRouter()
|
2019-10-15 23:19:45 +02:00
|
|
|
router.Use(recoveryMiddleware)
|
2019-12-08 21:56:02 +01:00
|
|
|
router.Use(metricsMiddleware)
|
2019-12-03 23:08:58 +01:00
|
|
|
router.HandleFunc("/", rl.indexGetHandler).Methods("GET")
|
|
|
|
router.HandleFunc("/", rl.newPasteHandler).Methods("POST")
|
|
|
|
router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}", rl.viewPasteHandler).Methods("GET")
|
|
|
|
router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}/nr", rl.viewPasteHandlerNoRedirect).Methods("GET")
|
|
|
|
router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}/meta", rl.viewPasteHandlerMeta).Methods("GET")
|
|
|
|
router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}", rl.deletePasteHandler).Methods("DELETE")
|
|
|
|
router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}/delete", rl.deletePasteHandler).Methods("POST")
|
|
|
|
router.HandleFunc("/uploads/{id:[A-Za-z0-9-_]+}/{filename:.+}", rl.uploadFileGetHandler).Methods("GET")
|
2019-09-19 21:29:25 +02:00
|
|
|
|
|
|
|
srv := &http.Server{
|
|
|
|
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())
|
|
|
|
}
|