优化
This commit is contained in:
parent
2bdc5e666b
commit
e952657166
@ -21,6 +21,7 @@ WORKDIR ${WORK_PATH}
|
||||
|
||||
# 从构建阶段复制二进制文件
|
||||
COPY --from=builder ${WORK_PATH}/main .
|
||||
COPY --from=0 ${WORK_PATH}/config/config.yaml .
|
||||
|
||||
# 暴露端口
|
||||
EXPOSE 80
|
||||
@ -29,4 +30,4 @@ EXPOSE 80
|
||||
# ENTRYPOINT ./main -c config.docker.toml
|
||||
|
||||
###############################################
|
||||
CMD ["./main"]
|
||||
CMD ["./main","-c","config.yaml"]
|
7
api/network/model/resp/network_resp.go
Normal file
7
api/network/model/resp/network_resp.go
Normal file
@ -0,0 +1,7 @@
|
||||
package resp
|
||||
|
||||
// HeadersResp 网络请求头响应
|
||||
type HeadersResp struct {
|
||||
IP string `json:"ip"`
|
||||
Headers map[string]string `json:"headers"`
|
||||
}
|
14
api/network/server/network_api.go
Normal file
14
api/network/server/network_api.go
Normal file
@ -0,0 +1,14 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"api/utils/r"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// NetWorkApi 网络接口
|
||||
type NetWorkApi struct{}
|
||||
|
||||
// GetHeaders 获取请求头
|
||||
func (*NetWorkApi) GetHeaders(c *gin.Context) {
|
||||
r.SuccessData(c, networkService.GetHeaders(c))
|
||||
}
|
7
api/network/server/z_enter.go
Normal file
7
api/network/server/z_enter.go
Normal file
@ -0,0 +1,7 @@
|
||||
package server
|
||||
|
||||
import "api/api/network/service"
|
||||
|
||||
var (
|
||||
networkService = service.NetWorkService{}
|
||||
)
|
30
api/network/service/network_service.go
Normal file
30
api/network/service/network_service.go
Normal file
@ -0,0 +1,30 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"api/api/network/model/resp"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// NetWorkService 网络服务
|
||||
type NetWorkService struct{}
|
||||
|
||||
func (NetWorkService) GetHeaders(c *gin.Context) resp.HeadersResp {
|
||||
// 获取所有的请求头
|
||||
request := c.Request
|
||||
headers := request.Header
|
||||
clientIP := c.ClientIP()
|
||||
|
||||
// 将请求头转换为 map[string]string
|
||||
headersMap := make(map[string]string)
|
||||
for key, value := range headers {
|
||||
headersMap[key] = value[0] // 注意,这里假设每个键只有一个值,你可以根据实际情况修改逻辑
|
||||
}
|
||||
headersMap["platform"] = headers.Get("Sec-CH-UA-Platform")
|
||||
headersMap["model"] = headers.Get("Sec-CH-UA-Model")
|
||||
|
||||
result := resp.HeadersResp{}
|
||||
result.IP = clientIP
|
||||
result.Headers = headersMap
|
||||
|
||||
return result
|
||||
}
|
14
config/config.go
Normal file
14
config/config.go
Normal file
@ -0,0 +1,14 @@
|
||||
package config
|
||||
|
||||
var Cfg Config
|
||||
|
||||
// Config 配置
|
||||
type Config struct {
|
||||
Server Server
|
||||
}
|
||||
|
||||
// Server 服务配置
|
||||
type Server struct {
|
||||
AppMode string // 应用模式
|
||||
ServerPort string // 服务端口
|
||||
}
|
4
config/config.yaml
Normal file
4
config/config.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
# 本地运行的配置文件
|
||||
Server:
|
||||
AppMode: debug # debug 开发环境, release 生产环境
|
||||
ServerPort: :80 # 服务端口
|
52
go.mod
52
go.mod
@ -2,31 +2,53 @@ module api
|
||||
|
||||
go 1.23
|
||||
|
||||
require github.com/gin-gonic/gin v1.9.1
|
||||
require (
|
||||
github.com/gin-contrib/cors v1.7.2
|
||||
github.com/gin-gonic/gin v1.9.1
|
||||
github.com/spf13/viper v1.19.0
|
||||
golang.org/x/sync v0.8.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/bytedance/sonic v1.9.1 // indirect
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
|
||||
github.com/bytedance/sonic v1.11.6 // indirect
|
||||
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.14.0 // indirect
|
||||
github.com/go-playground/validator/v10 v10.20.0 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
|
||||
github.com/leodido/go-urn v1.2.4 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/magiconair/properties v1.8.7 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
|
||||
github.com/sagikazarmark/locafero v0.4.0 // indirect
|
||||
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
|
||||
github.com/sourcegraph/conc v0.3.0 // indirect
|
||||
github.com/spf13/afero v1.11.0 // indirect
|
||||
github.com/spf13/cast v1.6.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/subosito/gotenv v1.6.0 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.11 // indirect
|
||||
golang.org/x/arch v0.3.0 // indirect
|
||||
golang.org/x/crypto v0.9.0 // indirect
|
||||
golang.org/x/net v0.10.0 // indirect
|
||||
golang.org/x/sys v0.8.0 // indirect
|
||||
golang.org/x/text v0.9.0 // indirect
|
||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||
go.uber.org/atomic v1.9.0 // indirect
|
||||
go.uber.org/multierr v1.9.0 // indirect
|
||||
golang.org/x/arch v0.7.0 // indirect
|
||||
golang.org/x/crypto v0.22.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
|
||||
golang.org/x/net v0.24.0 // indirect
|
||||
golang.org/x/sys v0.19.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
google.golang.org/protobuf v1.34.1 // indirect
|
||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
132
go.sum
132
go.sum
@ -1,14 +1,23 @@
|
||||
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
|
||||
github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s=
|
||||
github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
|
||||
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
|
||||
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
||||
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
||||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
||||
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
|
||||
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
|
||||
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
|
||||
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
|
||||
github.com/gin-contrib/cors v1.7.2 h1:oLDHxdg8W/XDoN/8zamqk/Drgt4oVZDvaV0YmvVICQw=
|
||||
github.com/gin-contrib/cors v1.7.2/go.mod h1:SUJVARKgQ40dmrzgXEVxj2m7Ig1v1qIboQkPDTQ9t2E=
|
||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
|
||||
@ -19,66 +28,107 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js=
|
||||
github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
|
||||
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
|
||||
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
|
||||
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
|
||||
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
|
||||
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
|
||||
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
|
||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
|
||||
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
|
||||
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
|
||||
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
|
||||
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
||||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
|
||||
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
|
||||
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
|
||||
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
|
||||
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
|
||||
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
|
||||
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
|
||||
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
|
||||
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
|
||||
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI=
|
||||
github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
|
||||
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
||||
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
|
||||
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
||||
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
||||
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
|
||||
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
|
||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
|
||||
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
|
||||
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
|
||||
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
|
||||
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/arch v0.7.0 h1:pskyeJh/3AmoQ8CPE95vxHLqp1G1GfGNXTmcl9NEKTc=
|
||||
golang.org/x/arch v0.7.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
|
||||
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
|
||||
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
|
||||
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
|
||||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
|
||||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
|
||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
|
||||
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
|
||||
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
||||
|
39
main.go
39
main.go
@ -1,41 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"api/router"
|
||||
"golang.org/x/sync/errgroup"
|
||||
"log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
// 初始化全局变量
|
||||
router.InitGlobalVariable()
|
||||
|
||||
// 定义一个处理器函数,用于获取所有的请求头并返回给客户端
|
||||
r.GET("/api/getHeaders", func(c *gin.Context) {
|
||||
// 获取所有的请求头
|
||||
request := c.Request
|
||||
headers := request.Header
|
||||
clientIP := c.ClientIP()
|
||||
var g errgroup.Group
|
||||
|
||||
// 将请求头转换为 map[string]string
|
||||
headersMap := make(map[string]string)
|
||||
for key, value := range headers {
|
||||
headersMap[key] = value[0] // 注意,这里假设每个键只有一个值,你可以根据实际情况修改逻辑
|
||||
}
|
||||
headersMap["platform"] = headers.Get("Sec-CH-UA-Platform")
|
||||
headersMap["model"] = headers.Get("Sec-CH-UA-Model")
|
||||
|
||||
// 返回请求头的 map
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"ip": clientIP,
|
||||
"headers": headersMap,
|
||||
})
|
||||
// 服务接口
|
||||
g.Go(func() error {
|
||||
return router.Server().ListenAndServe()
|
||||
})
|
||||
|
||||
// 启动 Gin 服务器
|
||||
err := r.Run(":80")
|
||||
if err != nil {
|
||||
fmt.Print("Error")
|
||||
return
|
||||
if err := g.Wait(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
30
router/middleware/cors.go
Normal file
30
router/middleware/cors.go
Normal file
@ -0,0 +1,30 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Cors 跨域中间件
|
||||
func Cors() gin.HandlerFunc {
|
||||
return cors.New(cors.Config{
|
||||
// 允许跨域请求网站
|
||||
AllowOrigins: []string{"*"},
|
||||
// 允许使用的请求方式
|
||||
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"},
|
||||
// 允许使用的请求头
|
||||
AllowHeaders: []string{"Origin", "Authorization", "Content-Type", "X-Requested-With", "*"},
|
||||
// 暴露的请求头
|
||||
ExposeHeaders: []string{"Content-Type"},
|
||||
// 凭证共享
|
||||
AllowCredentials: true,
|
||||
// 允许跨域的源网站
|
||||
AllowOriginFunc: func(origin string) bool {
|
||||
return true
|
||||
},
|
||||
// 超时时间设定
|
||||
MaxAge: 24 * time.Hour,
|
||||
})
|
||||
}
|
25
router/router.go
Normal file
25
router/router.go
Normal file
@ -0,0 +1,25 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"api/config"
|
||||
"api/utils"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func InitGlobalVariable() {
|
||||
// 初始化 Viper
|
||||
utils.InitViper()
|
||||
}
|
||||
|
||||
func Server() *http.Server {
|
||||
serverPort := config.Cfg.Server.ServerPort
|
||||
log.Printf("服务启动于 %s 端口", serverPort)
|
||||
return &http.Server{
|
||||
Addr: serverPort,
|
||||
Handler: ServerRouter(),
|
||||
ReadTimeout: 5 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
}
|
||||
}
|
23
router/server_router.go
Normal file
23
router/server_router.go
Normal file
@ -0,0 +1,23 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"api/config"
|
||||
"api/router/middleware"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func ServerRouter() http.Handler {
|
||||
gin.SetMode(config.Cfg.Server.AppMode)
|
||||
r := gin.New()
|
||||
r.SetTrustedProxies([]string{"*"})
|
||||
|
||||
r.Use(middleware.Cors()) // 跨域中间件
|
||||
|
||||
// 无需鉴权的接口
|
||||
base := r.Group("/api")
|
||||
{
|
||||
base.GET("/getHeaders", networkApi.GetHeaders)
|
||||
}
|
||||
return r
|
||||
}
|
9
router/z_enter.go
Normal file
9
router/z_enter.go
Normal file
@ -0,0 +1,9 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"api/api/network/server"
|
||||
)
|
||||
|
||||
var (
|
||||
networkApi server.NetWorkApi
|
||||
)
|
128
utils/r/code.go
Normal file
128
utils/r/code.go
Normal file
@ -0,0 +1,128 @@
|
||||
package r
|
||||
|
||||
// 错误码汇总
|
||||
const (
|
||||
OK = 0
|
||||
FAIL = 500
|
||||
|
||||
// code= 9000... 通用错误
|
||||
ERROR_REQUEST_PARAM = 9001
|
||||
ERROR_REQUEST_PAGE = 9002
|
||||
ERROR_INVALID_PARAM = 9003
|
||||
ERROR_DB_OPE = 9004
|
||||
|
||||
// code 91... 文件相关错误
|
||||
ERROR_FILE_UPLOAD = 9100
|
||||
EEROR_FILE_RECEIVE = 9101
|
||||
|
||||
// code= 1000... 用户模块的错误
|
||||
ERROR_USER_NAME_USED = 1001
|
||||
ERROR_PASSWORD_WRONG = 1002
|
||||
ERROR_USER_NOT_EXIST = 1003
|
||||
ERROR_USER_NO_RIGHT = 1009
|
||||
ERROR_OLD_PASSWORD = 1010
|
||||
ERROR_EMAIL_SEND = 1011
|
||||
ERROR_EMAIL_HAS_SEND = 1012
|
||||
ERROR_VERIFICATION_CODE = 1013
|
||||
|
||||
// code = 1200.. 鉴权相关错误
|
||||
ERROR_TOKEN_NOT_EXIST = 1201
|
||||
ERROR_TOKEN_RUNTIME = 1202
|
||||
ERROR_TOKEN_WRONG = 1203
|
||||
ERROR_TOKEN_TYPE_WRONG = 1204
|
||||
ERROR_TOKEN_CREATE = 1205
|
||||
ERROR_PERMI_DENIED = 1206
|
||||
FORCE_OFFLINE = 1207
|
||||
LOGOUT = 1208
|
||||
|
||||
// code= 2000... 文章模块的错误
|
||||
ERROR_ART_NOT_EXIST = 2001
|
||||
|
||||
// code= 3000... 分类模块的错误
|
||||
ERROR_CATE_NAME_USED = 3001
|
||||
ERROR_CATE_NOT_EXIST = 3002
|
||||
ERROR_CATE_ART_EXIST = 3003
|
||||
|
||||
// code= 4000... 标签模块的错误
|
||||
ERROR_TAG_EXIST = 4001
|
||||
ERROR_TAG_NOT_EXIST = 4002
|
||||
ERROR_TAG_ART_EXIST = 4003
|
||||
|
||||
// code=5000... 评论模块的错误
|
||||
ERROR_COMMENT_NOT_EXIST = 5001
|
||||
|
||||
// code=6000... 权限模块的错误
|
||||
ERROR_RESOURCE_NAME_EXIST = 6001
|
||||
ERROR_RESOURCE_NOT_EXIST = 6002
|
||||
ERROR_RESOURCE_USED_BY_ROLE = 6003
|
||||
ERROR_RESOURCE_HAS_CHILDREN = 6004
|
||||
ERROR_MENU_NAME_EXIST = 6005
|
||||
ERROR_MENU_NOT_EXIST = 6006
|
||||
ERROR_MENU_USED_BY_ROLE = 6007
|
||||
ERROR_MENU_HAS_CHILDREN = 6008
|
||||
ERROR_ROLE_NAME_EXIST = 60010
|
||||
ERROR_ROLE_NOT_EXIST = 60011
|
||||
|
||||
// code=7000 ... 页面模块的错误
|
||||
ERROR_PAGE_NAME_EXIST = 7001
|
||||
)
|
||||
|
||||
var codeMsg = map[int]string{
|
||||
OK: "OK",
|
||||
FAIL: "FAIL",
|
||||
|
||||
ERROR_REQUEST_PARAM: "请求参数格式错误",
|
||||
ERROR_REQUEST_PAGE: "分页参数错误",
|
||||
ERROR_INVALID_PARAM: "不合法的请求参数",
|
||||
ERROR_DB_OPE: "数据库操作异常",
|
||||
|
||||
EEROR_FILE_RECEIVE: "文件接收失败",
|
||||
ERROR_FILE_UPLOAD: "文件上传失败",
|
||||
|
||||
ERROR_USER_NAME_USED: "用户名已存在",
|
||||
ERROR_USER_NOT_EXIST: "该用户不存在",
|
||||
ERROR_PASSWORD_WRONG: "密码错误",
|
||||
ERROR_USER_NO_RIGHT: "该用户无权限",
|
||||
ERROR_OLD_PASSWORD: "旧密码不正确",
|
||||
ERROR_EMAIL_SEND: "邮件发送失败",
|
||||
ERROR_EMAIL_HAS_SEND: "已朝该邮箱发送验证码(有效期 15 分钟),请检查回收站",
|
||||
ERROR_VERIFICATION_CODE: "验证码错误",
|
||||
|
||||
ERROR_TOKEN_NOT_EXIST: "TOKEN 不存在,请重新登陆",
|
||||
ERROR_TOKEN_RUNTIME: "TOKEN 已过期,请重新登陆",
|
||||
ERROR_TOKEN_WRONG: "TOKEN 不正确,请重新登陆",
|
||||
ERROR_TOKEN_TYPE_WRONG: "TOKEN 格式错误,请重新登陆",
|
||||
ERROR_TOKEN_CREATE: "TOKEN 生成失败",
|
||||
ERROR_PERMI_DENIED: "权限不足",
|
||||
FORCE_OFFLINE: "您已被强制下线",
|
||||
LOGOUT: "您已退出登录",
|
||||
|
||||
ERROR_ART_NOT_EXIST: "文章不存在",
|
||||
|
||||
ERROR_CATE_NAME_USED: "操作失败,分类名已存在",
|
||||
ERROR_CATE_NOT_EXIST: "操作失败,分类不存在",
|
||||
ERROR_CATE_ART_EXIST: "删除失败,分类下存在文章",
|
||||
|
||||
ERROR_TAG_EXIST: "操作失败,标签名已存在",
|
||||
ERROR_TAG_NOT_EXIST: "操作失败,标签不存在",
|
||||
ERROR_TAG_ART_EXIST: "删除失败,标签下存在文章",
|
||||
|
||||
ERROR_COMMENT_NOT_EXIST: "评论不存在",
|
||||
|
||||
ERROR_RESOURCE_NAME_EXIST: "该资源名已经存在",
|
||||
ERROR_RESOURCE_NOT_EXIST: "该资源不存在",
|
||||
ERROR_RESOURCE_HAS_CHILDREN: "该资源下存在子资源,无法删除",
|
||||
ERROR_RESOURCE_USED_BY_ROLE: "该资源正在被角色使用,无法删除",
|
||||
ERROR_MENU_NAME_EXIST: "该菜单名已经存在",
|
||||
ERROR_MENU_NOT_EXIST: "该菜单不存在",
|
||||
ERROR_MENU_USED_BY_ROLE: "该菜单正在被角色使用,无法删除",
|
||||
ERROR_MENU_HAS_CHILDREN: "该菜单下存在子菜单,无法删除",
|
||||
ERROR_ROLE_NAME_EXIST: "该角色名已经存在",
|
||||
ERROR_ROLE_NOT_EXIST: "该角色不存在",
|
||||
|
||||
ERROR_PAGE_NAME_EXIST: "该页面名称已经存在",
|
||||
}
|
||||
|
||||
func GetMsg(code int) string {
|
||||
return codeMsg[code]
|
||||
}
|
49
utils/r/result.go
Normal file
49
utils/r/result.go
Normal file
@ -0,0 +1,49 @@
|
||||
package r
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Response 响应结构体
|
||||
type Response struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data any `json:"data"`
|
||||
}
|
||||
|
||||
// ReturnJson 返回 JSON 数据
|
||||
func ReturnJson(c *gin.Context, httpCode, code int, msg string, data any) {
|
||||
// c.Header("", "") // 根据需要在头部添加其他信息
|
||||
c.JSON(httpCode, Response{
|
||||
Code: code,
|
||||
Message: msg,
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
// 语法糖函数封装
|
||||
|
||||
// Send 自定义 httpCode, code, data
|
||||
func Send(c *gin.Context, httpCode, code int, data any) {
|
||||
ReturnJson(c, httpCode, code, GetMsg(code), data)
|
||||
}
|
||||
|
||||
// SendCode 自动根据 code 获取 message, 且 data == nil
|
||||
func SendCode(c *gin.Context, code int) {
|
||||
Send(c, http.StatusOK, code, nil)
|
||||
}
|
||||
|
||||
// SendData 自动根据 code 获取 message, 且 data != nil
|
||||
func SendData(c *gin.Context, code int, data any) {
|
||||
Send(c, http.StatusOK, code, data)
|
||||
}
|
||||
|
||||
func SuccessData(c *gin.Context, data any) {
|
||||
Send(c, http.StatusOK, OK, data)
|
||||
}
|
||||
|
||||
func Success(c *gin.Context) {
|
||||
Send(c, http.StatusOK, OK, nil)
|
||||
}
|
40
utils/viper.go
Normal file
40
utils/viper.go
Normal file
@ -0,0 +1,40 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"api/config"
|
||||
"flag"
|
||||
"github.com/spf13/viper"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// InitViper 优先级: 方法传参 > 命令行 > 默认值
|
||||
func InitViper() {
|
||||
// 根据命令行读取配置文件路径
|
||||
var configPath string
|
||||
flag.StringVar(&configPath, "c", "", "chosse config file.")
|
||||
flag.Parse()
|
||||
if configPath != "" { // 命令行读取到参数
|
||||
log.Printf("您正在使用命令行的-c参数传递的值,config的路径为 %s\n", configPath)
|
||||
} else { // 命令行未读取到参数
|
||||
log.Println("命令行参数为空, 默认加载: config/config.yaml")
|
||||
configPath = "config/config.yaml"
|
||||
}
|
||||
|
||||
// 目前读取固定固定路径的配置文件
|
||||
v := viper.New()
|
||||
v.SetConfigFile(configPath)
|
||||
v.AutomaticEnv() // 允许使用环境变量
|
||||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) // SERVER_APPMODE => SERVER.APPMODE
|
||||
|
||||
// 读取配置文件
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
log.Panic("配置文件读取失败: ", err)
|
||||
}
|
||||
|
||||
// 加载配置文件内容到结构体对象
|
||||
if err := v.Unmarshal(&config.Cfg); err != nil {
|
||||
log.Panic("配置文件内容加载失败: ", err)
|
||||
}
|
||||
log.Println("配置文件内容加载成功")
|
||||
}
|
Loading…
Reference in New Issue
Block a user