Use full SQL persistence, init DB in main

This commit is contained in:
Fabian Reinartz 2015-10-06 21:10:24 +02:00
parent cc5662f1e8
commit f067c96525
2 changed files with 19 additions and 30 deletions

24
main.go
View File

@ -14,6 +14,7 @@
package main package main
import ( import (
"database/sql"
"flag" "flag"
"net/http" "net/http"
"os" "os"
@ -39,17 +40,24 @@ var (
func main() { func main() {
flag.Parse() flag.Parse()
data := provider.NewMemData() db, err := sql.Open("ql", filepath.Join(*dataDir, "am.db"))
if err != nil {
alerts := provider.NewMemAlerts(data) log.Fatal(err)
notifies := provider.NewMemNotifies(data) }
defer db.Close()
// silences := provider.NewMemSilences()
silences, err := provider.NewSQLSilences(filepath.Join(*dataDir, "am.db")) 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer silences.Close()
inhibitor := &Inhibitor{alerts: alerts} inhibitor := &Inhibitor{alerts: alerts}

View File

@ -20,12 +20,7 @@ type SQLNotifyInfo struct {
db *sql.DB db *sql.DB
} }
func NewSQLNotifyInfo(file string) (*SQLNotifyInfo, error) { func NewSQLNotifyInfo(db *sql.DB) (*SQLNotifyInfo, error) {
db, err := sql.Open("ql", file)
if err != nil {
return nil, err
}
tx, err := db.Begin() tx, err := db.Begin()
if err != nil { if err != nil {
return nil, err return nil, err
@ -134,12 +129,7 @@ type SQLAlerts struct {
next int next int
} }
func NewSQLAlerts(file string) (*SQLAlerts, error) { func NewSQLAlerts(db *sql.DB) (*SQLAlerts, error) {
db, err := sql.Open("ql", file)
if err != nil {
return nil, err
}
tx, err := db.Begin() tx, err := db.Begin()
if err != nil { if err != nil {
return nil, err return nil, err
@ -426,16 +416,7 @@ type SQLSilences struct {
db *sql.DB db *sql.DB
} }
func (s *SQLSilences) Close() error { func NewSQLSilences(db *sql.DB) (*SQLSilences, error) {
return s.db.Close()
}
func NewSQLSilences(file string) (*SQLSilences, error) {
db, err := sql.Open("ql", file)
if err != nil {
return nil, err
}
tx, err := db.Begin() tx, err := db.Begin()
if err != nil { if err != nil {
return nil, err return nil, err