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"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
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)
|
|
|
|
fmt.Fprintf(w, "internal server error: %v\n", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
log.Printf("error: %v\n", err)
|
|
|
|
renderInternalServerError(w, r, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-19 21:29:25 +02:00
|
|
|
func StartMainServer() {
|
|
|
|
// Initialize Gorilla router
|
|
|
|
router := mux.NewRouter()
|
2019-10-15 23:19:45 +02:00
|
|
|
router.Use(recoveryMiddleware)
|
2019-09-19 21:29:25 +02:00
|
|
|
router.HandleFunc("/", indexGetHandler).Methods("GET")
|
2019-09-21 13:11:38 +02:00
|
|
|
router.HandleFunc("/", newPasteHandler).Methods("POST")
|
|
|
|
router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}", viewPasteHandler).Methods("GET")
|
|
|
|
router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}/nr", viewPasteHandlerNoRedirect).Methods("GET")
|
|
|
|
router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}/meta", viewPasteHandlerMeta).Methods("GET")
|
|
|
|
router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}", deletePasteHandler).Methods("DELETE")
|
|
|
|
router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}/delete", deletePasteHandler).Methods("POST")
|
2019-09-19 21:29:25 +02:00
|
|
|
|
|
|
|
srv := &http.Server{
|
|
|
|
Handler: router,
|
|
|
|
Addr: "127.0.0.1:8000",
|
|
|
|
WriteTimeout: 15 * time.Second,
|
|
|
|
ReadTimeout: 15 * time.Second,
|
|
|
|
}
|
|
|
|
log.Fatal(srv.ListenAndServe())
|
|
|
|
}
|