router

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2020 License: MIT Imports: 6 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 (
	EMPTY_PARAMS errors.CodeError = iota + errors.MIN_PKG_Router
	HEADER_AUTH_MISSING
	HEADER_AUTH_EMPTY
	HEADER_AUTH_REQUIRE
	HEADER_AUTH_FORBIDDEN
	HEADER_AUTH_ERROR
)
View Source
const EMPTY_GROUP = "<nil>"

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 Handler

func Handler(routerList RouterList) http.Handler

func IsCodeError

func IsCodeError() bool

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 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)
}

func NewRouterList

func NewRouterList() RouterList

Jump to

Keyboard shortcuts

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