feat: split path into 2 variables (path+route)

This commit is contained in:
Samuel Berthe
2023-10-15 22:29:34 +02:00
parent 239c66b1eb
commit 1546b4083a
4 changed files with 11 additions and 31 deletions
+2 -26
View File
@@ -2,8 +2,6 @@ name: Lint
on:
push:
tags:
branches:
pull_request:
jobs:
@@ -17,28 +15,6 @@ jobs:
stable: false
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: latest
# Optional: working directory, useful for monorepos
working-directory: ./
# Optional: golangci-lint command line arguments.
args: --timeout 60s --max-same-issues 50
# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true
# Optional: if set to true then the action will use pre-installed Go.
# skip-go-installation: true
# Optional: if set to true then the action don't cache or restore ~/go/pkg.
# skip-pkg-cache: true
# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true
# optionally use a specific version of Go rather than the latest one
go_version: '1.21'
args: --timeout 120s --max-same-issues 50
+5 -5
View File
@@ -79,7 +79,7 @@ router.GET("/pong", func(c *gin.Context) {
router.Run(":1234")
// output:
// time=2023-04-10T14:00:0.000000Z level=INFO msg="Incoming request" status=200 method=GET path=/pong ip=127.0.0.1 latency=25.5µs user-agent=curl/7.77.0 time=2023-04-10T14:00:00.000Z
// time=2023-04-10T14:00:0.000000Z level=INFO msg="Incoming request" status=200 method=GET path=/pong route=/pong ip=127.0.0.1 latency=25.5µs user-agent=curl/7.77.0 time=2023-04-10T14:00:00.000Z
```
### Filters
@@ -158,7 +158,7 @@ router.GET("/pong", func(c *gin.Context) {
router.Run(":1234")
// output:
// time="2023-04-10 14:00:00" level=INFO msg="Incoming request" status=200 method=GET path=/pong ip=127.0.0.1 latency=25.5µs user-agent=curl/7.77.0 time="2023-04-10 14:00:00"
// time="2023-04-10 14:00:00" level=INFO msg="Incoming request" status=200 method=GET path=/pong route=/pong ip=127.0.0.1 latency=25.5µs user-agent=curl/7.77.0 time="2023-04-10 14:00:00"
```
### Using custom logger sub-group
@@ -188,7 +188,7 @@ router.GET("/pong", func(c *gin.Context) {
router.Run(":1234")
// output:
// time=2023-04-10T14:00:0.000000+02:00 level=INFO msg="Incoming request" http.status=200 http.method=GET http.path=/pong http.ip=127.0.0.1 http.latency=20.125µs http.user-agent=curl/7.77.0 time=2023-04-10T14:00:00.000+02:00
// time=2023-04-10T14:00:0.000000+02:00 level=INFO msg="Incoming request" http.status=200 http.method=GET http.path=/pong http.route=/pong http.ip=127.0.0.1 http.latency=20.125µs http.user-agent=curl/7.77.0 time=2023-04-10T14:00:00.000+02:00
```
### Add logger to a single route
@@ -215,7 +215,7 @@ router.GET("/pong", sloggin.New(logger), func(c *gin.Context) {
router.Run(":1234")
// output:
// time="2023-04-10 14:00:00" level=INFO msg="Incoming request" status=200 method=GET path=/pong ip=127.0.0.1 latency=25.5µs user-agent=curl/7.77.0 time="2023-04-10 14:00:00"
// time="2023-04-10 14:00:00" level=INFO msg="Incoming request" status=200 method=GET path=/pong route=/pong ip=127.0.0.1 latency=25.5µs user-agent=curl/7.77.0 time="2023-04-10 14:00:00"
```
### Adding custom attributes
@@ -249,7 +249,7 @@ router.GET("/pong", func(c *gin.Context) {
router.Run(":1234")
// output:
// time=2023-04-10T14:00:0.000000+02:00 level=INFO msg="Incoming request" environment=production server=gin/1.9.0 gin_mode=release server_start_time=2023-04-10T10:00:00.000+02:00 status=200 method=GET path=/pong ip=127.0.0.1 latency=25.5µs user-agent=curl/7.77.0 time=2023-04-10T14:00:00.000+02:00
// time=2023-04-10T14:00:0.000000+02:00 level=INFO msg="Incoming request" environment=production server=gin/1.9.0 gin_mode=release server_start_time=2023-04-10T10:00:00.000+02:00 status=200 method=GET path=/pong route=/pong ip=127.0.0.1 latency=25.5µs user-agent=curl/7.77.0 time=2023-04-10T14:00:00.000+02:00
```
### JSON output
+3
View File
@@ -38,6 +38,9 @@ func main() {
router.GET("/pong", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
router.GET("/pong/:id", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
logger.Info("Starting server")
if err := router.Run(":1234"); err != nil {
+1
View File
@@ -76,6 +76,7 @@ func NewWithConfig(logger *slog.Logger, config Config) gin.HandlerFunc {
slog.Int("status", c.Writer.Status()),
slog.String("method", c.Request.Method),
slog.String("path", path),
slog.String("route", c.FullPath()),
slog.String("ip", c.ClientIP()),
slog.Duration("latency", latency),
slog.String("user-agent", c.Request.UserAgent()),