Browse Source

Added original /put/ api

- does not yet log to db
master
Stephen McQuay 6 years ago
parent
commit
fdbdb037c6
2 changed files with 46 additions and 0 deletions
  1. +45
    -0
      api.go
  2. +1
    -0
      main.go

+ 45
- 0
api.go View File

@@ -2,6 +2,8 @@ package main

import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
)

@@ -20,6 +22,12 @@ 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"`
@@ -36,3 +44,40 @@ func apiInfo(w http.ResponseWriter, req *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

func put(w http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
b, _ := json.Marshal(JSONError{"supported methods: POST"})
http.Error(w, string(b), http.StatusMethodNotAllowed)
return
}

body, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Printf("unable to read request body:", err)
}
req.Body.Close()

message := LogMessage{}
if *verbose {
log.Printf("incoming: '%s'", body)
}
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)
}
return
}

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

if err := json.NewEncoder(w).Encode(JSONMessage{"ok"}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

+ 1
- 0
main.go View File

@@ -24,6 +24,7 @@ func main() {
log.Printf("%+v", config)

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

err = http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)
if err != nil {


Loading…
Cancel
Save