udemy-go-microservices/logger-service/cmd/api/handlers.go

41 lines
670 B
Go

package main
import (
"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
}
// insert data
event := data.LogEntry{
Name: requestPayload.Name,
Data: requestPayload.Data,
}
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)
}