ws

package
v0.0.1-202203191236 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2022 License: MPL-2.0 Imports: 18 Imported by: 1

Documentation

Index

Constants

View Source
const (
	ErrCodeServerInternal // 内部错误
	ErrCodeUnauthorized   // 未授权
	ErrCodeInvalidRequest // 非法请求
	ErrCodeForbidden      // 拒绝访问

)
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 = err.New("duplicate router group")
	ErrRouterGroupNotFound  = err.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 Failure

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

func Forbidden

func Forbidden(c *gin.Context)

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)

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)

Types

type HttpMethod

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

type MimeType

type MimeType int
const (
	MimeTypeBinary MimeType
	MimeTypeJPEG
	MimeTypePDF
	MimeTypeMSWord
)

type MyHttpServer

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

func NewHttpServer

func NewHttpServer(logger types.LogProvider, address string) *MyHttpServer

func (*MyHttpServer) AddGroupRoutes

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

func (*MyHttpServer) AddRoutes

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

AddRoutes add routes from Route slice

func (*MyHttpServer) CreateRouterGroup

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

CreateRouterGroup create a gin router group

func (*MyHttpServer) GetRouterGroup

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

func (*MyHttpServer) Run

func (srv *MyHttpServer) Run()

Run http server

type MyHttpsServer

type MyHttpsServer struct {
	*MyHttpServer
	CertPath string
	KeyPath  string
}

func NewHttpsServer

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

func (*MyHttpsServer) Run

func (srv *MyHttpsServer) Run()

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
}

Jump to

Keyboard shortcuts

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