-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28cf132
commit 2ab6b61
Showing
24 changed files
with
739 additions
and
64 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 |
---|---|---|
|
@@ -23,4 +23,6 @@ go.work | |
.idea | ||
.vscode/ | ||
build/ | ||
config/*.json | ||
config/*.json | ||
|
||
conf/appsettings.*.yaml |
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,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) |
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,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) | ||
} |
File renamed without changes.
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,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 | ||
} |
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,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" | ||
}(), | ||
} | ||
} |
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,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) | ||
} |
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,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" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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,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" |
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.