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

Dylan/feature/v0.1 executor #13

Merged
merged 8 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
12 changes: 8 additions & 4 deletions conf/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
"strings"
)

const envKey = "Env"
const ProdEnv = "prod"
const DevEnv = "dev"
cherry-yl-sh marked this conversation as resolved.
Show resolved Hide resolved

type Env struct {
Name string // env Name, like `prod`, `dev` and etc.,
Debugger bool // whether to use debugger
Expand Down Expand Up @@ -34,14 +38,14 @@ func getConfFilePath() *string {
var Environment *Env

func init() {
envName := "prod"
if len(os.Getenv("Env")) > 0 {
envName = os.Getenv("Env")
envName := ProdEnv
if len(os.Getenv(envKey)) > 0 {
envName = os.Getenv(envKey)
}
Environment = &Env{
Name: envName,
Debugger: func() bool {
return envName != "prod"
return envName != ProdEnv
}(),
}
}
19 changes: 19 additions & 0 deletions rpc_server/api/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package api

import (
"AAStarCommunity/EthPaymaster_BackService/conf"
"AAStarCommunity/EthPaymaster_BackService/rpc_server/models"
"github.com/gin-gonic/gin"
"time"
)

func Health(c *gin.Context) {
cherry-yl-sh marked this conversation as resolved.
Show resolved Hide resolved
response := models.GetResponse()
response.WithDataSuccess(c, gin.H{
"hello": "Eth Paymaster",
"environment": conf.Environment.Name,
"time": time.Now(),
"version": "v1.0.0",
})
response.Success(c)
}
10 changes: 10 additions & 0 deletions rpc_server/middlewares/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package middlewares

import (
"github.com/gin-gonic/gin"
)

// LogHandler log handler
func LogHandler() gin.HandlerFunc {
return gin.Logger()
}
4 changes: 2 additions & 2 deletions rpc_server/middlewares/rate_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const (

var limiter map[string]*rate.Limiter

// RateLimiterByApiKey represents the rate limit by each ApiKey for each api calling
func RateLimiterByApiKey() gin.HandlerFunc {
// RateLimiterByApiKeyHandler represents the rate limit by each ApiKey for each api calling
func RateLimiterByApiKeyHandler() gin.HandlerFunc {
return func(ctx *gin.Context) {
if exists, current := utils.CurrentUser(ctx); exists {

Expand Down
4 changes: 2 additions & 2 deletions rpc_server/middlewares/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"strings"
)

// GenericRecovery represents the generic error(panic) process
func GenericRecovery() gin.HandlerFunc {
// GenericRecoveryHandler represents the generic error(panic) process
func GenericRecoveryHandler() gin.HandlerFunc {
DefaultErrorWriter := &PanicExceptionRecord{}
return gin.RecoveryWithWriter(DefaultErrorWriter, func(c *gin.Context, err interface{}) {
errStr := ""
Expand Down
41 changes: 24 additions & 17 deletions rpc_server/routers/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,44 @@ import (
func SetRouters() (routers *gin.Engine) {
routers = gin.New()

// middlewares
handlers := make([]gin.HandlerFunc, 0)
handlers = append(handlers, middlewares.GenericRecovery())
buildMod(routers)
buildRoute(routers)
routers.NoRoute(func(ctx *gin.Context) {
models.GetResponse().SetHttpCode(http.StatusNotFound).FailCode(ctx, http.StatusNotFound)
})

return
}
func buildRoute(routers *gin.Engine) {
// build http routers and middleware
routers.Use(middlewares.GenericRecoveryHandler())
if conf.Environment.IsDevelopment() {
handlers = append(handlers, gin.Logger())
routers.Use(middlewares.LogHandler())
}
handlers = append(handlers, middlewares.CorsHandler())
routers.Use(middlewares.CorsHandler())
//build the routers not need api access like auth or Traffic limit
buildRouters(routers, RouterNotAPIAccessMaps)

routers.Use(middlewares.AuthHandler())
routers.Use(middlewares.RateLimiterByApiKeyHandler())
buildRouters(routers, RouterMaps)
}

func buildMod(routers *gin.Engine) {

// prod mode
if conf.Environment.IsProduction() {
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = io.Discard // disable gin log
return
}

// dev mod
if conf.Environment.IsDevelopment() {
gin.SetMode(gin.DebugMode)
buildSwagger(routers)
return
}

// use middlewares
routers.Use(handlers...)

// build http routers
buildRouters(routers)

routers.NoRoute(func(ctx *gin.Context) {
models.GetResponse().SetHttpCode(http.StatusNotFound).FailCode(ctx, http.StatusNotFound)
})

return
}

// buildSwagger build swagger
Expand Down
12 changes: 2 additions & 10 deletions rpc_server/routers/builder.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
package routers

import (
"AAStarCommunity/EthPaymaster_BackService/rpc_server/api"
"AAStarCommunity/EthPaymaster_BackService/rpc_server/middlewares"
"github.com/gin-gonic/gin"
)

// buildRouters Build Routers
func buildRouters(router *gin.Engine) {

router.POST("api/auth", api.Auth)

router.Use(middlewares.AuthHandler())
func buildRouters(router *gin.Engine, routerMaps []RouterMap) {
{
cherry-yl-sh marked this conversation as resolved.
Show resolved Hide resolved
router.Use(middlewares.RateLimiterByApiKey())

for _, routerMap := range RouterMaps {
for _, routerMap := range routerMaps {
for _, method := range routerMap.Methods {
if method == GET {
router.GET(routerMap.Url, routerMap.Func)
Expand Down
cherry-yl-sh marked this conversation as resolved.
Show resolved Hide resolved
File renamed without changes.
8 changes: 8 additions & 0 deletions rpc_server/routers/router_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package routers

import "testing"

func TestConst(t *testing.T) {

t.Logf("%s", string(TryPayUserOperation))
}
13 changes: 0 additions & 13 deletions rpc_server/routers/routers.go

This file was deleted.

29 changes: 29 additions & 0 deletions rpc_server/routers/routersMap.go
cherry-yl-sh marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package routers

import (
"AAStarCommunity/EthPaymaster_BackService/rpc_server/api"
v1 "AAStarCommunity/EthPaymaster_BackService/rpc_server/api/v1"
)

var RouterMaps []RouterMap
var RouterNotAPIAccessMaps []RouterMap
cherry-yl-sh marked this conversation as resolved.
Show resolved Hide resolved

func init() {
RouterMaps = make([]RouterMap, 0)

RouterMaps = append(RouterMaps, RouterMap{string(TryPayUserOperation), []RestfulMethod{POST}, v1.TryPayUserOperation})
RouterMaps = append(RouterMaps, RouterMap{string(GetSupportStrategy), []RestfulMethod{GET}, v1.GetSupportStrategy})
RouterMaps = append(RouterMaps, RouterMap{string(GetSupportEntrypoint), []RestfulMethod{GET}, v1.GetSupportEntrypoint})
RouterNotAPIAccessMaps = append(RouterMaps, RouterMap{string(Auth), []RestfulMethod{POST}, api.Auth})
cherry-yl-sh marked this conversation as resolved.
Show resolved Hide resolved
RouterNotAPIAccessMaps = append(RouterMaps, RouterMap{string(Health), []RestfulMethod{GET}, api.Health})
cherry-yl-sh marked this conversation as resolved.
Show resolved Hide resolved
}

type Path string

const (
TryPayUserOperation Path = "api/v1/try-pay-user-operation"
GetSupportStrategy Path = "api/v1/get-support-strategy"
GetSupportEntrypoint Path = "api/v1/get-support-entrypoint"
Auth Path = "api/auth"
Health Path = "api/health"
)
Loading