ws

package
v0.0.1-202202252100 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2022 License: MPL-2.0 Imports: 16 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 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 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, rawData 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 HttpServer

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

func NewHttpServer

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

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) Run

func (srv *HttpServer) Run()

Run http server

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
}

Jump to

Keyboard shortcuts

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