Skip to content

Commit

Permalink
chore: logging config
Browse files Browse the repository at this point in the history
  • Loading branch information
kayprogrammer committed Jan 26, 2025
1 parent b7980d3 commit 12fe168
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ func main() {
}

app.Use(swagger.New(swaggerCfg))
app.Use(routes.RequestLogger(db))

// Register Routes & Sockets
routes.SetupRoutes(app, db)

// RUN JOBS
jobs.RunJobs(conf, db)
log.Fatal(app.Listen(":" + conf.Port))
log.Fatal(app.Listen(":" + conf.Port))

}
12 changes: 12 additions & 0 deletions models/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package models

type Log struct {
BaseModel
StatusCode int
Method string
Path string
IP string
Params *string
ReqID string
Body []byte // Raw request body
}
40 changes: 33 additions & 7 deletions routes/middlewares.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package routes

import (
"encoding/json"
"strings"

"github.com/LitPad/backend/config"
Expand Down Expand Up @@ -96,33 +97,58 @@ func (ep Endpoint) WalletAccessMiddleware(c *fiber.Ctx) error {

token := c.Get("Access")

if len(token) < 1{
if len(token) < 1 {
return c.Status(403).JSON(utils.RequestErr(utils.ERR_NOT_ALLOWED, "Forbidden"))
}

if !strings.HasPrefix(token, "Litpad "){
if !strings.HasPrefix(token, "Litpad ") {
return c.Status(403).JSON(utils.RequestErr(utils.ERR_NOT_ALLOWED, "Forbidden"))
}

parsedToken, err := jwt.Parse(token[7:], func(t *jwt.Token) (interface{}, error) {
return []byte(conf.WalletSecret), nil
})

if err != nil{
if err != nil {
c.Status(500).JSON(utils.ERR_SERVER_ERROR)
}

if !parsedToken.Valid{
if !parsedToken.Valid {
c.Status(403).JSON(utils.RequestErr(utils.ERR_NOT_ALLOWED, "Forbidden"))
}

return c.Next()
}

func RequestLogger(db *gorm.DB) fiber.Handler {
return func(c *fiber.Ctx) error {
// Call the next middleware and capture response status after
err := c.Next()

// Serialize query parameters and route parameters
params, _ := json.Marshal(map[string]string{
"query": string(c.Request().URI().QueryString()),
"params": string(c.Params("*")), // captures all route parameters
})
paramsStr := string(params)
// Log details of the request
log := models.Log{
Method: c.Method(), Path: c.Path(), IP: c.IP(),
StatusCode: c.Response().StatusCode(), Body: c.Body(), Params: &paramsStr,
}
// Save log to the database
if dbErr := db.Create(&log).Error; dbErr != nil {
return dbErr // return error if logging fails
}

return err
}
}

func ParseUUID(input string) *uuid.UUID {
uuidVal, err := uuid.Parse(input)
uuidVal, err := uuid.Parse(input)
if err != nil {
return nil
}
return &uuidVal
}
return &uuidVal
}
2 changes: 0 additions & 2 deletions routes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (
"gorm.io/gorm"
)

var falsy = false

func ResponseMessage(message string) schemas.ResponseSchema {
return schemas.ResponseSchema{Status: "success", Message: message}
}
Expand Down

0 comments on commit 12fe168

Please sign in to comment.