From f067c96525899c7d75dc4fa5a81cd2b0ac27417e Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Tue, 6 Oct 2015 21:10:24 +0200 Subject: [PATCH] Use full SQL persistence, init DB in main --- main.go | 24 ++++++++++++++++-------- provider/sql.go | 25 +++---------------------- 2 files changed, 19 insertions(+), 30 deletions(-) diff --git a/main.go b/main.go index ab0efc7e..7d1ce948 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ package main import ( + "database/sql" "flag" "net/http" "os" @@ -39,17 +40,24 @@ var ( func main() { flag.Parse() - data := provider.NewMemData() - - alerts := provider.NewMemAlerts(data) - notifies := provider.NewMemNotifies(data) - - // silences := provider.NewMemSilences() - silences, err := provider.NewSQLSilences(filepath.Join(*dataDir, "am.db")) + db, err := sql.Open("ql", filepath.Join(*dataDir, "am.db")) + if err != nil { + log.Fatal(err) + } + defer db.Close() + + alerts, err := provider.NewSQLAlerts(db) + if err != nil { + log.Fatal(err) + } + notifies, err := provider.NewSQLNotifyInfo(db) + if err != nil { + log.Fatal(err) + } + silences, err := provider.NewSQLSilences(db) if err != nil { log.Fatal(err) } - defer silences.Close() inhibitor := &Inhibitor{alerts: alerts} diff --git a/provider/sql.go b/provider/sql.go index 9535ef25..20af7a76 100644 --- a/provider/sql.go +++ b/provider/sql.go @@ -20,12 +20,7 @@ type SQLNotifyInfo struct { db *sql.DB } -func NewSQLNotifyInfo(file string) (*SQLNotifyInfo, error) { - db, err := sql.Open("ql", file) - if err != nil { - return nil, err - } - +func NewSQLNotifyInfo(db *sql.DB) (*SQLNotifyInfo, error) { tx, err := db.Begin() if err != nil { return nil, err @@ -134,12 +129,7 @@ type SQLAlerts struct { next int } -func NewSQLAlerts(file string) (*SQLAlerts, error) { - db, err := sql.Open("ql", file) - if err != nil { - return nil, err - } - +func NewSQLAlerts(db *sql.DB) (*SQLAlerts, error) { tx, err := db.Begin() if err != nil { return nil, err @@ -426,16 +416,7 @@ type SQLSilences struct { db *sql.DB } -func (s *SQLSilences) Close() error { - return s.db.Close() -} - -func NewSQLSilences(file string) (*SQLSilences, error) { - db, err := sql.Open("ql", file) - if err != nil { - return nil, err - } - +func NewSQLSilences(db *sql.DB) (*SQLSilences, error) { tx, err := db.Begin() if err != nil { return nil, err