mirror of
https://github.com/skidoodle/pastebin
synced 2026-04-28 03:07:40 +02:00
small refactor
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
package main
|
||||
|
||||
//go:generate go tool templ generate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
@@ -9,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -16,7 +15,6 @@ import (
|
||||
"github.com/skidoodle/pastebin/store"
|
||||
)
|
||||
|
||||
// config holds the application configuration.
|
||||
type config struct {
|
||||
addr string
|
||||
maxSize int64
|
||||
@@ -24,13 +22,12 @@ type config struct {
|
||||
ttl time.Duration
|
||||
}
|
||||
|
||||
// parseFlags parses command-line flags.
|
||||
func parseFlags() *config {
|
||||
cfg := &config{}
|
||||
flag.StringVar(&cfg.addr, "addr", ":3000", "socket address to bind to")
|
||||
flag.Int64Var(&cfg.maxSize, "max-size", 32*1024, "maximum size of a paste in bytes")
|
||||
flag.Int64Var(&cfg.maxSize, "max-size", 10*1024*1024, "maximum size of a paste in bytes")
|
||||
flag.StringVar(&cfg.dbPath, "db-path", "pastebin.db", "path to the database file")
|
||||
flag.DurationVar(&cfg.ttl, "ttl", 7*24*time.Hour, "time to live for pastes (e.g., 24h, 7d)")
|
||||
flag.DurationVar(&cfg.ttl, "ttl", 7*24*time.Hour, "time to live for pastes")
|
||||
flag.Parse()
|
||||
return cfg
|
||||
}
|
||||
@@ -38,8 +35,7 @@ func parseFlags() *config {
|
||||
func main() {
|
||||
cfg := parseFlags()
|
||||
|
||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||
slog.SetDefault(logger)
|
||||
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil)))
|
||||
|
||||
storage, err := store.NewBoltStore(cfg.dbPath)
|
||||
if err != nil {
|
||||
@@ -51,25 +47,34 @@ func main() {
|
||||
go func() {
|
||||
ticker := time.NewTicker(1 * time.Hour)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
<-ticker.C
|
||||
for range ticker.C {
|
||||
storage.Cleanup(cfg.ttl)
|
||||
}
|
||||
}()
|
||||
|
||||
httpHandler := handler.NewHandler(storage, cfg.maxSize)
|
||||
h := handler.NewHandler(storage, cfg.maxSize, "view/templates/*.html")
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /style.css", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, "view/style.css")
|
||||
})
|
||||
mux.HandleFunc("GET /", httpHandler.HandleHome)
|
||||
mux.HandleFunc("POST /", httpHandler.HandleSet)
|
||||
|
||||
mux.HandleFunc("GET /{id}", httpHandler.HandleGet)
|
||||
mux.HandleFunc("GET /{id}/", httpHandler.HandleGet)
|
||||
mux.HandleFunc("GET /{id}/{theme}", httpHandler.HandleGet)
|
||||
mux.HandleFunc("GET /{id}/{theme}/", httpHandler.HandleGet)
|
||||
mux.HandleFunc("GET /static", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||
})
|
||||
|
||||
mux.HandleFunc("GET /static/{file...}", func(w http.ResponseWriter, r *http.Request) {
|
||||
file := r.PathValue("file")
|
||||
|
||||
if file == "" || strings.HasSuffix(file, "/") {
|
||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
http.ServeFile(w, r, "view/static/"+file)
|
||||
})
|
||||
|
||||
mux.HandleFunc("GET /", h.HandleHome)
|
||||
mux.HandleFunc("POST /", h.HandleSet)
|
||||
mux.HandleFunc("GET /raw/{id}", h.HandleRaw)
|
||||
mux.HandleFunc("GET /{id}", h.HandleGet)
|
||||
|
||||
server := &http.Server{
|
||||
Addr: cfg.addr,
|
||||
@@ -77,7 +82,7 @@ func main() {
|
||||
}
|
||||
|
||||
go func() {
|
||||
slog.Info("starting http server", "addr", cfg.addr, "maxSize", cfg.maxSize, "dbPath", cfg.dbPath, "ttl", cfg.ttl)
|
||||
slog.Info("starting http server", "addr", cfg.addr)
|
||||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
slog.Error("server error", "err", err)
|
||||
os.Exit(1)
|
||||
@@ -87,15 +92,11 @@ func main() {
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-quit
|
||||
slog.Info("shutting down server...")
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := server.Shutdown(ctx); err != nil {
|
||||
slog.Error("server shutdown failed", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
slog.Info("server exited gracefully")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user