wago

package module
v0.0.0-...-3aeb0ce Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2019 License: Apache-2.0 Imports: 21 Imported by: 0

README

wago

(Maybe will be) The easiest-to-use web framework for Go. Auto detect and register router to Gin, and some useful features will be supported.

Documentation

Index

Constants

View Source
const (
	RUN_MODE_DEBUG   = "debug"
	RUN_MODE_TEST    = "test"
	RUN_MODE_RELEASE = "release"

	ROUTER_MODE_AUTO    = "auto"
	ROUTER_MODE_COMMENT = "comment"

	// PanicLevel level, highest level of severity. Logs and then calls panic with the
	// message passed to Debug, Info, ...
	LevelPanic = logrus.PanicLevel
	// FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
	// logging level is set to Panic.
	LevelFatal = logrus.FatalLevel
	// ErrorLevel level. Logs. Used for errors that should definitely be noted.
	// Commonly used for hooks to send errors to an error tracking service.
	LevelError = logrus.ErrorLevel
	// WarnLevel level. Non-critical entries that deserve eyes.
	LevelWarn = logrus.WarnLevel
	// InfoLevel level. General operational entries about what's going on inside the
	// application.
	LevelInfo = logrus.InfoLevel
	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
	LevelDebug = logrus.DebugLevel
	// TraceLevel level. Designates finer-grained informational events than the Debug.
	LevelTrace = logrus.TraceLevel
)
View Source
const (
	REQUEST_ID = "W-Request-Id"
)

Variables

View Source
var (
	CommentRouters = make(map[string][]CommentRouter)

	// HTTP_METHOD list the supported http methods.
	HTTP_METHOD = map[string]bool{
		"GET":       true,
		"POST":      true,
		"PUT":       true,
		"DELETE":    true,
		"PATCH":     true,
		"OPTIONS":   true,
		"HEAD":      true,
		"TRACE":     true,
		"CONNECT":   true,
		"MKCOL":     true,
		"COPY":      true,
		"MOVE":      true,
		"PROPFIND":  true,
		"PROPPATCH": true,
		"LOCK":      true,
		"UNLOCK":    true,
	}

	// while RouterMode = auto, don't register struct method to router
	EXCLUDE_ROUTER_METHOD = map[string]bool{
		"Init": true,
	}
)
View Source
var (
	AppConfig = &Config{}
)

Functions

func AddRouterGroups

func AddRouterGroups(rgs ...*RouterGroup)

Add router groups

func AllowOriginFunc

func AllowOriginFunc(f func(string) bool)

set CORS AllowOriginFunc

func HandlerWrapper

func HandlerWrapper(controllerType reflect.Type, method string) gin.HandlerFunc

HandlerWrapper is a wrapper to transform controller'method to gin.HandlerFunc @TODO catch exceptions while method not exist encapsulate controller's method in gin.HandlerFunc means: while gin.HandlerFunc is invoked, the target controller's method will be invoked

func ParseRouter

func ParseRouter(rootDir ...string)

rootDir, default is "controller"

func Serve

func Serve()

Boot Wago app

func Use

func Use(middleware ...MiddleWareHandler) gin.IRoutes

Use attaches a global middleware to the router. ie. the middleware attached though Use() will be included in the handlers chain for every single request. Even 404, 405, static files... For example, this is the right place for a logger or error management middleware.

Types

type App

type App struct {
	App     string
	RunMode string

	// router mode, [auto, comment] supported.
	RouterMode string

	// only when RouterMode=comment, ControllerPath is valid.
	ControllerPath string

	// only when RouterMode=auto, RouterSep is valid.
	// only ”(empty), '-' or '_' allowed.
	// ” means use struct/method name as router path, eg: /v1/HomeTest/HelloWorld
	// '-' means use snake string as router path, eg: /v1/home-test/hello-world
	// '_' means use snake string as router path, eg: /v1/home_test/hello_world
	RouterSep string
}

application configuration

type CommentRouter

