logger auth error
This commit is contained in:
		@ -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)
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user