45 lines
754 B
Go
45 lines
754 B
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"logger/data"
|
|
"net/http"
|
|
)
|
|
|
|
type JSONPayload struct {
|
|
Name string `json:"name"`
|
|
Data string `json:"data"`
|
|
}
|
|
|
|
func (app *Config) WriteLog(w http.ResponseWriter, r *http.Request) {
|
|
// read json into var
|
|
var requestPayload JSONPayload
|
|
err := app.readJSON(w, r, &requestPayload)
|
|
if err != nil {
|
|
app.errorJSON(w, err)
|
|
return
|
|
}
|
|
log.Println("requestpayload", requestPayload)
|
|
|
|
// insert data
|
|
event := data.LogEntry{
|
|
Name: requestPayload.Name,
|
|
Data: requestPayload.Data,
|
|
}
|
|
|
|
log.Println("event", event)
|
|
|
|
err = app.Models.LogEntry.Insert(event)
|
|
if err != nil {
|
|
app.errorJSON(w, err)
|
|
return
|
|
}
|
|
|
|
resp := jsonResponse{
|
|
Error: false,
|
|
Message: "logged",
|
|
}
|
|
|
|
app.writeJSON(w, http.StatusAccepted, resp)
|
|
}
|