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
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}

View File

@ -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