-
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.
Merge pull request #4 from OVINC-CN/feat_server
feat: 增加超时配置&增加数据库连接配置&优化数据库日志
- Loading branch information
Showing
13 changed files
with
220 additions
and
76 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
.idea | ||
*.log |
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 |
---|---|---|
@@ -1,19 +1,29 @@ | ||
package configs | ||
|
||
import "github.com/OVINC-CN/DevTemplateGo/src/utils" | ||
import ( | ||
"github.com/OVINC-CN/DevTemplateGo/src/utils" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type dbConfigModel struct { | ||
Host string | ||
Port string | ||
User string | ||
Password string | ||
Name string | ||
Host string | ||
Port string | ||
User string | ||
Password string | ||
Name string | ||
MaxConnections int | ||
ConnectionTimeOut time.Duration | ||
SlowThreshold time.Duration | ||
} | ||
|
||
var DBConfig = dbConfigModel{ | ||
Host: utils.GetEnv("DB_HOST", "127.0.0.1"), | ||
Port: utils.GetEnv("DB_PORT", "3306"), | ||
User: utils.GetEnv("DB_USER", ""), | ||
Password: utils.GetEnv("DB_PASSWORD", ""), | ||
Name: utils.GetEnv("DB_NAME", ""), | ||
Host: utils.GetEnv("DB_HOST", "127.0.0.1"), | ||
Port: utils.GetEnv("DB_PORT", "3306"), | ||
User: utils.GetEnv("DB_USER", ""), | ||
Password: utils.GetEnv("DB_PASSWORD", ""), | ||
Name: utils.GetEnv("DB_NAME", ""), | ||
MaxConnections: utils.StrToInt(utils.GetEnv("DB_MAX_CONNECTIONS", "10")), | ||
ConnectionTimeOut: time.Duration(utils.StrToInt(utils.GetEnv("DB_CONNECTION_TIMEOUT", strconv.Itoa(60*60)))) * time.Second, | ||
SlowThreshold: time.Duration(utils.StrToInt(utils.GetEnv("DB_SLOW_THRESHOLD", strconv.Itoa(100)))) * time.Millisecond, | ||
} |
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
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,38 +1,26 @@ | ||
package middlewares | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"github.com/OVINC-CN/DevTemplateGo/src/services/account" | ||
"github.com/OVINC-CN/DevTemplateGo/src/utils" | ||
"github.com/gin-gonic/gin" | ||
"io" | ||
"time" | ||
) | ||
|
||
func RequestLogger() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
// 初始化请求时间 | ||
t := time.Now() | ||
// 提取请求体 | ||
requestRawData, err := c.GetRawData() | ||
c.Request.Body = io.NopCloser(bytes.NewReader(requestRawData)) | ||
if err != nil { | ||
utils.ContextWarningf(c, "[LoadRequestBodyFailed] %s", err) | ||
} | ||
// 执行 | ||
c.Next() | ||
// 记录请求耗时 | ||
duration := time.Since(t).Milliseconds() | ||
// 解析请求体 | ||
var requestJsonData map[string]interface{} | ||
if len(requestRawData) > 0 { | ||
if err = json.Unmarshal(requestRawData, &requestJsonData); err != nil { | ||
requestRawData = []byte("-") | ||
} | ||
} else { | ||
requestRawData = []byte("-") | ||
// 记录用户 | ||
username := account.GetContextUser(c).Username | ||
if username == "" { | ||
username = "-" | ||
} | ||
// 记录请求日志 | ||
utils.ContextInfof(c, "[RequestLog] (%dms) %s %s %s %d", duration, c.Request.Method, c.Request.URL, requestRawData, c.Writer.Status()) | ||
utils.ContextInfof(c, "[RequestLog] %s %s %s %d %d", username, c.Request.Method, c.Request.URL, duration, c.Writer.Status()) | ||
} | ||
} |
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,20 @@ | ||
package middlewares | ||
|
||
import ( | ||
"github.com/OVINC-CN/DevTemplateGo/src/configs" | ||
"github.com/gin-contrib/timeout" | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
) | ||
|
||
func Timeout() gin.HandlerFunc { | ||
return timeout.New( | ||
timeout.WithTimeout(configs.Config.RequestTimeout), | ||
timeout.WithHandler(func(c *gin.Context) { | ||
c.Next() | ||
}), | ||
timeout.WithResponse(func(c *gin.Context) { | ||
c.AbortWithStatusJSON(http.StatusGatewayTimeout, gin.H{"error": "timeout"}) | ||
}), | ||
) | ||
} |
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
Oops, something went wrong.