Files
minibin/internal/server/routes.go
T
2026-11-22 19:07:55 -05:00

58 lines
2.1 KiB
Go

package server
import (
"fmt"
"net/http"
"time"
"git.whoswhip.dev/whoswhip/minibin/internal/config"
"git.whoswhip.dev/whoswhip/minibin/internal/middleware"
"git.whoswhip.dev/whoswhip/minibin/internal/pages"
"git.whoswhip.dev/whoswhip/minibin/internal/reports"
"git.whoswhip.dev/whoswhip/minibin/internal/uploads"
"gorm.io/gorm"
)
func health(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprintln(w, "OK")
}
func NewRouter(db *gorm.DB, cfg config.Config, p *pages.Pages, tracker *uploads.AccessTracker, cStore *reports.ChallengeStore) http.Handler {
mux := http.NewServeMux()
fs := http.FileServer(http.Dir("web/static"))
mux.Handle("GET /css/", fs)
mux.Handle("GET /js/", fs)
mux.Handle("GET /static/", http.StripPrefix("/static/", fs))
mux.Handle("GET /qts/", http.StripPrefix(
"/qts/",
http.FileServer((http.Dir("qts"))),
))
mux.HandleFunc("GET /{$}", pages.HomeHandler(p, cfg))
mux.HandleFunc("GET /tos", pages.StaticPage(p, "tos.html"))
mux.HandleFunc("GET /privacy", pages.StaticPage(p, "privacy.html"))
mux.HandleFunc("GET /faq", pages.FaqHandler(p, cfg))
clientIpResolver := newClientIPResolve(cfg.IPAdressSource, cfg.TrustedProxies)
reportLimiter := middleware.NewRateLimiter(12*time.Second, 2, p, clientIpResolver)
mux.HandleFunc("GET /report", reports.ReportPageHandler(p, cStore, cfg))
mux.HandleFunc("POST /report", reportLimiter.LimitRequest(reports.ReportUploadHandler(db, cfg, p, cStore)))
u := http.NewServeMux()
u.HandleFunc("GET /{id}", uploads.ContentHandler(db, cfg, tracker, p))
u.HandleFunc("GET /{id}/info", uploads.ContentInfoHandler(db, tracker, p))
mux.Handle("/", u)
uploadLimiter := middleware.NewRateLimiter(6*time.Second, 3, p, clientIpResolver)
ulr := uploadLimiter.LimitRequest
mux.HandleFunc("POST /upload", ulr(uploads.UploadHandler(db, cfg, p)))
mux.HandleFunc("PUT /upload", ulr(uploads.UploadHandler(db, cfg, p)))
mux.HandleFunc("DELETE /{id}", ulr(uploads.DeleteHandler(db, cfg, p)))
mux.HandleFunc("POST /{id}/delete", ulr(uploads.DeleteHandler(db, cfg, p)))
mux.HandleFunc("GET /health", health)
return mux
}