router

package
v1.15.1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 11 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 (
	ErrorParamEmpty liberr.CodeError = iota + liberr.MinPkgRouter
	ErrorConfigValidator
	ErrorHeaderAuth
	ErrorHeaderAuthMissing
	ErrorHeaderAuthEmpty
	ErrorHeaderAuthRequire
	ErrorHeaderAuthForbidden
)
View Source
const (
	EmptyHandlerGroup           = "<nil>"
	GinContextStartUnixNanoTime = "gin-ctx-start-unix-nano-time"
	GinContextRequestPath       = "gin-ctx-request-path"
	GinContextRequestUser       = "gin-ctx-request-user"
)

Variables

This section is empty.

Functions

func DefaultGinInit added in v1.6.0

func DefaultGinInit() *ginsdk.Engine

func DefaultGinWithTrustedPlatform added in v1.7.4

func DefaultGinWithTrustedPlatform(trustedPlatform string) *ginsdk.Engine

func DefaultGinWithTrustyProxy added in v1.6.0

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

func GinAccessLog added in v1.9.8

func GinAccessLog(log liblog.FuncLog) ginsdk.HandlerFunc

func GinAddGlobalMiddleware added in v1.9.8

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

func GinEngine added in v1.9.8

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

func GinErrorLog added in v1.9.8

func GinErrorLog(log liblog.FuncLog) ginsdk.HandlerFunc

func GinLatencyContext added in v1.9.8

func GinLatencyContext(c *ginsdk.Context)

func GinRequestContext added in v1.9.8

func GinRequestContext(c *ginsdk.Context)

func Handler

func Handler(routerList RouterList) http.Handler

func RoutersHandler

func RoutersHandler(engine *ginsdk.Engine)

func RoutersRegister

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

func RoutersRegisterInGroup

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

func SetGinHandler

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

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

Types

type RegisterRouter

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

type RegisterRouterInGroup

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

type RouterList

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

func NewRouterList

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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