cherryGin

package module
v1.3.12 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: MIT Imports: 16 Imported by: 5

README

gin组件

  • 基于gin实现http server

Install

Prerequisites
  • GO >= 1.17
Using go get
go get github.com/cherry-game/cherry/components/gin@latest

Quick Start

import cherryGIN "github.com/cherry-game/cherry/components/gin"

example

Documentation

Overview

Package cherryGin from https://github.com/gin-contrib/zap/

Index

Constants

View Source
const (
	NamePrefix = "gin_component_"
)

Variables

This section is empty.

Functions

func BindHandler

func BindHandler(handler func(ctx *Context)) gin.HandlerFunc

func BindHandlers

func BindHandlers(handlers []GinHandlerFunc) []gin.HandlerFunc

func SetMode

func SetMode(value string)

Types

type BaseController

type BaseController struct {
	App    cfacade.IApplication
	Engine *gin.Engine
}

func (*BaseController) Any

func (b *BaseController) Any(relativePath string, handlers ...GinHandlerFunc)

func (*BaseController) GET

func (b *BaseController) GET(relativePath string, handlers ...GinHandlerFunc)

func (*BaseController) Group

func (b *BaseController) Group(relativePath string, handlers ...GinHandlerFunc) *Group

func (*BaseController) Init

func (b *BaseController) Init()

func (*BaseController) POST

func (b *BaseController) POST(relativePath string, handlers ...GinHandlerFunc)

func (*BaseController) PreInit

func (b *BaseController) PreInit(app cfacade.IApplication, engine *gin.Engine)

func (*BaseController) Stop

func (b *BaseController) Stop()

type Component

type Component struct {
	cfacade.Component
	*HttpServer
	// contains filtered or unexported fields
}

Component wrapper gin

func New

func New(name string, address string, opts ...OptionFunc) *Component

func NewHttp

func NewHttp(name, address string) *Component

func NewHttps

func NewHttps(name, address, certFile, keyFile string) *Component

func (*Component) Engine

func (g *Component) Engine() *gin.Engine

func (*Component) Init

func (g *Component) Init()

func (*Component) Name

func (g *Component) Name() string

Name unique components name

func (*Component) OnAfterInit

func (g *Component) OnAfterInit()

func (*Component) OnBeforeStop

func (g *Component) OnBeforeStop()

func (*Component) OnStop

func (g *Component) OnStop()

func (*Component) Register

func (g *Component) Register(controllers ...IController) *Component

type Context

type Context struct {
	*gin.Context
}

func (*Context) GetBody

func (g *Context) GetBody() string

func (*Context) GetBool

func (g *Context) GetBool(name string, defaultValue bool, checkPost ...bool) bool

func (*Context) GetInt

func (g *Context) GetInt(name string, defaultValue int, checkPost ...bool) int

func (*Context) GetInt32

func (g *Context) GetInt32(name string, defaultValue int32, checkPost ...bool) int32

func (*Context) GetInt32Cookie added in v1.2.4

func (g *Context) GetInt32Cookie(name string, defaultValue int32) int32

func (*Context) GetInt64

func (g *Context) GetInt64(name string, defaultValue int64, checkPost ...bool) int64

func (*Context) GetInt64Cookie added in v1.2.4

func (g *Context) GetInt64Cookie(name string, defaultValue int64) int64

func (*Context) GetIntCookie added in v1.2.4

func (g *Context) GetIntCookie(name string, defaultValue int) int

func (*Context) GetParams

func (g *Context) GetParams(checkPost ...bool) map[string]string

func (*Context) GetString

func (g *Context) GetString(name, defaultValue string, checkPost ...bool) string

func (*Context) GetStringCookie added in v1.2.4

func (g *Context) GetStringCookie(name string, defaultValue string) string

func (*Context) HTML200

func (g *Context) HTML200(name string, obj ...interface{})

func (*Context) IsGet

func (g *Context) IsGet() bool

func (*Context) IsPost

func (g *Context) IsPost() bool

func (*Context) JSON200

func (g *Context) JSON200(obj interface{})

func (*Context) PostFormInt32Array

func (g *Context) PostFormInt32Array(name string) []int32

