This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,5 +2,4 @@
|
||||
*.db
|
||||
*.db-shm
|
||||
*.db-wal
|
||||
pocketwatch
|
||||
.DS_Store
|
||||
|
||||
74
cmd/pocketwatch/main.go
Normal file
74
cmd/pocketwatch/main.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"time"
|
||||
_ "time/tzdata"
|
||||
|
||||
"pocketwatch-go/internal/app"
|
||||
)
|
||||
|
||||
func main() {
|
||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
|
||||
addr := env("APP_ADDR", ":8080")
|
||||
dataDir := env("DATA_DIR", "/data")
|
||||
if err := os.MkdirAll(dataDir, 0o750); err != nil {
|
||||
logger.Error("create data directory", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
cfg := app.Config{
|
||||
DBPath: filepath.Join(dataDir, "pocketwatch.db"),
|
||||
CookieSecure: env("COOKIE_SECURE", "false") == "true",
|
||||
SessionTTL: 30 * 24 * time.Hour,
|
||||
}
|
||||
|
||||
a, err := app.New(cfg, logger)
|
||||
if err != nil {
|
||||
logger.Error("initialize app", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer a.Close()
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: a.Handler(),
|
||||
ReadHeaderTimeout: 10 * time.Second,
|
||||
ReadTimeout: 20 * time.Second,
|
||||
WriteTimeout: 60 * time.Second,
|
||||
IdleTimeout: 120 * time.Second,
|
||||
MaxHeaderBytes: 1 << 20,
|
||||
}
|
||||
|
||||
go func() {
|
||||
logger.Info("pocketwatch listening", "addr", addr, "db", cfg.DBPath)
|
||||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
logger.Error("http server", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}()
|
||||
|
||||
stop := make(chan os.Signal, 1)
|
||||
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
|
||||
<-stop
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := srv.Shutdown(ctx); err != nil {
|
||||
logger.Error("graceful shutdown", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func env(key, fallback string) string {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
return v
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
Reference in New Issue
Block a user