ginx

package module
v0.0.0-...-018b13d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 24, 2024 License: MIT Imports: 12 Imported by: 0

README

ginx

go get github.com/DaHuangQwQ/ginx

样例代码 https://github.com/DaHuangQwQ/ginx/tree/main/example

  1. 基于反射实现的自动 api 文档生成
type UserGetReq struct {
	ginx.Meta `method:"GET" path:"users/:id"`
	Id        int `json:"id" validate:"required,min=1,max=32"`
}
  1. jwt中间件
  2. 限流中间件
  3. 可观测中间件
  4. 简化代码
package main

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

type UserGetReq struct {
	ginx.Meta `method:"GET" path:"/users/:id"`
	Id        int `json:"id" uri:"id" validate:"required,min=1,max=32"`
}

type UserGetRes struct {
	Code int `json:"code"`
}

func getUser(ctx *gin.Context, req UserGetReq) (ginx.Result[UserGetRes], error) {
	return ginx.Result[UserGetRes]{
		Code: 0,
		Msg:  "ok",
		Data: UserGetRes{
			Code: req.Id,
		},
	}, nil
}

func main() {
	server := ginx.NewServer(":8080")
	server.Handle(ginx.Wrap[UserGetReq, UserGetRes](getUser))
	_ = server.Start()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitCounter

func InitCounter(opt prometheus.CounterOpts)

func NewWarpLogger

func NewWarpLogger(l Logger)

func Wrap

func Wrap[Req any, Res any](fn func(ctx *gin.Context, req Req) (Result[Res], error)) (string, string, gin.HandlerFunc)

func WrapWithToken

func WrapWithToken[Req any, Res any](fn func(ctx *gin.Context, req Req, u UserClaims) (Result[Res], error)) (string, string, gin.HandlerFunc)

Types

type Field

type Field struct {
	Key string
	Val any
}

type Limiter

type Limiter interface {
	Limit(ctx context.Context, key string) (bool, error)
}

type Logger

type Logger interface {
	Debug(msg string, args ...Field)
	Info(msg string, args ...Field)
	Warn(msg string, args ...Field)
	Error(msg string, args ...Field)
}
var (
	L   Logger
	Oai = openapi.NewOpenAPI()
)

type Meta

type Meta struct{}

type Result

type Result[T any] struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data T      `json:"data"`
}

type Server

type Server struct {
	*gin.Engine
	OpenAPI *openapi.OpenAPI
	// contains filtered or unexported fields
}

func NewServer

func NewServer(addr string, opts ...gin.OptionFunc) *Server

func (*Server) Handle

func (s *Server) Handle(method, path string, handler gin.HandlerFunc)

func (*Server) MarshalSpec

func (s *Server) MarshalSpec() ([]byte, error)

func (*Server) RegisterOpenAPIRoutes

func (s *Server) RegisterOpenAPIRoutes(path string)

RegisterOpenAPIRoutes Registers the routes to serve the OpenAPI spec and Swagger UI.

func (*Server) SaveOpenAPIToFile

func (s *Server) SaveOpenAPIToFile(path string) error

func (*Server) Start

func (s *Server) Start() error

func (*Server) Use

func (s *Server) Use(middleware ...gin.HandlerFunc)

type UserClaims

type UserClaims struct {
	Id        int64
	UserAgent string
	Ssid      string
	jwt.RegisteredClaims
}

Directories

Path Synopsis
example
hello_world command
jwt command
prometheus command
ratelimit command
middleware

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL