Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] gin support, swagger, github action #6

Merged
merged 6 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/coverage.yml
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ go.work
.idea
.vscode/
build/
config/*.json
config/*.json

conf/appsettings.*.yaml
19 changes: 19 additions & 0 deletions README.md
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)
33 changes: 25 additions & 8 deletions cmd/server/main.go
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.
49 changes: 49 additions & 0 deletions conf/conf.go
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
}
47 changes: 47 additions & 0 deletions conf/env.go
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"
}(),
}
}
69 changes: 69 additions & 0 deletions docs/docs.go
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)
}
40 changes: 40 additions & 0 deletions docs/swagger.json
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"
}
}
}
}
}
}
24 changes: 24 additions & 0 deletions docs/swagger.yaml
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"
20 changes: 19 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
Loading
Loading