router

package module
v0.0.0-...-c42a961 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2023 License: MIT Imports: 6 Imported by: 0

README

Router GO Library

Install

import:
- package: github.com/best-expendables-v2/router
  version: x.x.x

Middlewares

  • middleware.Authentication
  • middleware.Authorized
  • middleware.OnlyRoles
  • middleware.ACL
  • middleware.AccessLog
  • middleware.Prometheus
  • middleware.Recoverer
  • middleware.OpenTrace
  • middleware.RequestID [DEPRECATED]

Initialization

Function router.New() returns router with configured list of middlewares.

func main() {
	loggerFactory := logger.NewLoggerFactory(logger.InfoLevel),
	
	config := router.Configuration{
		LoggerFactory: loggerFactory,
		PanicHandler: panicHandler,
	}
	
	mux, err := router.New(config)
	...
}
Configuration parameters:

Namespace [REQUIRED]
Service name (namespace) from k8n.

LoggerFactory [REQUIRED]
This factory using for "context-logger".

PanicHandler
Panic will be handled by this handler.

Tracer
Tracer for OpenTrace. Should be nil. By default middleware will use GlobalTracer and this is enough for most of our use-cases.

DisableAccessLog
Disable access log

Custom router

For exiting code you can initialize middlewares by yourself.
You have to follow this order of middlewares on for router initialization:

  • Recoverer
  • RequestID
  • Authentication
  • ContextLogger
  • AccessLog
Panic recovery

For the panic case, recoverer does not responsible for error response, it only sends HTTP response code 500. For response with error messages you have to define panic handler and pass it as argument. Example

AccessLog

Improved ReqResLogger which write log into JSON format and ignore binary(non-text) body.

Authentication & Authorization

Authentication middleware receive user information only and set it into the context.
ACL middleware using for access control.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(cfg Configuration) (chi.Router, error)

New returns router with default list of middlewares

Types

type Configuration

type Configuration struct {
	// LoggerFactory using in ContextLogger middleware
	LoggerFactory logger.Factory

	// NewrelicApp
	NewrelicApp newrelic.Application

	// PanicHandler optional parameter
	// On nil panic returns only 500 status code
	PanicHandler middleware.PanicHandler

	AccessLog struct {
		// Disable access log middleware
		Disable bool
	}
}

Configuration router configuration

Directories

Path Synopsis
_examples
internal
chi
pkg
net

Jump to

Keyboard shortcuts

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