50 lines
854 B
Go
50 lines
854 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"logger/data"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
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,
|
|
}
|
|
|
|
now := time.Now()
|
|
event.Data += fmt.Sprintf(" received date %d", now.UnixMicro())
|
|
|
|
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)
|
|
}
|