organize project

took me a bit to figure out how to do this properly, but i believe it's improved my understanding of how go works
This commit is contained in:
2026-11-21 16:18:30 -05:00
parent 741b91b637
commit e5a2f47f47
28 changed files with 304 additions and 1148 deletions
+45
View File
@@ -0,0 +1,45 @@
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
}