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:
@@ -1,19 +1,46 @@
|
||||
package diunwebhook
|
||||
|
||||
func GetUpdatesMap() map[string]UpdateEntry {
|
||||
m, _ := GetUpdates()
|
||||
import "database/sql"
|
||||
|
||||
// NewTestServer constructs a Server with a fresh in-memory SQLite database.
|
||||
// Each call returns an isolated server -- tests do not share state.
|
||||
func NewTestServer() (*Server, error) {
|
||||
db, err := sql.Open("sqlite", ":memory:")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := RunMigrations(db); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
store := NewSQLiteStore(db)
|
||||
return NewServer(store, ""), nil
|
||||
}
|
||||
|
||||
// NewTestServerWithSecret constructs a Server with webhook authentication enabled.
|
||||
func NewTestServerWithSecret(secret string) (*Server, error) {
|
||||
db, err := sql.Open("sqlite", ":memory:")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := RunMigrations(db); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
store := NewSQLiteStore(db)
|
||||
return NewServer(store, secret), nil
|
||||
}
|
||||
|
||||
// TestUpsertEvent calls UpsertEvent on the server's store (for test setup).
|
||||
func (s *Server) TestUpsertEvent(event DiunEvent) error {
|
||||
return s.store.UpsertEvent(event)
|
||||
}
|
||||
|
||||
// TestGetUpdates calls GetUpdates on the server's store (for test assertions).
|
||||
func (s *Server) TestGetUpdates() (map[string]UpdateEntry, error) {
|
||||
return s.store.GetUpdates()
|
||||
}
|
||||
|
||||
// TestGetUpdatesMap is a convenience wrapper that returns the map without error.
|
||||
func (s *Server) TestGetUpdatesMap() map[string]UpdateEntry {
|
||||
m, _ := s.store.GetUpdates()
|
||||
return m
|
||||
}
|
||||
|
||||
func UpdatesReset() {
|
||||
InitDB(":memory:")
|
||||
}
|
||||
|
||||
func ResetTags() {
|
||||
db.Exec(`DELETE FROM tag_assignments`)
|
||||
db.Exec(`DELETE FROM tags`)
|
||||
}
|
||||
|
||||
func ResetWebhookSecret() {
|
||||
SetWebhookSecret("")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user