type CommentRouter struct {
	Method     string
	Router     string
	HTTPMethod []string
}

type Config

type Config struct {
	toml.Tree

	// app configuration
	App App

	// log configuration
	Log Log

	// HTTP server configuration
	Server Server
}

type Context

type Context = gin.Context

type Controller

type Controller struct {
	// context of current request
	Ctx *Context

	// controller logger
	// init request ID
	Logger *logger.Entry
}

func (*Controller) Init

func (t *Controller) Init(c *Context)

init request context

func (*Controller) RequestId

func (t *Controller) RequestId() string

get request id from context

type ControllerCommentRouter

type ControllerCommentRouter struct {
	Pkg            string // name of current controller's pkg name, e.g: package home, Pkg="home"
	Controller     string
	CommentRouters []CommentRouter
}

type Cors

type Cors struct {
	AllowOrigins     []string
	AllowMethods     []string
	AllowHeaders     []string
	ExposeHeaders    []string
	AllowCredentials bool
	AllowOriginFunc  func(string) bool
	MaxAge           int
}

HTTP CORS configuration

type IController

type IController interface {
	Init(*Context)
}

type Log

type Log struct {
	// [json,text,logstash,fluentd] supported
	Formatter string

	// Log level
	Level uint32

	// logging method name
	LogMethodName bool

	// Filename is the file to write logs to.  Backup log files will be retained
	// in the same directory.  It uses <processname>-lumberjack.log in
	// os.TempDir() if empty.
	Filename string

	// MaxSize is the maximum size in megabytes of the log file before it gets
	// rotated. It defaults to 100 megabytes.
	MaxSize int

	// MaxBackups is the maximum number of old log files to retain.  The default
	// is to retain all old log files (though MaxAge may still cause them to get
	// deleted.)
	MaxBackups int

	// MaxAge is the maximum number of days to retain old log files based on the
	// timestamp encoded in their filename.  Note that a day is defined as 24
	// hours and may not exactly correspond to calendar days due to daylight
	// savings, leap seconds, etc. The default is not to remove old log files
	// based on age.
	MaxAge int

	// Compress determines if the rotated log files should be compressed
	// using gzip.
	Compress bool

	// print log to console
	Console bool
}

log configurations

type MiddleWareHandler

type MiddleWareHandler = gin.HandlerFunc

type RouterGroup

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

add controller instance to set routers

func NewRouterGroup

func NewRouterGroup() *RouterGroup

func (*RouterGroup) Controller

func (t *RouterGroup) Controller(controllers ...IController) *RouterGroup

config controller routers

func (*RouterGroup) Prefix

func (t *RouterGroup) Prefix(prefix string) *RouterGroup

set api prefixes

func (*RouterGroup) Use

func (t *RouterGroup) Use(middleWares ...MiddleWareHandler) *RouterGroup

bind middleWares

type Server

type Server struct {
	Port              int
	Host              string
	ReadTimeout       int
	ReadHeaderTimeout int
	WhiteTimeout      int
	IdleTimeout       int
	MaxHeaderBytes    int

	// HTTP CORS configuration
	Cors Cors
}

TLSConfig *tls.Config TLSNextProto map[string]func(*Server, *tls.Conn, Handler) ConnState func(net.Conn, ConnState) HTTP server configuration

type Wago

type Wago struct {
	// Use gin as http server
	Server *gin.Engine
	// contains filtered or unexported fields
}
var (
	WagoApp *Wago
)

func NewWago

func NewWago() *Wago

func (*Wago) ServeHTTP

func (t *Wago) ServeHTTP(w http.ResponseWriter, req *http.Request)

type WrapperFunc

type WrapperFunc func()

Wrapper for controller functions which is type of gin.HandlerFunc WagoHandler gin.HandlerFunc

Directories

Path Synopsis
uuid
Package uniuri generates random strings good for use in URIs to identify unique objects.
Package uniuri generates random strings good for use in URIs to identify unique objects.

Jump to

Keyboard shortcuts

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