Browse Source

added sql storage

master
Stephen McQuay 6 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 {
Message string `json:"message"`
}

type LogMessage struct {
Level int `json:"level"`
Namespace string `json:"namespace"`
Payload interface{} `json:"payload"`
}

func apiInfo(w http.ResponseWriter, req *http.Request) {
version := struct {
Version string `json:"version"`
@@ -64,13 +58,12 @@ func put(w http.ResponseWriter, req *http.Request) {
}
parse_err := json.Unmarshal(body, &message)
if parse_err != nil {
if err := json.NewEncoder(w).Encode(JSONError{parse_err.Error()}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
b, _ := json.Marshal(JSONError{err.Error()})
http.Error(w, string(b), http.StatusMethodNotAllowed)
return
}

_, err = json.Marshal(message.Payload)
_, err = message.save()
if err != nil {
b, _ := json.Marshal(JSONError{err.Error()})
http.Error(w, string(b), http.StatusMethodNotAllowed)


+ 25
- 0
db.go View File

@@ -0,0 +1,25 @@
package main

import (
"fmt"
"github.com/jmoiron/sqlx"
"log"
)

func dbSetup(config *Configs) {
var err error
psqlConfig := fmt.Sprintf(
"dbname=%s host=%s sslmode=disable",
config.Dbname,
config.Dbhost,
)
db, err = sqlx.Open(
"postgres",
psqlConfig,
)
if err != nil {
log.Fatal("Issues connecting to db:", err)
} else {
log.Printf("connected to db %s", psqlConfig)
}
}

+ 10
- 0
init.sql View File

@@ -0,0 +1,10 @@
DROP TABLE log;

CREATE TABLE IF NOT EXISTS
log (
log_id SERIAL,
log_time timestamp DEFAULT CURRENT_TIMESTAMP,
namespace varchar(256) NULL,
level int NOT NULL DEFAULT 0,
payload json NOT NULL
);

+ 37
- 0
log.go View File

@@ -0,0 +1,37 @@
package main

import (
"database/sql"
"encoding/json"
)

type LogMessage struct {
Namespace string `json:"namespace"`
Level int `json:"level"`
Payload interface{} `json:"payload"`
}

func (l *LogMessage) json() ([]byte, error) {
return json.Marshal(l.Payload)
}

func (l *LogMessage) save() (sql.Result, error) {
j, err := l.json()
if err != nil {
return nil, err
}
params := map[string]interface{}{
"namespace": l.Namespace,
"level": l.Level,
"payload": j,
}
// TODO: actually make this return something useful as a first param, might
// need RETURNING log_id?
return db.NamedExecMap(`
INSERT INTO log
(namespace, level, payload)
VALUES
(:namespace, :level, :payload)`,
params,
)
}

+ 6
- 1
main.go View File

@@ -3,13 +3,17 @@ package main
import (
"flag"
"fmt"
"github.com/jmoiron/sqlx"
"github.com/kelseyhightower/envconfig"
_ "github.com/lib/pq"
"log"
"net/http"
)

var verbose = flag.Bool("verbose", false, "increase logging verbosity")

var db *sqlx.DB

func main() {
flag.Parse()
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
@@ -21,7 +25,8 @@ func main() {
if err := config.validate(); err != nil {
log.Fatal(err)
}
log.Printf("%+v", config)
log.Printf("config: %#v", config)
dbSetup(config)

http.Handle("/api/v0/info/", JsonHandler(apiInfo))
http.Handle("/api/v0/put/", JsonHandler(put))


Loading…
Cancel
Save