-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: daz-3ux <[email protected]>
- Loading branch information
Showing
8 changed files
with
132 additions
and
0 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
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
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
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 @@ | ||
## known | ||
|
||
- 放置需要共享的 key | ||
- X-Request-ID 会同时被 `日志` 以及 `gin.Context` 需要 | ||
- 所以将 key 的名字保存在共享包 `known` 中, 便于使用 |
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,11 @@ | ||
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. The original repo for | ||
// this file is https://github.com/Daz-3ux/dBlog. | ||
|
||
package known | ||
|
||
const ( | ||
// XRequestIDKey is used to define the key in the Gin context representing the request UUID. | ||
XRequestIDKey = "X-Request-Id" | ||
) |
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
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,48 @@ | ||
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. The original repo for | ||
// this file is https://github.com/Daz-3ux/dBlog. | ||
|
||
package middleware | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
// NoCache is a Gin middleware used to disable client-side caching of HTTP request response | ||
func NoCache(c *gin.Context) { | ||
c.Header("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate, value") | ||
c.Header("Expires", "Thu, 01 Jan 1970 00:00:00 GMT") | ||
c.Header("Last-Modified", time.Now().UTC().Format(http.TimeFormat)) | ||
c.Next() | ||
} | ||
|
||
// Cors is a Gin middleware used to set the headers for OPTIONS requests | ||
// then exit the middleware chain and complete the request (for handling browser cross-origin requests). | ||
func Cors(c *gin.Context) { | ||
if c.Request.Method != "OPTIONS" { | ||
c.Next() | ||
} else { | ||
c.Header("Access-Control-Allow-Origin", "*") | ||
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS") | ||
c.Header("Access-Control-Allow-Headers", "authorization, origin, content-type, accept") | ||
c.Header("Access-Control-Allow-Credentials", "false") | ||
c.Header("Access-Control-Max-Age", "600") | ||
c.Header("ALLOW", "HEAD, GET, POST, PUT, PATCH, DELETE, OPTIONS") | ||
c.Header("Content-Type", "application/json") | ||
c.AbortWithStatus(http.StatusNoContent) | ||
} | ||
} | ||
|
||
// Secure is a Gin middleware used to add HTTP headers related to security and resource access | ||
func Secure(c *gin.Context) { | ||
c.Header("Access-Control-Allow-Origin", "*") | ||
c.Header("X-Frame-Options", "DENY") | ||
c.Header("X-Content-Type-Options", "nosniff") | ||
c.Header("X-XSS-Protection", "1; mode=block") | ||
if c.Request.TLS != nil { | ||
c.Header("Strict-Transport-Security", "max-age=31536000") | ||
} | ||
} |
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,33 @@ | ||
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. The original repo for | ||
// this file is https://github.com/Daz-3ux/dBlog. | ||
|
||
package middleware | ||
|
||
import ( | ||
"github.com/Daz-3ux/dBlog/internal/pkg/known" | ||
"github.com/gin-gonic/gin" | ||
"github.com/google/uuid" | ||
) | ||
|
||
func RequestID() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
// Get the request id from the Gin context | ||
requestID := c.Request.Header.Get(known.XRequestIDKey) | ||
|
||
// If the request id is empty, set a new one | ||
if requestID == "" { | ||
requestID = uuid.New().String() | ||
} | ||
|
||
// Set the request id to the Gin context | ||
c.Set(known.XRequestIDKey, requestID) | ||
|
||
// Set the request id to the response header | ||
c.Writer.Header().Set(known.XRequestIDKey, requestID) | ||
|
||
// Continue | ||
c.Next() | ||
} | ||
} |