organize
This commit is contained in:
@@ -2,17 +2,9 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"html/template"
|
||||
"log"
|
||||
"math/rand/v2"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -35,45 +27,11 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
pages, err := template.New("").Funcs(template.FuncMap{
|
||||
"hasPrefix": strings.HasPrefix,
|
||||
}).ParseGlob("templates/*.html")
|
||||
pages, err := loadPages()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
qtFiles, err := os.ReadDir("qts")
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var qts []string
|
||||
for _, qt := range qtFiles {
|
||||
if qt.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
switch strings.ToLower(filepath.Ext(qt.Name())) {
|
||||
case ".jpeg", "jpg", ".jxl", ".png", ".webp", ".avif", ".gif":
|
||||
qts = append(qts, "/qts/"+url.PathEscape(qt.Name()))
|
||||
}
|
||||
}
|
||||
|
||||
pastePlaceholders, err := os.ReadFile("paste-placeholders.txt")
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var placeholders []string
|
||||
if err == nil {
|
||||
content := strings.ReplaceAll(string(pastePlaceholders), "\r\n", "\n")
|
||||
placeholders = strings.Split(strings.TrimSpace(content), "\n")
|
||||
} else {
|
||||
placeholders = []string{
|
||||
"cmon, paste something already!",
|
||||
}
|
||||
}
|
||||
|
||||
tracker := &AccessTracker{
|
||||
pending: make(map[string]int64),
|
||||
}
|
||||
@@ -81,60 +39,10 @@ func main() {
|
||||
defer cancel()
|
||||
go tracker.Run(ctx, db)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
fs := http.FileServer(http.Dir("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 /{$}", func(w http.ResponseWriter, r *http.Request) {
|
||||
data := HomePageData{
|
||||
Expiries: config.Expiries,
|
||||
FileMaxSize: humanize.Bytes(uint64(config.MaxFileBytes)),
|
||||
PasteMaxSize: humanize.Bytes(uint64(config.MaxPasteBytes)),
|
||||
Qt: getRandomQt(qts),
|
||||
}
|
||||
|
||||
if len(placeholders) > 0 {
|
||||
data.PastePlaceholder = placeholders[rand.IntN(len(placeholders))]
|
||||
}
|
||||
|
||||
render(pages, w, "home.html", data)
|
||||
})
|
||||
mux.HandleFunc("GET /tos", staticPage(pages, "tos.html", qts))
|
||||
mux.HandleFunc("GET /privacy", staticPage(pages, "privacy.html", qts))
|
||||
mux.HandleFunc("GET /faq", func(w http.ResponseWriter, r *http.Request) {
|
||||
render(pages, w, "faq.html", map[string]any{
|
||||
"Expiries": config.Expiries,
|
||||
"Host": config.Host,
|
||||
"NormalizeMIMETypes": config.NormalizeMIMETypes,
|
||||
"Qt": getRandomQt(qts),
|
||||
})
|
||||
})
|
||||
uploads := http.NewServeMux()
|
||||
uploads.HandleFunc("GET /{id}", contentHandler(db, config, tracker))
|
||||
uploads.HandleFunc("GET /{id}/info", contentInfoHandler(db, tracker))
|
||||
mux.Handle("/", uploads)
|
||||
mux.HandleFunc("POST /upload", uploadHandler(db, config, pages, qts))
|
||||
mux.HandleFunc("PUT /upload", uploadHandler(db, config, pages, qts))
|
||||
mux.HandleFunc("DELETE /{id}", deleteHandler(db, config))
|
||||
mux.HandleFunc("POST /{id}/delete", deleteHandler(db, config))
|
||||
mux.HandleFunc("GET /health", health)
|
||||
|
||||
log.Println("listening on", config.Addr)
|
||||
|
||||
err = http.ListenAndServe(config.Addr, mux)
|
||||
router := newRouter(db, config, pages, tracker)
|
||||
err = http.ListenAndServe(config.Addr, router)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func getRandomQt(qts []string) string {
|
||||
if len(qts) > 0 {
|
||||
return qts[rand.IntN(len(qts))]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user