package rushlink import ( "fmt" "log" "net/http" "runtime/debug" "time" "gitea.hashru.nl/dsprenkels/rushlink/internal/db" "github.com/gorilla/mux" ) type rushlink struct { db *db.Database fs *db.FileStore } 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) debug.PrintStack() fmt.Fprintf(w, "internal server error: %v\n", err) } }() if err := recover(); err != nil { log.Printf("error: %v\n", err) debug.PrintStack() renderInternalServerError(w, r, err) } }() next.ServeHTTP(w, r) }) } func StartMainServer(addr string, db *db.Database, fs *db.FileStore) { rl := rushlink{ db: db, fs: fs, } // Initialize Gorilla router router := mux.NewRouter() router.Use(recoveryMiddleware) 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") srv := &http.Server{ Handler: router, Addr: addr, WriteTimeout: 15 * time.Second, ReadTimeout: 15 * time.Second, } log.Fatal(srv.ListenAndServe()) }