Help us shape the future of Go! Take the 2026 Go Developer Survey

api

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: Apache-2.0 Imports: 30 Imported by: 14

Documentation

Index

Constants

This section is empty.

Variables

View Source
var APIs = map[string]*API{}

APIs is the loaded API list

View Source
var HTTPGuards = map[string]gin.HandlerFunc{}

HTTPGuards 支持的中间件

Functions

func AddGuard

func AddGuard(name string, guard gin.HandlerFunc)

AddGuard add guard

func BuildHandler added in v1.0.0

func BuildHandler(http HTTP, path Path) gin.HandlerFunc

BuildHandler builds a gin.HandlerFunc for the given HTTP and Path configuration This is the public API for building handlers dynamically

func BuildRouteTable added in v1.0.0

func BuildRouteTable()

BuildRouteTable builds the route table from loaded APIs Should be called after loading APIs and before using FindHandler

func FindHandler added in v1.0.0

func FindHandler(method, path string) (*API, *Path, gin.HandlerFunc, map[string]string, error)

FindHandler finds a handler by method and path from the route table Returns the API, Path, Handler, extracted parameters, and error

func IsAllowed

func IsAllowed(c *gin.Context, allowsMap map[string]bool) bool

IsAllowed check if the referer is in allow list

func ProcessGuard

func ProcessGuard(name string, cors ...gin.HandlerFunc) gin.HandlerFunc

ProcessGuard guard process

func ReloadAPIs added in v1.0.0

func ReloadAPIs(root string) error

ReloadAPIs reloads all API definitions from the specified directory This function is thread-safe and performs atomic replacement of both APIs and the route table

func SetGuards

func SetGuards(guards map[string]gin.HandlerFunc)

SetGuards set guards

func SetRoutes

func SetRoutes(router *gin.Engine, path string, allows ...string)

SetRoutes set the api routes

Types

type API

type API struct {
	ID   string `jsong:"id"`
	Name string
	File string
	Type string
	HTTP HTTP
}

API 数据接口

func Load

func Load(file, id string, guard ...string) (*API, error)

Load load the api from file, parse and register it to the global APIs map

func LoadSource added in v1.0.0

func LoadSource(file string, data []byte, id string, guard ...string) (*API, error)

LoadSource parses API source data, registers it to the global APIs map, and returns the *API

func Select

func Select(id string) *API

Select select api

func (*API) Reload

func (api *API) Reload() (*API, error)

Reload reloads a single API definition from its file

type HTTP

type HTTP struct {
	Name        string `json:"name"`
	Version     string `json:"version"`
	Description string `json:"description,omitempty"`
	Group       string `json:"group,omitempty"`
	Guard       string `json:"guard,omitempty"`
	Paths       []Path `json:"paths,omitempty"`
}

HTTP http 协议服务

func (HTTP) Route

func (http HTTP) Route(router gin.IRoutes, path Path, allows ...string)

Route 路径配置转换为路由

func (HTTP) Routes

func (http HTTP) Routes(router *gin.Engine, path string, allows ...string)

Routes 配置转换为路由

type Out

type Out struct {
	Status   int               `json:"status"`
	Type     string            `json:"type,omitempty"`
	Body     interface{}       `json:"body,omitempty"`
	Headers  map[string]string `json:"headers,omitempty"`
	Redirect *Redirect         `json:"redirect,omitempty"`
}

Out http 输出

type Path

type Path struct {
	Label          string        `json:"label,omitempty"`
	Description    string        `json:"description,omitempty"`
	Path           string        `json:"path"`
	Method         string        `json:"method"`
	Process        string        `json:"process"`
	Guard          string        `json:"guard,omitempty"`
	In             []interface{} `json:"in,omitempty"`
	Out            Out           `json:"out,omitempty"`
	ProcessHandler bool          `json:"processHandler,omitempty"`
}

Path HTTP Path

type Redirect

type Redirect struct {
	Code     int    `json:"code,omitempty"`
	Location string `json:"location,omitempty"`
}

Redirect out redirect

type RouteEntry added in v1.0.0

type RouteEntry struct {
	Pattern string         // Original route pattern, e.g., /user/:id
	Regex   *regexp.Regexp // Compiled regex for matching
	Params  []string       // Parameter names, e.g., ["id"]
	API     *API           // Reference to the API definition
	Path    *Path          // Reference to the Path definition
}

RouteEntry represents a single route entry

type RouteTable added in v1.0.0

type RouteTable struct {
	// contains filtered or unexported fields
}

RouteTable is a thread-safe route table for dynamic route lookup

Jump to

Keyboard shortcuts

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