coco

package module
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2023 License: MIT Imports: 19 Imported by: 0

README

coco

a simple web framework base on gin and viper.

supports followed features

  • graceful shutdown
  • shutdown hook
  • boot hook
  • port hook

Install

go get github.com/dstgo/coco

Quickstart

coco.SetMode(coco.ReleaseMode)
c := coco.New()
c.Run()

Documentation

Index

Constants

View Source
const (
	ReleaseMode GinMode = "release"
	DebugMode           = "debug"
	Test                = "test"
)

Variables

View Source
var MetaKey = "meta"

MetaKey meta key, could update if you need

Functions

func BindPairs added in v1.0.6

func BindPairs(ctx *gin.Context, pairs ...BindPair) error

func MetaHandler added in v1.0.12

func MetaHandler(meta Meta) gin.HandlerFunc

func SetMode

func SetMode(mode GinMode)

SetMode it should be set before httpServer starting param mode GinMode

Types

type BindPair added in v1.0.6

type BindPair struct {
	B binding.Binding
	V Validator
}

func Pair added in v1.0.6

func Pair(b binding.Binding, val Validator) BindPair

type Coco

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

Coco coco http server

func New added in v1.0.4

func New(opts ...ComponentFn) *Coco

New create new coco with options param opts ...ComponentFn return *Coco

func NewWithCore added in v1.0.4

func NewWithCore(core Core) *Coco

NewWithCore param Core *Core return *Coco with custom Core

func (*Coco) AddInterruptFn

func (c *Coco) AddInterruptFn(fn ...InterruptFn)

func (*Coco) AddPostAsyncCs

func (c *Coco) AddPostAsyncCs(cs ...ComponentFn)

func (*Coco) AddPreAsyncCs

func (c *Coco) AddPreAsyncCs(cs ...ComponentFn)

func (*Coco) AddPreSyncCs

func (c *Coco) AddPreSyncCs(cs ...ComponentFn)

func (*Coco) Core added in v1.0.9

func (c *Coco) Core() Core

func (*Coco) Run

func (c *Coco) Run() error

func (*Coco) RunListener added in v1.0.4

func (c *Coco) RunListener(l net.Listener) error

func (*Coco) RunListenerTls added in v1.0.4

func (c *Coco) RunListenerTls(l net.Listener, certFile, keyFile string) error

func (*Coco) RunTLS

func (c *Coco) RunTLS(certFile, keyFile string) error

func (*Coco) SetSignals

func (c *Coco) SetSignals(s ...os.Signal)

type ComponentFn

type ComponentFn func(coco *Core)

func WithConfig added in v1.0.4

func WithConfig(cfg *Config) ComponentFn

func WithCtx added in v1.0.4

func WithCtx(ctx context.Context) ComponentFn

func WithEngine added in v1.0.10

func WithEngine(e *gin.Engine) ComponentFn

func WithLogger added in v1.0.4

func WithLogger(logger *logrus.Logger) ComponentFn

func WithMiddleware added in v1.0.4

func WithMiddleware(opts ...MiddlewareOpt) ComponentFn

func WithServer added in v1.0.4

func WithServer(server *http.Server) ComponentFn

func WithValidator added in v1.0.4

func WithValidator(vFn ValidateComp) ComponentFn

type Config

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

func DefaultConfig

func DefaultConfig() *Config

func NewConfigFile added in v1.0.4

func NewConfigFile(file string) *Config

func NewConfigReader added in v1.0.4

func NewConfigReader(reader io.Reader, cfgType string) *Config

func (*Config) ReadConfig

func (c *Config) ReadConfig() error

func (*Config) SetCfgType added in v1.0.4

func (c *Config) SetCfgType(t string)

func (*Config) Viper

func (c *Config) Viper() *viper.Viper

type Core

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

Core coco Core of http server

func (*Core) Cfg added in v1.0.5

func (c *Core) Cfg() *Config

func (*Core) Ctx

func (c *Core) Ctx() context.Context

func (*Core) Engine

func (c *Core) Engine() *gin.Engine

func (*Core) L added in v1.0.5

func (c *Core) L() *logrus.Logger

func (*Core) Server

func (c *Core) Server() *http.Server

type GinMode

type GinMode = string

func Mode

func Mode() GinMode

type InterruptFn

type InterruptFn func(coco *Core, signal os.Signal)

func ShutdownWithCloseHttp

func ShutdownWithCloseHttp() InterruptFn

func ShutdownWithInfo

