Skip to content

Commit

Permalink
comments to English
Browse files Browse the repository at this point in the history
  • Loading branch information
fanhousanbu committed Feb 29, 2024
1 parent 9813aac commit 3ad0e96
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 31 deletions.
2 changes: 1 addition & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

var aPort = flag.String("port", "", "端口")

// runMode 获取运行模式
// runMode running mode
// @string: 端口
func runMode() string {
// 优先读取命令行参数,其次使用go env,最后使用默认值
Expand Down
7 changes: 2 additions & 5 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ type Conf struct {

var conf *Conf

// getConf 读取配置
// 默认从配置文件取,如果配置文件中的db节点内容为空,则从环境变量取
// 如果配置文件不存在,则db从环境变量取,其他值使用默认值
// getConf read conf from file
func getConf() *Conf {
once.Do(func() {
if conf == nil {
Expand All @@ -27,8 +25,7 @@ func getConf() *Conf {
return conf
}

// getConfiguration 读取配置
// 从配置文件读取,如果环境变量存在对应值,则取环境变量值
// getConfiguration
func getConfiguration(filePath *string) *Conf {
if file, err := os.ReadFile(*filePath); err != nil {
return mappingEnvToConf(nil)
Expand Down
2 changes: 1 addition & 1 deletion rpc_server/middlewares/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package middlewares
import "github.com/gin-gonic/gin"
import "github.com/gin-contrib/cors"

// CorsHandler 跨域处理中间件
// CorsHandler cross domain handler
func CorsHandler() gin.HandlerFunc {
return cors.Default()
}
5 changes: 2 additions & 3 deletions rpc_server/middlewares/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (
"strings"
)

// GenericRecovery 通用错误 (panic) 拦截中间件、对可能发生的错误进行拦截、统一记录
// GenericRecovery represents the generic error(panic) process
func GenericRecovery() gin.HandlerFunc {
DefaultErrorWriter := &PanicExceptionRecord{}
return gin.RecoveryWithWriter(DefaultErrorWriter, func(c *gin.Context, err interface{}) {
// 这里针对发生的panic等异常进行统一响应即
errStr := ""
if conf.Environment.Debugger {
errStr = fmt.Sprintf("%v", err)
Expand All @@ -23,7 +22,7 @@ func GenericRecovery() gin.HandlerFunc {
})
}

// PanicExceptionRecord panic等异常记录
// PanicExceptionRecord represents the record of panic exception
type PanicExceptionRecord struct{}

func (p *PanicExceptionRecord) Write(b []byte) (n int, err error) {
Expand Down
18 changes: 9 additions & 9 deletions rpc_server/models/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ type Response struct {
Result *Result
}

// Fail 错误返回
// Fail represents 5XX error
func (r *Response) Fail(ctx *gin.Context) *Response {
r.SetCode(http.StatusInternalServerError)
r.json(ctx)
return r
}

// FailCode 自定义错误码返回
// FailCode customer error codes
func (r *Response) FailCode(ctx *gin.Context, code int, msg ...string) *Response {
r.SetCode(code)
if msg != nil {
Expand All @@ -74,14 +74,14 @@ func (r *Response) FailCode(ctx *gin.Context, code int, msg ...string) *Response
return r
}

// Success 正确返回
// Success represents the success response
func (r *Response) Success(ctx *gin.Context) *Response {
r.SetCode(http.StatusOK)
r.json(ctx)
return r
}

// WithDataSuccess 成功后需要返回值
// WithDataSuccess return success with data
func (r *Response) WithDataSuccess(ctx *gin.Context, data interface{}) *Response {
r.SetCode(http.StatusOK)
r.WithData(data)
Expand All @@ -98,13 +98,13 @@ func (r *Response) withDataAndHttpCode(code int, ctx *gin.Context, data interfac
return r
}

// SetCode 设置返回code码
// SetCode set business code
func (r *Response) SetCode(code int) *Response {
r.Result.Code = code
return r
}

// SetHttpCode 设置http状态码
// SetHttpCode set http status code
func (r *Response) SetHttpCode(code int) *Response {
r.httpCode = code
return r
Expand All @@ -114,7 +114,7 @@ type defaultRes struct {
Result any `json:"result"`
}

// WithData 设置返回data数据
// WithData represents response with data
func (r *Response) WithData(data interface{}) *Response {
switch data.(type) {
case string, int, bool:
Expand All @@ -125,13 +125,13 @@ func (r *Response) WithData(data interface{}) *Response {
return r
}

// WithMessage 设置返回自定义错误消息
// WithMessage represents returns response with message
func (r *Response) WithMessage(message string) *Response {
r.Result.Message = message
return r
}

// json 返回 gin 框架的 HandlerFunc
// json returns HandlerFunc
func (r *Response) json(ctx *gin.Context) {
r.Result.Cost = time.Since(ctx.GetTime("requestStartTime")).String()
ctx.AbortWithStatusJSON(r.httpCode, r.Result)
Expand Down
22 changes: 11 additions & 11 deletions rpc_server/routers/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,34 @@ import (
"net/http"
)

// SetRouters 设置API路由
// SetRouters set routers
func SetRouters() (routers *gin.Engine) {
routers = gin.New()

// 中间件
// middlewares
handlers := make([]gin.HandlerFunc, 0)
handlers = append(handlers, middlewares.GenericRecovery())
if conf.Environment.IsDevelopment() {
handlers = append(handlers, gin.Logger())
}
handlers = append(handlers, middlewares.CorsHandler())

// 生产模式配置
// prod mode
if conf.Environment.IsProduction() {
gin.SetMode(gin.ReleaseMode) // 生产模式
gin.DefaultWriter = io.Discard // 禁用 gin 输出接口访问日志
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = io.Discard // disable gin log
}

// 开发模式配置
// dev mod
if conf.Environment.IsDevelopment() {
gin.SetMode(gin.DebugMode) // 调试模式
buildSwagger(routers) // 构建swagger
gin.SetMode(gin.DebugMode)
buildSwagger(routers)
}

// 加载中间件
// use middlewares
routers.Use(handlers...)

buildRouters(routers) // 构建http路由
buildRouters(routers) // build http routers

routers.NoRoute(func(ctx *gin.Context) {
models.GetResponse().SetHttpCode(http.StatusNotFound).FailCode(ctx, http.StatusNotFound)
Expand All @@ -48,7 +48,7 @@ func SetRouters() (routers *gin.Engine) {
return
}

// buildSwagger 创建swagger文档
// buildSwagger build swagger
func buildSwagger(router *gin.Engine) {
docs.SwaggerInfo.BasePath = "/"
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
Expand Down
2 changes: 1 addition & 1 deletion rpc_server/routers/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/gin-gonic/gin"
)

// buildRouters 构建路由表
// buildRouters Build Routers
func buildRouters(router *gin.Engine) {

for _, routerMap := range RouterMaps {
Expand Down

0 comments on commit 3ad0e96

Please sign in to comment.