took me a bit to figure out how to do this properly, but i believe it's improved my understanding of how go works
46 lines
1.7 KiB
Go
46 lines
1.7 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"git.whoswhip.dev/whoswhip/minibin/internal/config"
|
|
"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))
|
|
mux.HandleFunc("GET /report", reports.ReportPageHandler(p, cStore, cfg))
|
|
mux.HandleFunc("POST /report", 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)
|
|
mux.HandleFunc("POST /upload", uploads.UploadHandler(db, cfg, p))
|
|
mux.HandleFunc("PUT /upload", uploads.UploadHandler(db, cfg, p))
|
|
mux.HandleFunc("DELETE /{id}", uploads.DeleteHandler(db, cfg, p))
|
|
mux.HandleFunc("POST /{id}/delete", uploads.DeleteHandler(db, cfg, p))
|
|
mux.HandleFunc("GET /health", health)
|
|
return mux
|
|
}
|