organize
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"html/template"
|
||||
"log"
|
||||
"math/rand/v2"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
)
|
||||
|
||||
type Pages struct {
|
||||
Templates *template.Template
|
||||
Qts []string
|
||||
Placeholders []string
|
||||
}
|
||||
|
||||
type HomePageData struct {
|
||||
Expiries []Expiry
|
||||
Qt string
|
||||
PastePlaceholder string
|
||||
FileMaxSize string
|
||||
PasteMaxSize string
|
||||
}
|
||||
|
||||
func render(pages *Pages, w http.ResponseWriter, name string, data any) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
if err := pages.Templates.ExecuteTemplate(w, name, data); err != nil {
|
||||
log.Printf("render %s: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
func staticPage(pages *Pages, name string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
render(pages, w, name, getRandomQt(pages.Qts))
|
||||
}
|
||||
}
|
||||
|
||||
func getRandomQt(qts []string) string {
|
||||
if len(qts) > 0 {
|
||||
return qts[rand.IntN(len(qts))]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func loadPages() (*Pages, error) {
|
||||
templates, err := template.New("").Funcs(template.FuncMap{
|
||||
"hasPrefix": strings.HasPrefix,
|
||||
}).ParseGlob("templates/*.html")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pages := &Pages{
|
||||
Templates: templates,
|
||||
Placeholders: []string{"cmon, paste something already!"},
|
||||
}
|
||||
|
||||
qtFiles, err := os.ReadDir("qts")
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, err
|
||||
}
|
||||
for _, qt := range qtFiles {
|
||||
if qt.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
switch strings.ToLower(filepath.Ext(qt.Name())) {
|
||||
case ".jpeg", ".jpg", ".jxl", ".png", ".webp", ".avif", ".gif":
|
||||
pages.Qts = append(pages.Qts, "/qts/"+url.PathEscape(qt.Name()))
|
||||
}
|
||||
}
|
||||
|
||||
pastePlaceholders, err := os.ReadFile("paste-placeholders.txt")
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
placeholders := strings.TrimSpace(strings.ReplaceAll(string(pastePlaceholders), "\r\n", "\n"))
|
||||
if placeholders != "" {
|
||||
pages.Placeholders = strings.Split(placeholders, "\n")
|
||||
}
|
||||
return pages, nil
|
||||
}
|
||||
|
||||
func homeHandler(pages *Pages, cfg Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
data := HomePageData{
|
||||
Expiries: cfg.Expiries,
|
||||
FileMaxSize: humanize.Bytes(uint64(cfg.MaxFileBytes)),
|
||||
PasteMaxSize: humanize.Bytes(uint64(cfg.MaxPasteBytes)),
|
||||
Qt: getRandomQt(pages.Qts),
|
||||
}
|
||||
|
||||
if len(pages.Placeholders) > 0 {
|
||||
data.PastePlaceholder = pages.Placeholders[rand.IntN(len(pages.Placeholders))]
|
||||
}
|
||||
|
||||
render(pages, w, "home.html", data)
|
||||
}
|
||||
}
|
||||
|
||||
func faqHandler(pages *Pages, cfg Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
render(pages, w, "faq.html", map[string]any{
|
||||
"Expiries": cfg.Expiries,
|
||||
"Host": cfg.Host,
|
||||
"NormalizeMIMETypes": cfg.NormalizeMIMETypes,
|
||||
"Qt": getRandomQt(pages.Qts),
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user