diff options
-rw-r--r-- | nsa.go | 10 |
1 files changed, 10 insertions, 0 deletions
@@ -3,6 +3,7 @@ package nsa | |||
3 | import ( | 3 | import ( |
4 | "database/sql" | 4 | "database/sql" |
5 | "encoding/json" | 5 | "encoding/json" |
6 | "errors" | ||
6 | "fmt" | 7 | "fmt" |
7 | "os" | 8 | "os" |
8 | "strings" | 9 | "strings" |
@@ -40,6 +41,9 @@ func (l *Logger) getJSON(data interface{}) string { | |||
40 | } | 41 | } |
41 | 42 | ||
42 | func (l *Logger) insertEvent(id int, data string) { | 43 | func (l *Logger) insertEvent(id int, data string) { |
44 | if database == nil { | ||
45 | return | ||
46 | } | ||
43 | _, err := database.Exec( | 47 | _, err := database.Exec( |
44 | "INSERT INTO nsa.events (type, data, client_ip) VALUES ($1, $2, $3);", | 48 | "INSERT INTO nsa.events (type, data, client_ip) VALUES ($1, $2, $3);", |
45 | id, data, l.ip) | 49 | id, data, l.ip) |
@@ -49,6 +53,9 @@ func (l *Logger) insertEvent(id int, data string) { | |||
49 | } | 53 | } |
50 | 54 | ||
51 | func (l *Logger) Log(event string, data interface{}) { | 55 | func (l *Logger) Log(event string, data interface{}) { |
56 | if database == nil { | ||
57 | return | ||
58 | } | ||
52 | go func() { | 59 | go func() { |
53 | id := l.getEventID(event) | 60 | id := l.getEventID(event) |
54 | if id == 0 { | 61 | if id == 0 { |
@@ -79,6 +86,9 @@ func Init(db *sql.DB) error { | |||
79 | } | 86 | } |
80 | 87 | ||
81 | func SetEvents(events []string) error { | 88 | func SetEvents(events []string) error { |
89 | if database == nil { | ||
90 | return errors.New("not initialized") | ||
91 | } | ||
82 | rows, err := database.Query("SELECT id, name FROM nsa.event_types;") | 92 | rows, err := database.Query("SELECT id, name FROM nsa.event_types;") |
83 | if err != nil { | 93 | if err != nil { |
84 | return err | 94 | return err |