feat(02-02): convert handlers to Server struct methods, remove globals
- Add Server struct with store Store and webhookSecret fields - Add NewServer constructor - Convert all 6 handler functions to methods on *Server - Replace all inline SQL with s.store.X() calls - Remove package-level globals db, mu, webhookSecret - Remove InitDB, SetWebhookSecret, UpdateEvent, GetUpdates functions - Update export_test.go: replace old helpers with NewTestServer, NewTestServerWithSecret, TestUpsertEvent, TestGetUpdatesMap - Update main.go: sql.Open -> RunMigrations -> NewSQLiteStore -> NewServer -> routes
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
"time"
|
||||
|
||||
diun "awesomeProject/pkg/diunwebhook"
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -18,33 +20,42 @@ func main() {
|
||||
if dbPath == "" {
|
||||
dbPath = "./diun.db"
|
||||
}
|
||||
if err := diun.InitDB(dbPath); err != nil {
|
||||
log.Fatalf("InitDB: %v", err)
|
||||
|
||||
db, err := sql.Open("sqlite", dbPath)
|
||||
if err != nil {
|
||||
log.Fatalf("sql.Open: %v", err)
|
||||
}
|
||||
|
||||
if err := diun.RunMigrations(db); err != nil {
|
||||
log.Fatalf("RunMigrations: %v", err)
|
||||
}
|
||||
|
||||
store := diun.NewSQLiteStore(db)
|
||||
|
||||
secret := os.Getenv("WEBHOOK_SECRET")
|
||||
if secret == "" {
|
||||
log.Println("WARNING: WEBHOOK_SECRET not set — webhook endpoint is unprotected")
|
||||
} else {
|
||||
diun.SetWebhookSecret(secret)
|
||||
log.Println("Webhook endpoint protected with token authentication")
|
||||
}
|
||||
|
||||
srv := diun.NewServer(store, secret)
|
||||
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/webhook", diun.WebhookHandler)
|
||||
mux.HandleFunc("/api/updates/", diun.DismissHandler)
|
||||
mux.HandleFunc("/api/updates", diun.UpdatesHandler)
|
||||
mux.HandleFunc("/api/tags", diun.TagsHandler)
|
||||
mux.HandleFunc("/api/tags/", diun.TagByIDHandler)
|
||||
mux.HandleFunc("/api/tag-assignments", diun.TagAssignmentHandler)
|
||||
mux.HandleFunc("/webhook", srv.WebhookHandler)
|
||||
mux.HandleFunc("/api/updates/", srv.DismissHandler)
|
||||
mux.HandleFunc("/api/updates", srv.UpdatesHandler)
|
||||
mux.HandleFunc("/api/tags", srv.TagsHandler)
|
||||
mux.HandleFunc("/api/tags/", srv.TagByIDHandler)
|
||||
mux.HandleFunc("/api/tag-assignments", srv.TagAssignmentHandler)
|
||||
mux.Handle("/", http.FileServer(http.Dir("./frontend/dist")))
|
||||
|
||||
srv := &http.Server{
|
||||
httpSrv := &http.Server{
|
||||
Addr: ":" + port,
|
||||
Handler: mux,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
@@ -57,7 +68,7 @@ func main() {
|
||||
|
||||
go func() {
|
||||
log.Printf("Listening on :%s", port)
|
||||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
if err := httpSrv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Fatalf("ListenAndServe: %v", err)
|
||||
}
|
||||
}()
|
||||
@@ -67,7 +78,7 @@ func main() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := srv.Shutdown(ctx); err != nil {
|
||||
if err := httpSrv.Shutdown(ctx); err != nil {
|
||||
log.Printf("Shutdown error: %v", err)
|
||||
} else {
|
||||
log.Println("Server stopped cleanly")
|
||||
|
||||
Reference in New Issue
Block a user