router

package
v1.9.8 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2022 License: MIT Imports: 10 Imported by: 0

README

Package Router

This package help to manage routers in an API. This package requires go Gin Tonic API Framework.

By default, Gin Tonic API need a main package to register all handler func into the gin engine. This way is not easy usable with many people who's can add routers or exploit them. In add, middleware isn't defined as real middleware but more than cascaded call of all registered handler for a route.

This package allows to change this process :

  • auth allow to register a real middleware who's can manage call of other handler for a route
  • register implement a global collector of handler with router / group and called into init func of yours routers directly
  • router implements the global registrar of all registered handler with route and group, ordered by group
  • ...

Example of implementation

We will work on an example of file/folder tree like this :

/
  bin/
    api/
      config/
      routers/
        routers.go

in the routers.go file, we will implement the router package call :

package routers

import (
	"github.com/nabbar/golib/router"

    "myapp/bin/api/config"
)

var (
	RouterList = router.NewRouterList()
)

func Run() {
	config.GetConfig().ServerListen(router.Handler(RouterList))
}

This variable RouterList will be call by all routers. Note you will just need to call your routers' packages into a main router like this :

package main

import (
    _ "myapp/bin/api/routers/status"
    _ "myapp/bin/api/routers/static"
  // ... add all your packages with an init register
  // careful: do not add this import into your routers.go package to avoid circular import
)

Documentation

Index

Constants

View Source
const (
	AUTH_CODE_SUCCESS = iota
	AUTH_CODE_REQUIRE
	AUTH_CODE_FORBIDDEN
)
View Source
const (
	HEAD_AUTH_REQR = "WWW-Authenticate"
	HEAD_AUTH_SEND = "Authorization"
	HEAD_AUTH_REAL = "Basic realm=LDAP Authorization Required"
)
View Source
const (
	ErrorParamEmpty liberr.CodeError = iota + liberr.MinPkgRouter
	ErrorHeaderAuth
	ErrorHeaderAuthMissing
	ErrorHeaderAuthEmpty
	ErrorHeaderAuthRequire
	ErrorHeaderAuthForbidden
)
View Source
const (
	EmptyHandlerGroup           = "<nil>"
	GinContextStartUnixNanoTime = "gin-ctx-start-unix-nano-time"
	GinContextRequestPath       = "gin-ctx-request-path"
)

Variables

This section is empty.

Functions

func AuthForbidden

func AuthForbidden(c *gin.Context, err error)

func AuthRequire

func AuthRequire(c *gin.Context, err error)

func DefaultGinInit added in v1.6.0

func DefaultGinInit() *gin.Engine

func DefaultGinWithTrustedPlatform added in v1.7.4

func DefaultGinWithTrustedPlatform(trustedPlatform string) *gin.Engine

func DefaultGinWithTrustyProxy added in v1.6.0

func DefaultGinWithTrustyProxy(trustyProxy []string) *gin.Engine

func GinAccessLog added in v1.9.8

func GinAccessLog(log liblog.FuncLog) gin.HandlerFunc

func GinAddGlobalMiddleware added in v1.9.8

func GinAddGlobalMiddleware(eng *gin.Engine, middleware ...gin.HandlerFunc) *gin.Engine

func GinEngine added in v1.9.8

func GinEngine(trustedPlatform string, trustyProxy ...string) (*gin.Engine, error)

func GinErrorLog added in v1.9.8

func GinErrorLog(log liblog.FuncLog) gin.HandlerFunc

func GinLatencyContext added in v1.9.8

func GinLatencyContext(c *gin.Context)

func GinRequestContext added in v1.9.8

func GinRequestContext(c *gin.Context)

func Handler

func Handler(routerList RouterList) http.Handler

func RoutersHandler

func RoutersHandler(engine *gin.Engine)

func RoutersRegister

func RoutersRegister(method string, relativePath string, router ...gin.HandlerFunc)

func RoutersRegisterInGroup

func RoutersRegisterInGroup(group, method string, relativePath string, router ...gin.HandlerFunc)

func SetGinHandler

func SetGinHandler(fct func(c *gin.Context)) gin.HandlerFunc

SetGinHandler func that return given func as ginTonic HandlerFunc interface type.

Types

type AuthCode

type AuthCode uint8

type Authorization

type Authorization interface {
	Handler(c *gin.Context)
	Register(router ...gin.HandlerFunc) gin.HandlerFunc
	Append(router ...gin.HandlerFunc)
}

func NewAuthorization

func NewAuthorization(HeadAuthType string, authCheckFunc func(AuthHeader string) (AuthCode, errors.Error)) Authorization

type Headers

type Headers interface {
	Add(key, value string)
	Set(key, value string)
	Get(key string) string
	Del(key string)

	Header() map[string]string
	Register(router ...gin.HandlerFunc) []gin.HandlerFunc
	Handler(c *gin.Context)

	Clone() Headers
}

func NewHeaders

func NewHeaders() Headers

type HeadersConfig added in v1.5.0

type HeadersConfig map[string]string

func (HeadersConfig) New added in v1.5.0

func (h HeadersConfig) New() Headers

type RegisterRouter

type RegisterRouter func(method string, relativePath string, router ...gin.HandlerFunc)

type RegisterRouterInGroup

type RegisterRouterInGroup func(group, method string, relativePath string, router ...gin.HandlerFunc)

type RouterList

type RouterList interface {
	Register(method string, relativePath string, router ...gin.HandlerFunc)
	RegisterInGroup(group, method string, relativePath string, router ...gin.HandlerFunc)
	Handler(engine *gin.Engine)
	Engine() *gin.Engine
}

func NewRouterList

func NewRouterList(initGin func() *gin.Engine) RouterList

Jump to

Keyboard shortcuts

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