add paste functionality
Some checks failed
Build and Deploy Website / build (push) Failing after 2m46s

This commit is contained in:
2026-02-27 23:38:14 -08:00
parent ed5e40b3f4
commit 3934751615
15 changed files with 959 additions and 260 deletions

View File

@@ -1,31 +1,46 @@
package main
import (
"log"
"context"
"log/slog"
"net/http"
"os"
"path/filepath"
"time"
"golang.org/x/time/rate"
"smpark.in/paste"
)
func main() {
publicDir := "./public"
spaIndex := filepath.Join(publicDir, "app", "index.html")
store := paste.NewStore()
go paste.StartExpiryWorker(context.Background(), store, 30*time.Second)
rl := paste.NewRateLimiter(rate.Every(30*time.Second), 1)
handlers := paste.NewHandlers(store)
http.HandleFunc("POST /api/paste", rl.Middleware(handlers.Create))
http.HandleFunc("GET /api/paste/{id}/raw", handlers.GetRaw)
http.HandleFunc("GET /api/paste/{id}", handlers.Get)
fs := http.FileServer(http.Dir(publicDir))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Try to serve the static file
path := filepath.Join(publicDir, filepath.Clean(r.URL.Path))
if info, err := os.Stat(path); err == nil && !info.IsDir() {
fs.ServeHTTP(w, r)
return
}
// Fall back to SPA index for client-side routing
http.ServeFile(w, r, spaIndex)
})
addr := ":8080"
log.Printf("Listening on %s", addr)
log.Fatal(http.ListenAndServe(addr, nil))
slog.Info("listening", "addr", addr)
if err := http.ListenAndServe(addr, nil); err != nil {
slog.Error("server error", "err", err)
}
}