diff --git a/.gitignore b/.gitignore index ce392607..b4cf4192 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,6 @@ go.work .idea .vscode/ build/ -config/*.json \ No newline at end of file +config/*.json + +conf/appsettings.*.yaml diff --git a/README.md b/README.md index 378e16e9..ae75c6a4 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,21 @@ # EthPaymaster-Back EthPaymaster relay Back-end Service + + +# Quick Start + +## 1. Swagger + +### 1.1 install + +```shell +go install github.com/swaggo/swag/cmd/swag@latest +``` + +### 1.2 init swag + +```shell +swag init -g ./cmd/server/main.go +``` + +> FAQ: [Unknown LeftDelim and RightDelim in swag.Spec](https://github.com/swaggo/swag/issues/1568) diff --git a/cmd/server/main.go b/cmd/server/main.go index 3477f819..3e68eb43 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,18 +1,36 @@ package main import ( - "AAStarCommunity/EthPaymaster_BackService/router" - "fmt" + "AAStarCommunity/EthPaymaster_BackService/rpc_server/routers" + "flag" + "os" + "strings" ) -func init() { - //init global variables when service start +var aPort = flag.String("port", "", "端口") +// runMode 获取运行模式 +// @string: 端口 +func runMode() string { + // 优先读取命令行参数,其次使用go env,最后使用默认值 + flag.Parse() + + if len(*aPort) == 0 { + *aPort = os.Getenv("port") + } + + if len(*aPort) == 0 { + *aPort = ":80" + } + + if !strings.HasPrefix(*aPort, ":") { + *aPort = ":" + *aPort + } + + return *aPort } func main() { - //use InitRouter - router.InitRouter() - fmt.Printf("Server now running on 0.0.0.0:%d", 8080) - router.Engine.Run(":8080") + port := runMode() + _ = routers.SetRouters().Run(port) } diff --git a/docs/api.md b/conf/appsettings.yaml similarity index 100% rename from docs/api.md rename to conf/appsettings.yaml diff --git a/conf/conf.go b/conf/conf.go new file mode 100644 index 00000000..f720b773 --- /dev/null +++ b/conf/conf.go @@ -0,0 +1,52 @@ +package conf + +import ( + "k8s.io/apimachinery/pkg/util/yaml" + "os" + "sync" +) + +var once sync.Once + +type Conf struct { + // TODO: Add Conf Structure Here +} + +var conf *Conf + +// getConf 读取配置 +// 默认从配置文件取,如果配置文件中的db节点内容为空,则从环境变量取 +// 如果配置文件不存在,则db从环境变量取,其他值使用默认值 +func getConf() *Conf { + once.Do(func() { + if conf == nil { + filePath := getConfFilePath() + conf = getConfiguration(filePath) + } + }) + return conf +} + +// getConfiguration 读取配置 +// 从配置文件读取,如果环境变量存在对应值,则取环境变量值 +func getConfiguration(filePath *string) *Conf { + if file, err := os.ReadFile(*filePath); err != nil { + return mappingEnvToConf(nil) + } else { + c := Conf{} + err := yaml.Unmarshal(file, &c) + if err != nil { + return mappingEnvToConf(&c) + } + + return &c + } +} + +func mappingEnvToConf(conf *Conf) *Conf { + + // TODO: read from env + // e.g. if dummy := os.Getenv("dummy"); len(dummy) > 0 {conf.Dummy = dummy} + + return conf +} diff --git a/conf/env.go b/conf/env.go new file mode 100644 index 00000000..f43ea398 --- /dev/null +++ b/conf/env.go @@ -0,0 +1,47 @@ +package conf + +import ( + "fmt" + "os" + "strings" +) + +type Env struct { + Name string // 环境名称 + Debugger bool // 是否调试模式 +} + +func (env *Env) IsDevelopment() bool { + return strings.EqualFold("dev", env.Name) +} + +func (env *Env) IsProduction() bool { + return strings.EqualFold("prod", env.Name) +} + +func (env *Env) GetEnvName() *string { + return &env.Name +} + +func getConfFilePath() *string { + path := fmt.Sprintf("conf/appsettings.%s.yaml", strings.ToLower(Environment.Name)) + if _, err := os.Stat(path); err != nil && os.IsNotExist(err) { + path = fmt.Sprintf("conf/appsettings.yaml") + } + return &path +} + +var Environment *Env + +func init() { + envName := "prod" + if len(os.Getenv("Env")) > 0 { + envName = os.Getenv("Env") + } + Environment = &Env{ + Name: envName, + Debugger: func() bool { + return envName != "prod" + }(), + } +} diff --git a/docs/docs.go b/docs/docs.go new file mode 100644 index 00000000..9f6757d1 --- /dev/null +++ b/docs/docs.go @@ -0,0 +1,69 @@ +// Package docs Code generated by swaggo/swag. DO NOT EDIT +package docs + +import "github.com/swaggo/swag" + +const docTemplate = `{ + "schemes": {{ marshal .Schemes }}, + "swagger": "2.0", + "info": { + "description": "{{escape .Description}}", + "title": "{{.Title}}", + "contact": {}, + "version": "{{.Version}}" + }, + "host": "{{.Host}}", + "basePath": "{{.BasePath}}", + "paths": { + "/api/v1/sponsor-user-operation": { + "post": { + "description": "sponsor the userOp", + "consumes": [ + "application/json" + ], + "tags": [ + "Sponsor" + ], + "responses": { + "200": { + "description": "OK" + } + } + } + }, + "/api/v1/validate-user-operation": { + "post": { + "description": "validate the userOp for sponsor", + "consumes": [ + "application/json" + ], + "tags": [ + "Sponsor" + ], + "responses": { + "200": { + "description": "OK" + } + } + } + } + } +}` + +// SwaggerInfo holds exported Swagger Info so clients can modify it +var SwaggerInfo = &swag.Spec{ + Version: "", + Host: "", + BasePath: "", + Schemes: []string{}, + Title: "", + Description: "", + InfoInstanceName: "swagger", + SwaggerTemplate: docTemplate, + LeftDelim: "{{", + RightDelim: "}}", +} + +func init() { + swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo) +} diff --git a/docs/swagger.json b/docs/swagger.json new file mode 100644 index 00000000..c6aee7bd --- /dev/null +++ b/docs/swagger.json @@ -0,0 +1,40 @@ +{ + "swagger": "2.0", + "info": { + "contact": {} + }, + "paths": { + "/api/v1/sponsor-user-operation": { + "post": { + "description": "sponsor the userOp", + "consumes": [ + "application/json" + ], + "tags": [ + "Sponsor" + ], + "responses": { + "200": { + "description": "OK" + } + } + } + }, + "/api/v1/validate-user-operation": { + "post": { + "description": "validate the userOp for sponsor", + "consumes": [ + "application/json" + ], + "tags": [ + "Sponsor" + ], + "responses": { + "200": { + "description": "OK" + } + } + } + } + } +} \ No newline at end of file diff --git a/docs/swagger.yaml b/docs/swagger.yaml new file mode 100644 index 00000000..58b6deff --- /dev/null +++ b/docs/swagger.yaml @@ -0,0 +1,24 @@ +info: + contact: {} +paths: + /api/v1/sponsor-user-operation: + post: + consumes: + - application/json + description: sponsor the userOp + responses: + "200": + description: OK + tags: + - Sponsor + /api/v1/validate-user-operation: + post: + consumes: + - application/json + description: validate the userOp for sponsor + responses: + "200": + description: OK + tags: + - Sponsor +swagger: "2.0" diff --git a/go.mod b/go.mod index 7e458433..3ca0a885 100644 --- a/go.mod +++ b/go.mod @@ -2,21 +2,35 @@ module AAStarCommunity/EthPaymaster_BackService go 1.22.0 -require github.com/gin-gonic/gin v1.9.1 +require ( + github.com/gin-contrib/cors v1.5.0 + github.com/gin-gonic/gin v1.9.1 + github.com/swaggo/files v1.0.1 + github.com/swaggo/gin-swagger v1.6.0 + github.com/swaggo/swag v1.16.3 + k8s.io/apimachinery v0.29.2 +) require ( + github.com/KyleBanks/depth v1.2.1 // indirect github.com/bytedance/sonic v1.11.1 // indirect github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect github.com/chenzhuoyu/iasm v0.9.1 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-openapi/jsonpointer v0.20.2 // indirect + github.com/go-openapi/jsonreference v0.20.4 // indirect + github.com/go-openapi/spec v0.20.14 // indirect + github.com/go-openapi/swag v0.22.9 // 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.18.0 // indirect github.com/goccy/go-json v0.10.2 // indirect + github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.7 // indirect github.com/leodido/go-urn v1.4.0 // indirect + github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect @@ -28,6 +42,10 @@ require ( golang.org/x/net v0.21.0 // indirect golang.org/x/sys v0.17.0 // indirect golang.org/x/text v0.14.0 // indirect + golang.org/x/tools v0.18.0 // indirect google.golang.org/protobuf v1.32.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect + sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect + sigs.k8s.io/yaml v1.3.0 // indirect ) diff --git a/go.sum b/go.sum index 811b5427..2b217352 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,7 @@ +github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= +github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= +github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= +github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= github.com/bytedance/sonic v1.11.1 h1:JC0+6c9FoWYYxakaoa+c5QTtJeiSZNeByOBhXtAFSn4= @@ -9,14 +13,43 @@ github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpV github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0= github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= 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.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.5.0 h1:DgGKV7DDoOn36DFkNtbHrjoRiT5ExCe+PC9/xp7aKvk= +github.com/gin-contrib/cors v1.5.0/go.mod h1:TvU7MAZ3EwrPLI2ztzTt3tqgvBCq+wn8WpZmfADjupI= +github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4= +github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjovCXNuzzk= 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= github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= +github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= +github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= +github.com/go-openapi/jsonpointer v0.19.6 h1:eCs3fxoIi3Wh6vtgmLTOjdhSpiqphQ+DaPn38N2ZdrE= +github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs= +github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q= +github.com/go-openapi/jsonpointer v0.20.2/go.mod h1:bHen+N0u1KEO3YlmqOjTT9Adn1RfD91Ar825/PuiRVs= +github.com/go-openapi/jsonreference v0.19.6/go.mod h1:diGHMEHg2IqXZGKxqyvWdfWU/aim5Dprw5bqpKkTvns= +github.com/go-openapi/jsonreference v0.20.2 h1:3sVjiK66+uXK/6oQ8xgcRKcFgQ5KXa2KvnJRumpMGbE= +github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En5Ap4rVB5KVcIDZG2k= +github.com/go-openapi/jsonreference v0.20.4 h1:bKlDxQxQJgwpUSgOENiMPzCTBVuc7vTdXSSgNeAhojU= +github.com/go-openapi/jsonreference v0.20.4/go.mod h1:5pZJyJP2MnYCpoeoMAql78cCHauHj0V9Lhc506VOpw4= +github.com/go-openapi/spec v0.20.4 h1:O8hJrt0UMnhHcluhIdUgCLRWyM2x7QkBXRvOs7m+O1M= +github.com/go-openapi/spec v0.20.4/go.mod h1:faYFR1CvsJZ0mNsmsphTMSoRrNV3TEDoAM7FOEWeq8I= +github.com/go-openapi/spec v0.20.14 h1:7CBlRnw+mtjFGlPDRZmAMnq35cRzI91xj03HVyUi/Do= +github.com/go-openapi/spec v0.20.14/go.mod h1:8EOhTpBoFiask8rrgwbLC3zmJfz4zsCUueRuPM6GNkw= +github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk= +github.com/go-openapi/swag v0.19.15/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ= +github.com/go-openapi/swag v0.22.3 h1:yMBqmnQ0gyZvEb/+KzuWZOXgllrXT4SADYbvDaXHv/g= +github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14= +github.com/go-openapi/swag v0.22.9 h1:XX2DssF+mQKM2DHsbgZK74y/zj4mo9I99+89xUmuZCE= +github.com/go-openapi/swag v0.22.9/go.mod h1:3/OXnFfnMAwBD099SwYRk7GD3xOrr1iL7d/XNLXVVwE= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= 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= @@ -25,17 +58,32 @@ github.com/go-playground/validator/v10 v10.18.0 h1:BvolUXjp4zuvkZ5YN5t7ebzbhlUtP github.com/go-playground/validator/v10 v10.18.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/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= 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.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.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +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/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= +github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= +github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= 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/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -43,41 +91,105 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w 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/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= +github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= 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/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 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.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/swaggo/files v1.0.1 h1:J1bVJ4XHZNq0I46UU90611i9/YzdrF7x92oX1ig5IdE= +github.com/swaggo/files v1.0.1/go.mod h1:0qXmMNH6sXNf+73t65aKeB+ApmgxdnkQzVTAj2uaMUg= +github.com/swaggo/gin-swagger v1.6.0 h1:y8sxvQ3E20/RCyrXeFfg60r6H0Z+SwpTjMYsMm+zy8M= +github.com/swaggo/gin-swagger v1.6.0/go.mod h1:BG00cCEy294xtVpyIAHG6+e2Qzj/xKlRdOqDkvq0uzo= +github.com/swaggo/swag v1.8.12 h1:pctzkNPu0AlQP2royqX3apjKCQonAnf7KGoxeO4y64w= +github.com/swaggo/swag v1.8.12/go.mod h1:lNfm6Gg+oAq3zRJQNEMBE66LIJKM44mxFqhEEgy2its= +github.com/swaggo/swag v1.16.3 h1:PnCYjPCah8FK4I26l2F/KQ4yz3sILcVUN3cTlBFA9Pg= +github.com/swaggo/swag v1.16.3/go.mod h1:DImHIuOFXKpMFAQjcC7FG4m3Dg4+QuUgUzJmKjI/gRk= 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.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= 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.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.20.0 h1:jmAMJJZXr5KiCw05dfYK9QnqaqKLYXijU23lsEdcQqg= golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= +golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 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.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA= +golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0= +golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ= +golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/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/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/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= +k8s.io/apimachinery v0.29.2 h1:EWGpfJ856oj11C52NRCHuU7rFDwxev48z+6DSlGNsV8= +k8s.io/apimachinery v0.29.2/go.mod h1:6HVkd1FwxIagpYrHSwJlQqZI3G9LfYWRPAkUvLnXTKU= nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= +sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo= +sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0= +sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo= +sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8= diff --git a/router/main.go b/router/main.go deleted file mode 100644 index 7c65e819..00000000 --- a/router/main.go +++ /dev/null @@ -1,25 +0,0 @@ -package router - -import ( - "github.com/gin-gonic/gin" -) - -var ( - Engine *gin.Engine -) - -func InitRouter() { - if Engine != nil { - return - } - - Engine = gin.Default() - Engine.GET("/healthz", func(c *gin.Context) { - c.JSON(200, gin.H{ - "status": "ok", - }) - }) - Engine.POST("/sponsorUserOperation", sponsorUserOperation) - Engine.POST("/v1/user/relation/bind", validateUserOperation) - -} diff --git a/router/main_test.go b/router/main_test.go deleted file mode 100644 index 66dc9ad0..00000000 --- a/router/main_test.go +++ /dev/null @@ -1,6 +0,0 @@ -package router - -import "testing" - -func TestMain(m *testing.M) { -} diff --git a/router/sponsorUserOperation.go b/router/sponsorUserOperation.go deleted file mode 100644 index 2c772be9..00000000 --- a/router/sponsorUserOperation.go +++ /dev/null @@ -1,11 +0,0 @@ -package router - -import ( - "github.com/gin-gonic/gin" -) - -func sponsorUserOperation(c *gin.Context) { - c.JSON(200, gin.H{ - "status": "ok", - }) -} diff --git a/router/validateUserOperation.go b/router/validateUserOperation.go deleted file mode 100644 index 807e6f85..00000000 --- a/router/validateUserOperation.go +++ /dev/null @@ -1,9 +0,0 @@ -package router - -import "github.com/gin-gonic/gin" - -func validateUserOperation(c *gin.Context) { - c.JSON(200, gin.H{ - "status": "ok", - }) -} diff --git a/rpc_server/api/v1/sponsor_user_operation.go b/rpc_server/api/v1/sponsor_user_operation.go new file mode 100644 index 00000000..b5134320 --- /dev/null +++ b/rpc_server/api/v1/sponsor_user_operation.go @@ -0,0 +1,18 @@ +package v1 + +import ( + "github.com/gin-gonic/gin" +) + +// SponsorUserOperation +// @Tags Sponsor +// @Description sponsor the userOp +// @Accept json +// @Product json +// @Router /api/v1/sponsor-user-operation [post] +// @Success 200 +func SponsorUserOperation(c *gin.Context) { + c.JSON(200, gin.H{ + "status": "ok", + }) +} diff --git a/rpc_server/api/v1/validate_user_operation.go b/rpc_server/api/v1/validate_user_operation.go new file mode 100644 index 00000000..2f245deb --- /dev/null +++ b/rpc_server/api/v1/validate_user_operation.go @@ -0,0 +1,16 @@ +package v1 + +import "github.com/gin-gonic/gin" + +// ValidateUserOperation +// @Tags Sponsor +// @Description validate the userOp for sponsor +// @Accept json +// @Product json +// @Router /api/v1/validate-user-operation [post] +// @Success 200 +func ValidateUserOperation(c *gin.Context) { + c.JSON(200, gin.H{ + "status": "ok", + }) +} diff --git a/rpc_server/middlewares/cors.go b/rpc_server/middlewares/cors.go new file mode 100644 index 00000000..180ce430 --- /dev/null +++ b/rpc_server/middlewares/cors.go @@ -0,0 +1,9 @@ +package middlewares + +import "github.com/gin-gonic/gin" +import "github.com/gin-contrib/cors" + +// CorsHandler 跨域处理中间件 +func CorsHandler() gin.HandlerFunc { + return cors.Default() +} diff --git a/rpc_server/middlewares/recovery.go b/rpc_server/middlewares/recovery.go new file mode 100644 index 00000000..458059a7 --- /dev/null +++ b/rpc_server/middlewares/recovery.go @@ -0,0 +1,36 @@ +package middlewares + +import ( + "AAStarCommunity/EthPaymaster_BackService/conf" + "AAStarCommunity/EthPaymaster_BackService/rpc_server/models" + "errors" + "fmt" + "github.com/gin-gonic/gin" + "net/http" + "strings" +) + +// GenericRecovery 通用错误 (panic) 拦截中间件、对可能发生的错误进行拦截、统一记录 +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) + } + models.GetResponse().SetHttpCode(http.StatusInternalServerError).FailCode(c, http.StatusInternalServerError, errStr) + }) +} + +// PanicExceptionRecord panic等异常记录 +type PanicExceptionRecord struct{} + +func (p *PanicExceptionRecord) Write(b []byte) (n int, err error) { + s1 := "An error occurred in the server's internal code:" + var build strings.Builder + build.WriteString(s1) + build.Write(b) + errStr := build.String() + return len(errStr), errors.New(errStr) +} diff --git a/rpc_server/models/response.go b/rpc_server/models/response.go new file mode 100644 index 00000000..97107c3e --- /dev/null +++ b/rpc_server/models/response.go @@ -0,0 +1,138 @@ +package models + +import ( + "github.com/gin-gonic/gin" + "net/http" + "time" +) + +func GetResponse() *Response { + return &Response{ + httpCode: http.StatusOK, + Result: &Result{ + Code: 0, + Message: "", + Data: nil, + Cost: "", + }, + } +} +func BadRequest(ctx *gin.Context, data ...any) { + GetResponse().withDataAndHttpCode(http.StatusBadRequest, ctx, data) +} + +// Success 业务成功响应 +func Success(ctx *gin.Context, data ...any) { + if data != nil { + GetResponse().WithDataSuccess(ctx, data[0]) + return + } + GetResponse().Success(ctx) +} + +// Fail 业务失败响应 +func Fail(ctx *gin.Context, code int, message *string, data ...any) { + var msg string + if message == nil { + msg = "" + } else { + msg = *message + } + if data != nil { + GetResponse().WithData(data[0]).FailCode(ctx, code, msg) + return + } + GetResponse().FailCode(ctx, code, msg) +} + +type Result struct { + Code int `json:"code"` + Message string `json:"message"` + Data interface{} `json:"data"` + Cost string `json:"cost"` +} + +type Response struct { + httpCode int + Result *Result +} + +// Fail 错误返回 +func (r *Response) Fail(ctx *gin.Context) *Response { + r.SetCode(http.StatusInternalServerError) + r.json(ctx) + return r +} + +// FailCode 自定义错误码返回 +func (r *Response) FailCode(ctx *gin.Context, code int, msg ...string) *Response { + r.SetCode(code) + if msg != nil { + r.WithMessage(msg[0]) + } + r.json(ctx) + return r +} + +// Success 正确返回 +func (r *Response) Success(ctx *gin.Context) *Response { + r.SetCode(http.StatusOK) + r.json(ctx) + return r +} + +// WithDataSuccess 成功后需要返回值 +func (r *Response) WithDataSuccess(ctx *gin.Context, data interface{}) *Response { + r.SetCode(http.StatusOK) + r.WithData(data) + r.json(ctx) + return r +} + +func (r *Response) withDataAndHttpCode(code int, ctx *gin.Context, data interface{}) *Response { + r.SetHttpCode(code) + if data != nil { + r.WithData(data) + } + r.json(ctx) + return r +} + +// SetCode 设置返回code码 +func (r *Response) SetCode(code int) *Response { + r.Result.Code = code + return r +} + +// SetHttpCode 设置http状态码 +func (r *Response) SetHttpCode(code int) *Response { + r.httpCode = code + return r +} + +type defaultRes struct { + Result any `json:"result"` +} + +// WithData 设置返回data数据 +func (r *Response) WithData(data interface{}) *Response { + switch data.(type) { + case string, int, bool: + r.Result.Data = &defaultRes{Result: data} + default: + r.Result.Data = data + } + return r +} + +// WithMessage 设置返回自定义错误消息 +func (r *Response) WithMessage(message string) *Response { + r.Result.Message = message + return r +} + +// json 返回 gin 框架的 HandlerFunc +func (r *Response) json(ctx *gin.Context) { + r.Result.Cost = time.Since(ctx.GetTime("requestStartTime")).String() + ctx.AbortWithStatusJSON(r.httpCode, r.Result) +} diff --git a/rpc_server/routers/boot.go b/rpc_server/routers/boot.go new file mode 100644 index 00000000..ad4ebc6c --- /dev/null +++ b/rpc_server/routers/boot.go @@ -0,0 +1,55 @@ +package routers + +import ( + "AAStarCommunity/EthPaymaster_BackService/conf" + "AAStarCommunity/EthPaymaster_BackService/docs" + "AAStarCommunity/EthPaymaster_BackService/rpc_server/middlewares" + "AAStarCommunity/EthPaymaster_BackService/rpc_server/models" + "github.com/gin-gonic/gin" + swaggerFiles "github.com/swaggo/files" + ginSwagger "github.com/swaggo/gin-swagger" + "io" + "net/http" +) + +// SetRouters 设置API路由 +func SetRouters() (routers *gin.Engine) { + routers = gin.New() + + // 中间件 + handlers := make([]gin.HandlerFunc, 0) + handlers = append(handlers, middlewares.GenericRecovery()) + if conf.Environment.IsDevelopment() { + handlers = append(handlers, gin.Logger()) + } + handlers = append(handlers, middlewares.CorsHandler()) + + // 生产模式配置 + if conf.Environment.IsProduction() { + gin.SetMode(gin.ReleaseMode) // 生产模式 + gin.DefaultWriter = io.Discard // 禁用 gin 输出接口访问日志 + } + + // 开发模式配置 + if conf.Environment.IsDevelopment() { + gin.SetMode(gin.DebugMode) // 调试模式 + buildSwagger(routers) // 构建swagger + } + + // 加载中间件 + routers.Use(handlers...) + + buildRouters(routers) // 构建http路由 + + routers.NoRoute(func(ctx *gin.Context) { + models.GetResponse().SetHttpCode(http.StatusNotFound).FailCode(ctx, http.StatusNotFound) + }) + + return +} + +// buildSwagger 创建swagger文档 +func buildSwagger(router *gin.Engine) { + docs.SwaggerInfo.BasePath = "/" + router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) +} diff --git a/rpc_server/routers/builder.go b/rpc_server/routers/builder.go new file mode 100644 index 00000000..f385ebb0 --- /dev/null +++ b/rpc_server/routers/builder.go @@ -0,0 +1,23 @@ +package routers + +import ( + "github.com/gin-gonic/gin" +) + +// buildRouters 构建路由表 +func buildRouters(router *gin.Engine) { + + for _, routerMap := range RouterMaps { + for _, method := range routerMap.Methods { + if method == GET { + router.GET(routerMap.Url, routerMap.Func) + } else if method == PUT { + router.PUT(routerMap.Url, routerMap.Func) + } else if method == POST { + router.POST(routerMap.Url, routerMap.Func) + } else if method == DELETE { + router.DELETE(routerMap.Url, routerMap.Func) + } // ignore rest methods + } + } +} diff --git a/rpc_server/routers/map.go b/rpc_server/routers/map.go new file mode 100644 index 00000000..1a9b78cf --- /dev/null +++ b/rpc_server/routers/map.go @@ -0,0 +1,18 @@ +package routers + +import "github.com/gin-gonic/gin" + +type RestfulMethod string + +const ( + PUT RestfulMethod = "PUT" + GET RestfulMethod = "GET" + DELETE RestfulMethod = "DELETE" + POST RestfulMethod = "POST" +) + +type RouterMap struct { + Url string + Methods []RestfulMethod + Func func(ctx *gin.Context) +} diff --git a/rpc_server/routers/routers.go b/rpc_server/routers/routers.go new file mode 100644 index 00000000..ecf9da10 --- /dev/null +++ b/rpc_server/routers/routers.go @@ -0,0 +1,12 @@ +package routers + +import v1 "AAStarCommunity/EthPaymaster_BackService/rpc_server/api/v1" + +var RouterMaps []RouterMap + +func init() { + RouterMaps = make([]RouterMap, 0) + + RouterMaps = append(RouterMaps, RouterMap{"api/v1/sponsor-user-operation", []RestfulMethod{POST}, v1.SponsorUserOperation}) + RouterMaps = append(RouterMaps, RouterMap{"api/v1/validate-user-operation", []RestfulMethod{POST}, v1.ValidateUserOperation}) +}