Browse Source

added sql storage

Stephen McQuay 4 years ago
parent
commit
5732f06932
5 changed files with 81 additions and 11 deletions
  1. 3
    10
      api.go
  2. 25
    0
      db.go
  3. 10
    0
      init.sql
  4. 37
    0
      log.go
  5. 6
    1
      main.go

+ 3
- 10
api.go View File

@@ -22,12 +22,6 @@ type JSONMessage struct {
22 22
 	Message string `json:"message"`
23 23
 }
24 24
 
25
-type LogMessage struct {
26
-	Level     int         `json:"level"`
27
-	Namespace string      `json:"namespace"`
28
-	Payload   interface{} `json:"payload"`
29
-}
30
-
31 25
 func apiInfo(w http.ResponseWriter, req *http.Request) {
32 26
 	version := struct {
33 27
 		Version string   `json:"version"`
@@ -64,13 +58,12 @@ func put(w http.ResponseWriter, req *http.Request) {
64 58
 	}
65 59
 	parse_err := json.Unmarshal(body, &message)
66 60
 	if parse_err != nil {
67
-		if err := json.NewEncoder(w).Encode(JSONError{parse_err.Error()}); err != nil {
68
-			http.Error(w, err.Error(), http.StatusInternalServerError)
69
-		}
61
+		b, _ := json.Marshal(JSONError{err.Error()})
62
+		http.Error(w, string(b), http.StatusMethodNotAllowed)
70 63
 		return
71 64
 	}
72 65
 
73
-	_, err = json.Marshal(message.Payload)
66
+	_, err = message.save()
74 67
 	if err != nil {
75 68
 		b, _ := json.Marshal(JSONError{err.Error()})
76 69
 		http.Error(w, string(b), http.StatusMethodNotAllowed)

+ 25
- 0
db.go View File

@@ -0,0 +1,25 @@
1
+package main
2
+
3
+import (
4
+	"fmt"
5
+	"github.com/jmoiron/sqlx"
6
+	"log"
7
+)
8
+
9
+func dbSetup(config *Configs) {
10
+	var err error
11
+	psqlConfig := fmt.Sprintf(
12
+		"dbname=%s host=%s sslmode=disable",
13
+		config.Dbname,
14
+		config.Dbhost,
15
+	)
16
+	db, err = sqlx.Open(
17
+		"postgres",
18
+		psqlConfig,
19
+	)
20
+	if err != nil {
21
+		log.Fatal("Issues connecting to db:", err)
22
+	} else {
23
+		log.Printf("connected to db %s", psqlConfig)
24
+	}
25
+}

+ 10
- 0
init.sql View File

@@ -0,0 +1,10 @@
1
+DROP TABLE log;
2
+
3
+CREATE TABLE IF NOT EXISTS
4
+log (
5
+    log_id SERIAL,
6
+    log_time timestamp DEFAULT CURRENT_TIMESTAMP,
7
+    namespace varchar(256) NULL,
8
+    level int NOT NULL DEFAULT 0,
9
+    payload json NOT NULL
10
+);

+ 37
- 0
log.go View File

@@ -0,0 +1,37 @@
1
+package main
2
+
3
+import (
4
+	"database/sql"
5
+	"encoding/json"
6
+)
7
+
8
+type LogMessage struct {
9
+	Namespace string      `json:"namespace"`
10
+	Level     int         `json:"level"`
11
+	Payload   interface{} `json:"payload"`
12
+}
13
+
14
+func (l *LogMessage) json() ([]byte, error) {
15
+	return json.Marshal(l.Payload)
16
+}
17
+
18
+func (l *LogMessage) save() (sql.Result, error) {
19
+	j, err := l.json()
20
+	if err != nil {
21
+		return nil, err
22
+	}
23
+	params := map[string]interface{}{
24
+		"namespace": l.Namespace,
25
+		"level":     l.Level,
26
+		"payload":   j,
27
+	}
28
+	// TODO: actually make this return something useful as a first param, might
29
+	// need RETURNING log_id?
30
+	return db.NamedExecMap(`
31
+		INSERT INTO log
32
+			(namespace, level, payload)
33
+		VALUES
34
+			(:namespace, :level, :payload)`,
35
+		params,
36
+	)
37
+}

+ 6
- 1
main.go View File

@@ -3,13 +3,17 @@ package main
3 3
 import (
4 4
 	"flag"
5 5
 	"fmt"
6
+	"github.com/jmoiron/sqlx"
6 7
 	"github.com/kelseyhightower/envconfig"
8
+	_ "github.com/lib/pq"
7 9
 	"log"
8 10
 	"net/http"
9 11
 )
10 12
 
11 13
 var verbose = flag.Bool("verbose", false, "increase logging verbosity")
12 14
 
15
+var db *sqlx.DB
16
+
13 17
 func main() {
14 18
 	flag.Parse()
15 19
 	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
@@ -21,7 +25,8 @@ func main() {
21 25
 	if err := config.validate(); err != nil {
22 26
 		log.Fatal(err)
23 27
 	}
24
-	log.Printf("%+v", config)
28
+	log.Printf("config: %#v", config)
29
+	dbSetup(config)
25 30
 
26 31
 	http.Handle("/api/v0/info/", JsonHandler(apiInfo))
27 32
 	http.Handle("/api/v0/put/", JsonHandler(put))

Loading…
Cancel
Save