mirror of
https://github.com/skidoodle/pastebin
synced 2025-10-14 09:44:48 +02:00
v2
This commit is contained in:
92
main.go
92
main.go
@@ -1,49 +1,101 @@
|
||||
package main
|
||||
|
||||
//go:generate go tool templ generate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/csehviktor/pastebin/handler"
|
||||
"github.com/csehviktor/pastebin/store"
|
||||
"github.com/skidoodle/pastebin/handler"
|
||||
"github.com/skidoodle/pastebin/store"
|
||||
)
|
||||
|
||||
type cli struct {
|
||||
// config holds the application configuration.
|
||||
type config struct {
|
||||
addr string
|
||||
maxSize int64
|
||||
dbPath string
|
||||
ttl time.Duration
|
||||
}
|
||||
|
||||
func parse_flags() *cli {
|
||||
cli := &cli{}
|
||||
|
||||
flag.StringVar(&cli.addr, "addr", ":3000", "socket address to bind to")
|
||||
flag.Int64Var(&cli.maxSize, "max-size", 32*1024, "maximum size of a paste in bytes")
|
||||
|
||||
// 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.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.Parse()
|
||||
|
||||
return cli
|
||||
return cfg
|
||||
}
|
||||
|
||||
func main() {
|
||||
cli := parse_flags()
|
||||
cfg := parseFlags()
|
||||
|
||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||
slog.SetDefault(logger)
|
||||
|
||||
storage, err := store.NewBoltStore(cfg.dbPath)
|
||||
if err != nil {
|
||||
slog.Error("failed to initialize store", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer storage.Close()
|
||||
|
||||
go func() {
|
||||
ticker := time.NewTicker(1 * time.Hour)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
<-ticker.C
|
||||
storage.Cleanup(cfg.ttl)
|
||||
}
|
||||
}()
|
||||
|
||||
httpHandler := handler.NewHandler(storage, cfg.maxSize)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
store := store.NewMemoryStore()
|
||||
httpHandler := handler.NewHandler(store, cli.maxSize)
|
||||
|
||||
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)
|
||||
|
||||
slog.Info("starting http server", "addr", cli.addr, "maxSize", cli.maxSize)
|
||||
|
||||
err := http.ListenAndServe(cli.addr, mux)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
server := &http.Server{
|
||||
Addr: cfg.addr,
|
||||
Handler: mux,
|
||||
}
|
||||
|
||||
go func() {
|
||||
slog.Info("starting http server", "addr", cfg.addr, "maxSize", cfg.maxSize, "dbPath", cfg.dbPath, "ttl", cfg.ttl)
|
||||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
slog.Error("server error", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}()
|
||||
|
||||
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