-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from AAStarCommunity/chao/feature/router-0.1
[FEAT] gin support, swagger, github action
- Loading branch information
Showing
25 changed files
with
756 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
on: | ||
pull_request: | ||
branches: | ||
- "*" | ||
push: | ||
branches: | ||
- "*" | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '1.22.0' | ||
- name: Build | ||
run: go build -v ./... | ||
- name: Test | ||
run: go test -v ./... | ||
|
||
name: Building And Testing |
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,35 @@ | ||
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", "", "Port") | ||
|
||
// runMode running mode | ||
// @string: Port | ||
func runMode() string { | ||
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,49 @@ | ||
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 read conf from file | ||
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 // env Name, like `prod`, `dev` and etc., | ||
Debugger bool // whether to use debugger | ||
} | ||
|
||
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.