ws

package
v1.0.37 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2023 License: MPL-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultPageSize = 10

Variables

View Source
var (
	ErrTokenNotFound                  = errors.New("token not found")
	ErrInvalidHttpAuthorizationHeader = errors.New("invalid http authorization header")
)
View Source
var (
	ErrDuplicateRouterGroup = errors.New("duplicate router group")
	ErrRouterGroupNotFound  = errors.New("router group not found")
)
View Source
var (
	HttpContentTypes = map[MimeType]string{
		MimeTypeBinary: "application/octet-stream",
		MimeTypePDF:    "application/pdf",
		MimeTypeMSWord: "application/msword",
		MimeTypeJPEG:   "image/jpeg",
	}
)
View Source
var SkipHttpMethods = []string{
	"PRI",
	"HEAD",
}

Functions

func AddMetaKvs

func AddMetaKvs(c *gin.Context, key, value string)

AddMetaKvs 添加信息到meta中去

func Download

func Download(c *gin.Context, mimeType MimeType, filename string, content []byte) error

Download a mime file

func Error

func Error(c *gin.Context, code int, msg string)

func Failure

func Failure(c *gin.Context, err error)

Failure grpc http错误

func Forbidden

func Forbidden(c *gin.Context, err error)

func GetAuthorizationToken

func GetAuthorizationToken(c *gin.Context) (string, error)

GetAuthorizationToken get bearer token or other token from http header

func GetMetaKvs

func GetMetaKvs(c *gin.Context) []string

GetMetaKvs 获取meta的[key, value] slice

func GetPagination

func GetPagination(c *gin.Context, args ...int64) *pagination.Pagination

GetPagination get *Pagination args[0] is default page size then you can use pagination.Paging or pagination.GetSQLClause(total) 去获取分页的limit子句

func GetRealIP

func GetRealIP(c *gin.Context) string

GetRealIP 获取真实IP

func InvalidRequest

func InvalidRequest(c *gin.Context, err error)

func IsPrivateIp

func IsPrivateIp(ipStr string) bool

IsPrivateIp 检查是否时内网ip

func NewRouter

func NewRouter(logger types.LogProvider) *gin.Engine

NewRouter create a gin router

func PermanentRedirect

func PermanentRedirect(c *gin.Context, location string)

func Redirect

func Redirect(c *gin.Context, location string)

func SetDebugMode

func SetDebugMode()

SetDebugMode set gin to debug mode

func SetReleaseMode

func SetReleaseMode()

SetReleaseMode set gin to release mode

func SetTestMode

func SetTestMode()

SetTestMode set gin to test mode

func Success

func Success(c *gin.Context, args ...interface{})

Success respond with data empty args respond with 'ok' message args[0] is the response data

func SuccessPages

func SuccessPages(c *gin.Context, total int64, pages interface{})

SuccessPages respond with pagination data

func SuccessRaw

func SuccessRaw(c *gin.Context, result interface{})

SuccessRaw respond with raw data

func Unauthorized

func Unauthorized(c *gin.Context, err error)

Types

type HttpMethod

type HttpMethod int
const (
	Get HttpMethod = iota
	Post
	Delete
)

type HttpServer

type HttpServer struct {
	*http.Server
	// contains filtered or unexported fields
}

func (*HttpServer) AddGroupRoutes

func (srv *HttpServer) AddGroupRoutes(groupName string, routes []*Route) error

func (*HttpServer) AddRoutes

func (srv *HttpServer) AddRoutes(routes []*Route)

AddRoutes add routes from Route slice

func (*HttpServer) CreateRouterGroup

func (srv *HttpServer) CreateRouterGroup(name, relativePath string, handlers ...gin.HandlerFunc) error

CreateRouterGroup create a gin router group

func (*HttpServer) GetRouterGroup

func (srv *HttpServer) GetRouterGroup(groupName string) *gin.RouterGroup

func (*HttpServer) NewRouterGroup

func (srv *HttpServer) NewRouterGroup(name, relativePath string, handlers ...gin.HandlerFunc) (*gin.RouterGroup, error)

NewRouterGroup new a gin router group

func (*HttpServer) Run

func (srv *HttpServer) Run()

Run http server

type HttpsServer

type HttpsServer struct {
	*HttpServer
	CertPath string
	KeyPath  string
}

func (*HttpsServer) Run

func (srv *HttpsServer) Run()

type MimeType

type MimeType int
const (
	MimeTypeBinary MimeType
	MimeTypeJPEG
	MimeTypePDF
	MimeTypeMSWord
)

type PageResponse

type PageResponse struct {
	Response
	Total int64 `json:"total"`
}

type PaginationParam

type PaginationParam struct {
	// 指定页面
	Page int64 `form:"page" json:"page"`
	// 指定PageSize
	PageSize int64 `form:"page_size" json:"page_size"`
	// 如果前端传过来了上次页面中的最后的主键id,则要记录起来,用作翻页优化
	LastPk int64 `form:"last_pk" json:"last_pk"`
}

type Response

type Response struct {
	Code int         `json:"code"`
	Data interface{} `json:"data"`
	Msg  string      `json:"msg"`
}

type Route

type Route struct {
	Method  HttpMethod
	Path    string
	Handler gin.HandlerFunc
}

type WebServer

type WebServer interface {
	Run()
	NewRouterGroup(name, relativePath string, handlers ...gin.HandlerFunc) (*gin.RouterGroup, error)
	CreateRouterGroup(name, relativePath string, handlers ...gin.HandlerFunc) error
	GetRouterGroup(groupName string) *gin.RouterGroup
	AddRoutes(routes []*Route)
	AddGroupRoutes(groupName string, routes []*Route) error
}

func NewHttpServer

func NewHttpServer(logger types.LogProvider, address string) WebServer

func NewHttpsServer

func NewHttpsServer(logger types.LogProvider, certPath, keyPath, address string) (WebServer, error)

Jump to

Keyboard shortcuts

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