func ShutdownWithInfo() InterruptFn

type Meta added in v1.0.12

type Meta = map[string]any

Meta route meta info

func MetaFromCtx added in v1.0.12

func MetaFromCtx(ctx *gin.Context) Meta

type MiddlewareOpt added in v1.0.4

type MiddlewareOpt = func(coco *Core) gin.HandlerFunc

type Router added in v1.0.12

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

func NewRouter added in v1.0.12

func NewRouter(engine *gin.Engine) *Router

func (*Router) Any added in v1.0.12

func (w *Router) Any(path string, meta Meta, handler ...gin.HandlerFunc) []RouterInfo

func (*Router) DELETE added in v1.0.12

func (w *Router) DELETE(path string, meta Meta, handler ...gin.HandlerFunc) RouterInfo

func (*Router) GET added in v1.0.12

func (w *Router) GET(path string, meta Meta, handler ...gin.HandlerFunc) RouterInfo

func (*Router) Group added in v1.0.12

func (w *Router) Group(path string, meta Meta, handler ...gin.HandlerFunc) RouterGroup

func (*Router) HEAD added in v1.0.12

func (w *Router) HEAD(path string, meta Meta, handler ...gin.HandlerFunc) RouterInfo

func (*Router) Handle added in v1.0.12

func (w *Router) Handle(method, path string, meta Meta, handler ...gin.HandlerFunc) RouterInfo

func (*Router) Match added in v1.0.12

func (w *Router) Match(method []string, path string, meta Meta, handler ...gin.HandlerFunc) []RouterInfo

func (*Router) OPTIONS added in v1.0.12

func (w *Router) OPTIONS(path string, meta Meta, handler ...gin.HandlerFunc) RouterInfo

func (*Router) PATCH added in v1.0.12

func (w *Router) PATCH(path string, meta Meta, handler ...gin.HandlerFunc) RouterInfo

func (*Router) POST added in v1.0.12

func (w *Router) POST(path string, meta Meta, handler ...gin.HandlerFunc) RouterInfo

func (*Router) PUT added in v1.0.12

func (w *Router) PUT(path string, meta Meta, handler ...gin.HandlerFunc) RouterInfo

func (*Router) Use added in v1.0.13

func (w *Router) Use(handler ...gin.HandlerFunc)

func (*Router) Walk added in v1.0.12

func (w *Router) Walk(walker Walker) error

Walk iterate each Group with fn you should call this func on root router param walker Walker return error

type RouterGroup added in v1.0.12

type RouterGroup interface {
	Use(handler ...gin.HandlerFunc)
	Group(path string, meta Meta, handler ...gin.HandlerFunc) RouterGroup
	Handle(method, path string, meta Meta, handler ...gin.HandlerFunc) RouterInfo
	GET(path string, meta Meta, handler ...gin.HandlerFunc) RouterInfo
	POST(path string, meta Meta, handler ...gin.HandlerFunc) RouterInfo
	DELETE(path string, meta Meta, handler ...gin.HandlerFunc) RouterInfo
	PUT(path string, meta Meta, handler ...gin.HandlerFunc) RouterInfo
	HEAD(path string, meta Meta, handler ...gin.HandlerFunc) RouterInfo
	OPTIONS(path string, meta Meta, handler ...gin.HandlerFunc) RouterInfo
	PATCH(path string, meta Meta, handler ...gin.HandlerFunc) RouterInfo
	Any(path string, meta Meta, handler ...gin.HandlerFunc) []RouterInfo
	Match(method []string, path string, meta Meta, handler ...gin.HandlerFunc) []RouterInfo
}

type RouterInfo added in v1.0.12

type RouterInfo struct {
	IsGroup bool
	Group   *gin.RouterGroup

	Method   string
	FullPath string
	Meta     Meta

	Chain gin.HandlersChain
}

RouterInfo describe a registered info if IsGroup is true, the root represent itself

type Routes added in v1.0.12

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

Routes which store all registered route info

func (*Routes) AddRoute added in v1.0.12

func (r *Routes) AddRoute(info RouterInfo)

func (*Routes) Walk added in v1.0.12

func (r *Routes) Walk(w Walker) error

type ValidateComp added in v1.0.4

type ValidateComp func(core *Core) binding.StructValidator

type Validator added in v1.0.6

type Validator interface {
	Validate() error
}

type Walker added in v1.0.12

type Walker func(info RouterInfo) error

Jump to

Keyboard shortcuts

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