diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8663cad --- /dev/null +++ b/Dockerfile @@ -0,0 +1,36 @@ +# FROM 基于 golang:1.15-alpine +FROM golang:1.15-alpine AS builder + +# ENV 设置环境变量 +ENV GOPATH=/opt/repo +ENV GO111MODULE=on +ENV GOPROXY=https://goproxy.io,direct + +# ADD 源路径 目标路径 +COPY . $GOPATH/src/github.com/xinliangnote/go-gin-api + +# RUN 执行 go build . +RUN cd $GOPATH/src/github.com/xinliangnote/go-gin-api && go build . + +# FROM 基于 alpine:latest +FROM alpine:latest + +# RUN 设置代理镜像 +RUN echo -e http://mirrors.ustc.edu.cn/alpine/v3.13/main/ > /etc/apk/repositories + +# RUN 设置 Asia/Shanghai 时区 +RUN apk --no-cache add tzdata && \ + ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ + echo "Asia/Shanghai" > /etc/timezone + +# COPY 源路径 目标路径 从镜像中 COPY +COPY --from=builder /opt/repo/src/github.com/xinliangnote/go-gin-api /opt + +# EXPOSE 设置端口映射 +EXPOSE 9999/tcp + +# WORKDIR 设置工作目录 +WORKDIR /opt + +# CMD 设置启动命令 +CMD ["./go-gin-api", "-env", "fat"] \ No newline at end of file diff --git a/cmd/mysqlmd/mysql/mysql.go b/cmd/mysqlmd/mysql/mysql.go index 74a46ff..0188d37 100644 --- a/cmd/mysqlmd/mysql/mysql.go +++ b/cmd/mysqlmd/mysql/mysql.go @@ -3,7 +3,8 @@ package mysql import ( "fmt" - "github.com/pkg/errors" + "github.com/xinliangnote/go-gin-api/pkg/errors" + "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/schema" diff --git a/configs/constants.go b/configs/constants.go index 7a4b69a..cfd793b 100644 --- a/configs/constants.go +++ b/configs/constants.go @@ -1,36 +1,36 @@ package configs const ( - // 项目版本 + // ProjectVersion 项目版本 ProjectVersion = "v1.2.6" - // 项目名称 + // ProjectName 项目名称 ProjectName = "go-gin-api" - // 项目端口 + // ProjectPort 项目端口 ProjectPort = ":9999" - // 项目日志存放文件 + // ProjectLogFile 项目日志存放文件 ProjectLogFile = "./logs/" + ProjectName + "-access.log" - // 项目安装完成标识 + // ProjectInstallMark 项目安装完成标识 ProjectInstallMark = "INSTALL.lock" - // 登录验证 Token,Header 中传递的参数 + // HeaderLoginToken 登录验证 Token,Header 中传递的参数 HeaderLoginToken = "Token" - // 签名验证 Token,Header 中传递的参数 + // HeaderSignToken 签名验证 Token,Header 中传递的参数 HeaderSignToken = "Authorization" - // 签名验证 Date,Header 中传递的参数 + // HeaderSignTokenDate 签名验证 Date,Header 中传递的参数 HeaderSignTokenDate = "Authorization-Date" - // Redis Key 前缀 - 防止重复提交 + // RedisKeyPrefixRequestID Redis Key 前缀 - 防止重复提交 RedisKeyPrefixRequestID = ProjectName + ":request-id:" - // Redis Key 前缀 - 登录用户信息 + // RedisKeyPrefixLoginUser Redis Key 前缀 - 登录用户信息 RedisKeyPrefixLoginUser = ProjectName + ":login-user:" - // Redis Key 前缀 - 签名验证信息 + // RedisKeyPrefixSignature Redis Key 前缀 - 签名验证信息 RedisKeyPrefixSignature = ProjectName + ":signature:" ) diff --git a/internal/api/code/code.go b/internal/api/code/code.go index e306273..cd8e1fb 100644 --- a/internal/api/code/code.go +++ b/internal/api/code/code.go @@ -1,6 +1,6 @@ package code -// 错误时返回结构 +// Failure 错误时返回结构 type Failure struct { Code int `json:"code"` // 业务码 Message string `json:"message"` // 描述信息 diff --git a/internal/graph/resolvers/user.go b/internal/graph/resolvers/user.go index b5818e8..20a3633 100644 --- a/internal/graph/resolvers/user.go +++ b/internal/graph/resolvers/user.go @@ -4,8 +4,7 @@ import ( "context" "github.com/xinliangnote/go-gin-api/internal/graph/model" - - "github.com/pkg/errors" + "github.com/xinliangnote/go-gin-api/pkg/errors" ) func (r *queryResolver) BySex(ctx context.Context, sex string) ([]*model.User, error) { diff --git a/internal/pkg/core/core.go b/internal/pkg/core/core.go index 45d036e..c812baa 100644 --- a/internal/pkg/core/core.go +++ b/internal/pkg/core/core.go @@ -14,11 +14,11 @@ import ( "github.com/xinliangnote/go-gin-api/pkg/color" "github.com/xinliangnote/go-gin-api/pkg/env" "github.com/xinliangnote/go-gin-api/pkg/errno" + "github.com/xinliangnote/go-gin-api/pkg/errors" "github.com/xinliangnote/go-gin-api/pkg/trace" "github.com/gin-contrib/pprof" "github.com/gin-gonic/gin" - "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus/promhttp" cors "github.com/rs/cors/wrapper/gin" ginSwagger "github.com/swaggo/gin-swagger" diff --git a/internal/pkg/db/mysql.go b/internal/pkg/db/mysql.go index e238097..acfef78 100644 --- a/internal/pkg/db/mysql.go +++ b/internal/pkg/db/mysql.go @@ -5,8 +5,8 @@ import ( "time" "github.com/xinliangnote/go-gin-api/configs" + "github.com/xinliangnote/go-gin-api/pkg/errors" - "github.com/pkg/errors" "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/schema" diff --git a/internal/router/middleware/middle_resubmit.go b/internal/router/middleware/middle_resubmit.go index decee46..662ba3d 100644 --- a/internal/router/middleware/middle_resubmit.go +++ b/internal/router/middleware/middle_resubmit.go @@ -9,9 +9,8 @@ import ( "github.com/xinliangnote/go-gin-api/internal/pkg/cache" "github.com/xinliangnote/go-gin-api/internal/pkg/core" "github.com/xinliangnote/go-gin-api/pkg/errno" + "github.com/xinliangnote/go-gin-api/pkg/errors" "github.com/xinliangnote/go-gin-api/pkg/token" - - "github.com/pkg/errors" ) const reSubmitMark = "1" diff --git a/internal/web/controller/generator_handler/func_gormexecute.go b/internal/web/controller/generator_handler/func_gormexecute.go index be8dba5..bfed5f4 100644 --- a/internal/web/controller/generator_handler/func_gormexecute.go +++ b/internal/web/controller/generator_handler/func_gormexecute.go @@ -27,11 +27,13 @@ func (h *handler) GormExecute() core.HandlerFunc { mysqlConf := configs.Get().MySQL.Read shellPath := fmt.Sprintf("%s %s %s %s %s %s", gormgenSh, mysqlConf.Addr, mysqlConf.User, mysqlConf.Pass, mysqlConf.Name, req.Tables) - // runtime.GOOS = linux or darwin - command := exec.Command("/bin/bash", "-c", shellPath) + command := new (exec.Cmd) if runtime.GOOS == "windows" { command = exec.Command("cmd", "/C", shellPath) + } else { + // runtime.GOOS = linux or darwin + command = exec.Command("/bin/bash", "-c", shellPath) } var stderr bytes.Buffer diff --git a/internal/web/controller/generator_handler/func_handlerexecute.go b/internal/web/controller/generator_handler/func_handlerexecute.go index a4a3ea3..6a82102 100644 --- a/internal/web/controller/generator_handler/func_handlerexecute.go +++ b/internal/web/controller/generator_handler/func_handlerexecute.go @@ -25,11 +25,13 @@ func (h *handler) HandlerExecute() core.HandlerFunc { shellPath := fmt.Sprintf("%s %s", handlergenSh, req.Name) - // runtime.GOOS = linux or darwin - command := exec.Command("/bin/bash", "-c", shellPath) + command := new (exec.Cmd) if runtime.GOOS == "windows" { command = exec.Command("cmd", "/C", shellPath) + } else { + // runtime.GOOS = linux or darwin + command = exec.Command("/bin/bash", "-c", shellPath) } var stderr bytes.Buffer diff --git a/internal/web/controller/install_handler/func_restart.go b/internal/web/controller/install_handler/func_restart.go index 726b6d1..781f5d6 100644 --- a/internal/web/controller/install_handler/func_restart.go +++ b/internal/web/controller/install_handler/func_restart.go @@ -12,11 +12,13 @@ func (h *handler) Restart() core.HandlerFunc { return func(c core.Context) { shellPath := "./scripts/restart.sh" - // runtime.GOOS = linux or darwin - command := exec.Command("/bin/bash", "-c", shellPath) + command := new (exec.Cmd) if runtime.GOOS == "windows" { command = exec.Command("cmd", "/C", shellPath) + } else { + // runtime.GOOS = linux or darwin + command = exec.Command("/bin/bash", "-c", shellPath) } var stderr bytes.Buffer diff --git a/pkg/ddm/type.go b/pkg/ddm/type.go index 5a25842..1464763 100644 --- a/pkg/ddm/type.go +++ b/pkg/ddm/type.go @@ -1,21 +1,21 @@ package ddm -// 手机号 132****7986 +// Mobile 手机号 132****7986 type Mobile string -// 银行卡号 622888******5676 +// BankCard 银行卡号 622888******5676 type BankCard string -// 身份证号 1******7 +// IDCard 身份证号 1******7 type IDCard string -// 姓名 *鸿章 +// IDName 姓名 *鸿章 // TODO:参考 https://blog.thinkeridea.com/201910/go/efficient_string_truncation.html // Deprecated:有更好的性能选择 type IDName string -// 密码 ****** +// PassWord 密码 ****** type PassWord string -// 邮箱 l***w@gmail.com +// Email 邮箱 l***w@gmail.com type Email string diff --git a/pkg/errno/errno.go b/pkg/errno/errno.go index 0f237d6..a0015d8 100644 --- a/pkg/errno/errno.go +++ b/pkg/errno/errno.go @@ -2,8 +2,6 @@ package errno import ( "encoding/json" - - "github.com/pkg/errors" ) var _ Error = (*err)(nil) @@ -43,7 +41,7 @@ func NewError(httpCode, businessCode int, msg string) Error { func (e *err) i() {} func (e *err) WithErr(err error) Error { - e.Err = errors.WithStack(err) + e.Err = err return e } diff --git a/pkg/errors/err.go b/pkg/errors/err.go index aa416ae..935e367 100644 --- a/pkg/errors/err.go +++ b/pkg/errors/err.go @@ -20,8 +20,8 @@ type Error interface { t() } -var _ (Error) = (*item)(nil) -var _ (fmt.Formatter) = (*item)(nil) +var _ Error = (*item)(nil) +var _ fmt.Formatter = (*item)(nil) type item struct { msg string diff --git a/pkg/hash/hash.go b/pkg/hash/hash.go index e708b76..8072acb 100644 --- a/pkg/hash/hash.go +++ b/pkg/hash/hash.go @@ -5,8 +5,10 @@ var _ Hash = (*hash)(nil) type Hash interface { i() - // hashids + // HashidsEncode 加密 HashidsEncode(params []int) (string, error) + + // HashidsDecode 解密 HashidsDecode(hash string) ([]int, error) } diff --git a/pkg/httpclient/client.go b/pkg/httpclient/client.go index 7872316..87ac350 100644 --- a/pkg/httpclient/client.go +++ b/pkg/httpclient/client.go @@ -8,9 +8,8 @@ import ( httpURL "net/url" "time" + "github.com/xinliangnote/go-gin-api/pkg/errors" "github.com/xinliangnote/go-gin-api/pkg/trace" - - "github.com/pkg/errors" ) const ( diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index 7225384..44c1797 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -108,7 +108,7 @@ func WithTimeLayout(timeLayout string) Option { } } -// WithEnableConsole write log to os.Stdout or os.Stderr +// WithDisableConsole WithEnableConsole write log to os.Stdout or os.Stderr func WithDisableConsole() Option { return func(opt *option) { opt.disableConsole = true diff --git a/pkg/signature/signature.go b/pkg/signature/signature.go index e5efd80..f3d5058 100644 --- a/pkg/signature/signature.go +++ b/pkg/signature/signature.go @@ -28,10 +28,10 @@ var methods = map[string]bool{ type Signature interface { i() - // 生成签名 + // Generate 生成签名 Generate(path string, method string, params url.Values) (authorization, date string, err error) - // 验证签名 + // Verify 验证签名 Verify(authorization, date string, path string, method string, params url.Values) (ok bool, err error) } diff --git a/pkg/signature/signature_generate.go b/pkg/signature/signature_generate.go index 02326d5..3ba0228 100644 --- a/pkg/signature/signature_generate.go +++ b/pkg/signature/signature_generate.go @@ -9,9 +9,8 @@ import ( "net/url" "strings" + "github.com/xinliangnote/go-gin-api/pkg/errors" "github.com/xinliangnote/go-gin-api/pkg/time_parse" - - "github.com/pkg/errors" ) // Generate diff --git a/pkg/signature/signature_verify.go b/pkg/signature/signature_verify.go index 1682747..3ca8abc 100644 --- a/pkg/signature/signature_verify.go +++ b/pkg/signature/signature_verify.go @@ -10,9 +10,8 @@ import ( "strings" "time" + "github.com/xinliangnote/go-gin-api/pkg/errors" "github.com/xinliangnote/go-gin-api/pkg/time_parse" - - "github.com/pkg/errors" ) func (s *signature) Verify(authorization, date string, path string, method string, params url.Values) (ok bool, err error) {