Skip to content

Commit

Permalink
init http server scaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
fanhousanbu committed Feb 29, 2024
1 parent 28cf132 commit 2ab6b61
Show file tree
Hide file tree
Showing 24 changed files with 739 additions and 64 deletions.
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)
34 changes: 26 additions & 8 deletions cmd/server/main.go
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.
52 changes: 52 additions & 0 deletions conf/conf.go
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
}
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 // 环境名称
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"
}(),
}
}
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

0 comments on commit 2ab6b61

Please sign in to comment.