logger auth error

This commit is contained in:
vinchent 2024-08-31 22:56:25 +02:00
parent a0eedd4250
commit be71cc40d1
6 changed files with 79 additions and 10 deletions

View File

@ -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
}

View File

@ -57,8 +57,9 @@ func (app *Config) authenticate(w http.ResponseWriter, a AuthPayload) {
Input: a,
Addr: "http://authentication-service/authenticate",
ErrCode: statusError{
Code: http.StatusUnauthorized,
Err: errors.New("invalid credentials"),
ExpectedCode: http.StatusAccepted,
ErrCode: http.StatusUnauthorized,
Err: errors.New("invalid credentials"),
},
SuccessMsg: "Authenticated",
}
@ -66,12 +67,14 @@ func (app *Config) authenticate(w http.ResponseWriter, a AuthPayload) {
}
func (app *Config) LogItem(w http.ResponseWriter, entry LogPayload) {
log.Println(entry)
loggerService := Microservice{
Input: entry,
Addr: "http://logger-service/log",
ErrCode: statusError{
Code: http.StatusInternalServerError,
Err: errors.New("internal error"),
ExpectedCode: http.StatusAccepted,
ErrCode: http.StatusInternalServerError,
Err: errors.New("internal error"),
},
SuccessMsg: "Logged!",
}
@ -79,8 +82,9 @@ func (app *Config) LogItem(w http.ResponseWriter, entry LogPayload) {
}
type statusError struct {
Code int
Err error
ExpectedCode int
ErrCode int
Err error
}
type Microservice struct {
@ -97,6 +101,7 @@ func (app *Config) callService(w http.ResponseWriter, ms Microservice) {
app.errorJSON(w, err, http.StatusBadRequest)
return
}
log.Println(ms.Input)
// call the service
req, err := http.NewRequest(
@ -120,8 +125,8 @@ func (app *Config) callService(w http.ResponseWriter, ms Microservice) {
log.Println(resp.Body)
// make sure we get back the correct status code
if resp.StatusCode != http.StatusAccepted {
app.errorJSON(w, ms.ErrCode.Err, ms.ErrCode.Code)
if resp.StatusCode != ms.ErrCode.ExpectedCode {
app.errorJSON(w, ms.ErrCode.Err, ms.ErrCode.ErrCode)
return
}

View File

@ -1,6 +1,7 @@
package main
import (
"log"
"logger/data"
"net/http"
)
@ -18,6 +19,7 @@ func (app *Config) WriteLog(w http.ResponseWriter, r *http.Request) {
app.errorJSON(w, err)
return
}
log.Println("requestpayload", requestPayload)
// insert data
event := data.LogEntry{
@ -25,6 +27,8 @@ func (app *Config) WriteLog(w http.ResponseWriter, r *http.Request) {
Data: requestPayload.Data,
}
log.Println("event", event)
err = app.Models.LogEntry.Insert(event)
if err != nil {
app.errorJSON(w, err)

View File

@ -8,6 +8,7 @@ import (
"net/http"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
@ -42,6 +43,8 @@ func main() {
}
}()
client = mongoClient
app := Config{
Models: data.New(client),
}
@ -75,6 +78,12 @@ func connectToMongo() (*mongo.Client, error) {
return nil, err
}
var result bson.M
if err = client.Database("Test").RunCommand(context.TODO(), bson.D{{Key: "ping", Value: 1}}).Decode(&result); err != nil {
log.Println("Error ping:", err)
return nil, err
}
log.Println("Connected to Mongo")
return client, nil

View File

@ -35,13 +35,15 @@ type LogEntry struct {
func (l *LogEntry) Insert(entry LogEntry) error {
collection := client.Database("logs").Collection("logs")
_, err := collection.InsertOne(context.TODO(), LogEntry{
newLog := LogEntry{
Name: entry.Name,
Data: entry.Data,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
})
}
_, err := collection.InsertOne(context.TODO(), newLog)
if err != nil {
log.Println("Insert error:", err)
return err
}
return nil

View File

@ -62,3 +62,14 @@ services:
restart: always
ports:
- 8090:8080
mongo-express:
image: mongo-express
restart: always
ports:
- 8091:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: password
ME_CONFIG_MONGODB_URL: mongodb://mongo:27017/
ME_CONFIG_BASICAUTH: false