func (*Context) PostFormInt64Array

func (g *Context) PostFormInt64Array(name string) []int64

func (*Context) PostFormIntArray

func (g *Context) PostFormIntArray(name string) []int

func (*Context) PostInt

func (g *Context) PostInt(name string, defaultValue int) int

func (*Context) PostInt32 added in v1.2.4

func (g *Context) PostInt32(name string, defaultValue int32) int32

func (*Context) PostInt64

func (g *Context) PostInt64(name string, defaultValue int64) int64

func (*Context) PostString

func (g *Context) PostString(name string, defaultValue string) string

func (*Context) RenderHTML

func (g *Context) RenderHTML(html string)

func (*Context) RenderJSON

func (g *Context) RenderJSON(value interface{})

func (*Context) RenderJsonString

func (g *Context) RenderJsonString(json string)

type GinHandlerFunc

type GinHandlerFunc func(ctx *Context)

func Cors

func Cors(domain ...string) GinHandlerFunc

func GinDefaultZap

func GinDefaultZap() GinHandlerFunc

func GinZap

func GinZap(timeFormat string, utc bool) GinHandlerFunc

GinZap returns a gin.HandlerFunc (middleware) that logs requests using uber-go/zap.

Requests with errors are logged using zap.Error(). Requests without errors are logged using zap.Info().

It receives:

  1. A time package format string (e.g. time.RFC3339).
  2. A boolean stating whether to use UTC time zone or local.

func MaxConnect

func MaxConnect(n int) GinHandlerFunc

MaxConnect limit max connect

func RecoveryWithZap

func RecoveryWithZap(stack bool) GinHandlerFunc

RecoveryWithZap returns a gin.HandlerFunc (middleware) that recovers from any panics and logs requests using uber-go/zap. All errors are logged using zap.Error(). stack means whether output the stack info. The stack info is easy to find where the error occurs but the stack info is too large.

type Group

type Group struct {
	*gin.RouterGroup
}

func (*Group) Any

func (p *Group) Any(relativePath string, handlers ...GinHandlerFunc)

func (*Group) GET

func (p *Group) GET(relativePath string, handlers ...GinHandlerFunc)

func (*Group) POST

func (p *Group) POST(relativePath string, handlers ...GinHandlerFunc)

type HttpServer

type HttpServer struct {
	cfacade.IApplication
	Options
	*gin.Engine
	// contains filtered or unexported fields
}

func NewHttpServer

func NewHttpServer(address string, opts ...OptionFunc) *HttpServer

func (*HttpServer) Register

func (p *HttpServer) Register(controllers ...IController) *HttpServer

func (*HttpServer) Run

func (p *HttpServer) Run()

func (*HttpServer) SetIApplication

func (p *HttpServer) SetIApplication(app cfacade.IApplication)

func (*HttpServer) Static

func (p *HttpServer) Static(relativePath string, staticDir string)

func (*HttpServer) StaticFile

func (p *HttpServer) StaticFile(relativePath, filepath string)

func (*HttpServer) Stop

func (p *HttpServer) Stop()

func (*HttpServer) Use

func (p *HttpServer) Use(middleware ...GinHandlerFunc)

type IController

type IController interface {
	PreInit(app cfacade.IApplication, engine *gin.Engine)
	Init()
	Stop()
}

type OptionFunc

type OptionFunc func(opts *Options)

func WithCert

func WithCert(certFile, keyFile string) OptionFunc

func WithIdleTimeout

func WithIdleTimeout(t time.Duration) OptionFunc

func WithMaxHeaderBytes

func WithMaxHeaderBytes(val int) OptionFunc

func WithReadHeaderTimeout

func WithReadHeaderTimeout(t time.Duration) OptionFunc

func WithReadTimeout

func WithReadTimeout(t time.Duration) OptionFunc

type Options

type Options struct {
	ReadTimeout       time.Duration
	ReadHeaderTimeout time.Duration
	WriteTimeout      time.Duration
	IdleTimeout       time.Duration
	MaxHeaderBytes    int
	CertFile          string
	KeyFile           string
}

Options http server parameter

Jump to

Keyboard shortcuts

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