logger auth error
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
@ -30,6 +32,15 @@ func (app *Config) Authenticate(w http.ResponseWriter, r *http.Request) {
|
||||
app.errorJSON(w, errors.New("invalid credentials"), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// log authentication
|
||||
|
||||
err = app.logRequest("authentication", fmt.Sprintf("%s is logged in", user.Email))
|
||||
if err != nil {
|
||||
app.errorJSON(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
payload := jsonResponse{
|
||||
Error: false,
|
||||
Message: fmt.Sprintf("%s %s is authorized to log in.", user.FirstName, user.LastName),
|
||||
@ -38,3 +49,30 @@ func (app *Config) Authenticate(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
app.writeJSON(w, http.StatusAccepted, payload)
|
||||
}
|
||||
|
||||
func (app *Config) logRequest(name, data string) error {
|
||||
var entry struct {
|
||||
Name string `json:"name"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
entry.Name = name
|
||||
entry.Data = data
|
||||
|
||||
jsonData, _ := json.MarshalIndent(entry, "", "\t")
|
||||
|
||||
logServiceURL := "http://logger-service/log"
|
||||
|
||||
request, err := http.NewRequest("POST", logServiceURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
request.Header.Add("Content-Type", "application/json")
|
||||
client := &http.Client{}
|
||||
_, err = client.Do(request)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user