-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor code: request/response logging middleware, entity, standard …
…logging for troubleshooting
- Loading branch information
1 parent
ef6aa27
commit 9adc363
Showing
20 changed files
with
215 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package common | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
func PrettifyJSON(input string) (string, error) { | ||
// First unmarshal the input string into a generic interface{} | ||
var temp interface{} | ||
err := json.Unmarshal([]byte(input), &temp) | ||
if err != nil { | ||
return "", fmt.Errorf("invalid JSON input: %v", err) | ||
} | ||
|
||
// Marshal back to JSON with indentation | ||
pretty, err := json.MarshalIndent(temp, "", " ") | ||
if err != nil { | ||
return "", fmt.Errorf("failed to marshal JSON: %v", err) | ||
} | ||
|
||
return string(pretty), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package entity | ||
|
||
import "registry-backend/ent" | ||
|
||
// NodeFilter holds optional parameters for filtering node results | ||
type NodeFilter struct { | ||
PublisherID string | ||
Search string | ||
IncludeBanned bool | ||
} | ||
|
||
// ListNodesResult is the structure that holds the paginated result of nodes | ||
type ListNodesResult struct { | ||
Total int `json:"total"` | ||
Nodes []*ent.Node `json:"nodes"` | ||
Page int `json:"page"` | ||
Limit int `json:"limit"` | ||
TotalPages int `json:"totalPages"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package entity | ||
|
||
import ( | ||
"registry-backend/ent" | ||
"registry-backend/ent/schema" | ||
"time" | ||
) | ||
|
||
type NodeVersionFilter struct { | ||
NodeId string | ||
Status []schema.NodeVersionStatus | ||
IncludeStatusReason bool | ||
MinAge time.Duration | ||
PageSize int | ||
Page int | ||
} | ||
|
||
type ListNodeVersionsResult struct { | ||
Total int `json:"total"` | ||
NodeVersions []*ent.NodeVersion `json:"nodes"` | ||
Page int `json:"page"` | ||
Limit int `json:"limit"` | ||
TotalPages int `json:"totalPages"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package entity | ||
|
||
type PublisherFilter struct { | ||
UserID string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
// File: common/types.go | ||
|
||
package common | ||
package entity | ||
|
||
type InviteTokenStatus string | ||
|
||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package drip_authentication | ||
package authentication | ||
|
||
import ( | ||
"context" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package drip_authentication | ||
package authentication | ||
|
||
import ( | ||
"net/http" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package drip_authentication | ||
package authentication | ||
|
||
import ( | ||
"context" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package drip_authentication | ||
package authentication | ||
|
||
import ( | ||
"net/http" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package drip_authentication | ||
package authentication | ||
|
||
import ( | ||
"net/http" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package drip_authentication | ||
package authentication | ||
|
||
import ( | ||
"net/http" | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package drip_metric | ||
package metric | ||
|
||
import ( | ||
"context" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package drip_metric | ||
package metric | ||
|
||
import ( | ||
"context" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package middleware | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/labstack/echo/v4" | ||
echo_middleware "github.com/labstack/echo/v4/middleware" | ||
"github.com/rs/zerolog/log" | ||
"io" | ||
) | ||
|
||
func RequestLoggerMiddleware() echo.MiddlewareFunc { | ||
return echo_middleware.RequestLoggerWithConfig(echo_middleware.RequestLoggerConfig{ | ||
LogURI: true, | ||
LogStatus: true, | ||
LogValuesFunc: func(c echo.Context, v echo_middleware.RequestLoggerValues) error { | ||
// Read the request body for logging | ||
requestBody, err := io.ReadAll(c.Request().Body) | ||
if err != nil { | ||
log.Ctx(c.Request().Context()).Error().Err(err).Msg("Failed to read request body") | ||
return err | ||
} | ||
// Reset the body for further use | ||
c.Request().Body = io.NopCloser(bytes.NewReader(requestBody)) | ||
|
||
// Log request details including query parameters | ||
log.Ctx(c.Request().Context()). | ||
Info(). | ||
Str("Method", c.Request().Method). | ||
Str("Path", c.Path()). | ||
Str("QueryParams", fmt.Sprintf("%v", c.QueryParams())). | ||
Str("RequestBody", string(requestBody)). | ||
Str("Headers", fmt.Sprintf("%v", c.Request().Header)). | ||
Msg("Request received") | ||
return nil | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package middleware | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
// Custom response writer to capture response body | ||
type responseWriter struct { | ||
http.ResponseWriter | ||
body *bytes.Buffer | ||
} | ||
|
||
func (rw *responseWriter) Write(p []byte) (n int, err error) { | ||
// Capture the response body in the buffer | ||
n, err = rw.body.Write(p) | ||
if err != nil { | ||
return n, err | ||
} | ||
// Write to the actual ResponseWriter | ||
return rw.ResponseWriter.Write(p) | ||
} | ||
|
||
// ResponseLoggerMiddleware will log response details and errors. | ||
func ResponseLoggerMiddleware() echo.MiddlewareFunc { | ||
return func(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
// Create a custom response writer to capture the response body | ||
rw := &responseWriter{ | ||
ResponseWriter: c.Response().Writer, | ||
body: new(bytes.Buffer), | ||
} | ||
c.Response().Writer = rw | ||
|
||
// Call the next handler in the chain | ||
err := next(c) | ||
|
||
// Log any errors that occur during handling | ||
if err != nil { | ||
log.Ctx(c.Request().Context()). | ||
Error(). | ||
Err(err). | ||
Str("Method", c.Request().Method). | ||
Str("Path", c.Path()). | ||
Msg("Error occurred during request handling") | ||
} | ||
|
||
// Log the response details | ||
log.Ctx(c.Request().Context()). | ||
Info(). | ||
Int("Status", c.Response().Status). | ||
Str("ResponseBody", rw.body.String()). | ||
Str("ResponseHeaders", fmt.Sprintf("%v", c.Response().Header())). | ||
Msg("Response sent") | ||
|
||
return err | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package drip_middleware | ||
package middleware | ||
|
||
import ( | ||
"context" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.