goi

package module
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2024 License: BSD-3-Clause Imports: 21 Imported by: 0

README

hgee

基于 net/http 进行开发的 Web 框架

功能:

  1. http web 服务
  2. 多级路由:子父路由,内置路由转换器,自定义路由转换器
  3. 静态路由:静态路由映射,返回文件对象即响应该文件内容
  4. 中间件:请求中间件,视图中间件,响应中间件
  5. 日志模块:支持三种日志,日志等级,按照日志大小、日期自动进行日志切割
  6. 内置 JWT token
  7. ORM 模型关系映射,Mysql、Sqlite3
  8. 内置缓存

详细示例:example

Documentation

Index

Constants

View Source
const (
	LFUInitValue    = 5   // LFU初始化值
	LFULogFactor    = 0.1 // LFU对数增长因子
	MaxCounterValue = 255 // 计数器最大值
)
View Source
const (
	DEBUG   logLevel = iota // 打印所有日志
	INFO                    // 打印所有日志
	WARNING                 // 打印 Warning、Error 日志
	ERROR                   // 打印 Error 日志
)

日志等级

Variables

View Source
var Settings *metaSettings

Functions

func LFULogIncr

func LFULogIncr(counter uint8) uint8

计算 LFU 递增值

func RegisterConverter

func RegisterConverter(typeName string, converter string)

func Version

func Version() string

版本

Types

type AsView

type AsView struct {
	GET     HandlerFunc
	HEAD    HandlerFunc
	POST    HandlerFunc
	PUT     HandlerFunc
	PATCH   HandlerFunc
	DELETE  HandlerFunc
	CONNECT HandlerFunc
	OPTIONS HandlerFunc
	TRACE   HandlerFunc
}

type CacheItems

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

type Data

type Data struct {
	Status int    `json:"status"`
	Msg    string `json:"msg"`
	Data   any    `json:"data"`
}

内置响应数据格式

type Engine

type Engine struct {
	Router      *metaRouter
	MiddleWares *metaMiddleWares
	Settings    *metaSettings
	Cache       *MetaCache
	Log         *MetaLogger
}

Engine 实现 ServeHTTP 接口

func NewHttpServer

func NewHttpServer() *Engine

创建一个 Http 服务

func (*Engine) HandlerHTTP

func (engine *Engine) HandlerHTTP(request *Request, response http.ResponseWriter) (result any)

处理 HTTP 请求

func (*Engine) RunServer

func (engine *Engine) RunServer() (err error)

启动 http 服务

func (*Engine) ServeHTTP

func (engine *Engine) ServeHTTP(response http.ResponseWriter, request *http.Request)

实现 ServeHTTP 接口

type EvictPolicy

type EvictPolicy uint8 // 缓存淘汰策略
const (
	NOEVICTION      EvictPolicy = iota // 直接返回错误,不淘汰任何已经存在的键
	ALLKEYS_RANDOM                     // 所有的键 随机移除 key
	ALLKEYS_LRU                        // 所有的键 移除最近最少使用的 key
	ALLKEYS_LFU                        // 所有的键 移除最近最不频繁使用的 key
	VOLATILE_RANDOM                    // 所有设置了过期时间的键 随机移除 key
	VOLATILE_LRU                       // 所有设置了过期时间的键 移除最近最少使用的 key
	VOLATILE_LFU                       // 所有设置了过期时间的键 移除最近最不频繁使用的 key
	VOLATILE_TTL                       // 所有设置了过期时间的键 移除快过期的 key
)

type ExpirationPolicy

type ExpirationPolicy uint8 // 过期策略
const (
	// 定期删除:默认每隔 1s 就随机抽取一些设置了过期时间的 key,检查其是否过期,如果有过期就删除。
	// 定时删除:某个设置了过期时间的 key,到期后立即删除。
	PERIODIC  ExpirationPolicy = iota // 定期删除
	SCHEDULED                         // 定时删除

)

type Float

type Float interface {
	float32 | float64
}

type HandlerFunc

type HandlerFunc func(*Request) any

HandlerFunc 定义 goi 使用的请求处理程序

type Int

type Int interface {
	int | int8 | int16 | int32 | int64
}

type IntAll

type IntAll interface {
	int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64
}

type MetaCache

type MetaCache struct {
	EVICT_POLICY      EvictPolicy      // 缓存淘汰策略
	EXPIRATION_POLICY ExpirationPolicy // 过期策略
	MAX_SIZE          int64
	// contains filtered or unexported fields
}
var Cache *MetaCache

func (*MetaCache) Del

func (cache *MetaCache) Del(key string)

Del 删除键值

func (*MetaCache) DelExp

func (cache *MetaCache) DelExp(key string) bool

DelExp 删除过期的 key, 如果 key 已过期则删除返回 true,未过期则不会删除返回 false

func (*MetaCache) Get

func (cache *MetaCache) Get(key string) (any, bool)

Get 查找键的值

func (*MetaCache) Set

func (cache *MetaCache) Set(key string, value any, expires int)

Set 设置键值

type MetaDataBase

type MetaDataBase struct {
	ENGINE       string
	NAME         string
	USER         string
	PASSWORD     string
	HOST         string
	PORT         uint16
	SERVICE_NAME string
}

数据库

type MetaLogger

type MetaLogger struct {
	DEBUG           bool   // 默认为 true
	INFO_OUT_PATH   string // 所有日志输出路径
	ACCESS_OUT_PATH string // 访问日志输出路径
	ERROR_OUT_PATH  string // 错误日志输出路径
	OUT_DATABASE    string // 所有日志输出到数据库
	SplitSize       int64  // 日志切割大小,默认为 1G 1024 * 1024 * 1024
	SplitTime       string // 日志切割大小,默认按天切割
	// contains filtered or unexported fields
}
var Log *MetaLogger

func NewLogger

func NewLogger() *MetaLogger

NewLogger 创建一个logger

func (*MetaLogger) Debug

func (logger *MetaLogger) Debug(log ...any)

日志方法 Debug 通用 Debug 日志

func (*MetaLogger) Error

func (logger *MetaLogger) Error(log ...any)

ERROR 通用 ERROR 日志

func (*MetaLogger) Info

func (logger *MetaLogger) Info(log ...any)

Info 通用 Info 日志

func (*MetaLogger) InitLogger

func (logger *MetaLogger) InitLogger(initInfo string)

日志初始化

func (*MetaLogger) Log

func (logger *MetaLogger) Log(level logLevel, log ...any)

Log 打印日志

func (*MetaLogger) Warning

func (logger *MetaLogger) Warning(log ...any)

WARNING 通用 WARNING 日志

type Request

type Request struct {
	Object      *http.Request
	Context     context.Context // 请求上下文
	PathParams  metaValues      // 路由参数
	QueryParams metaValues      // 查询字符串参数
	BodyParams  metaValues      // Body 传参
}

type RequestMiddleware

type RequestMiddleware func(*Request) any // 请求中间件

type Response

type Response struct {
	Status int
	Data   any
}

请求响应数据

type ResponseMiddleware

type ResponseMiddleware func(*Request, http.ResponseWriter) any // 响应中间件

type UInt

type UInt interface {
	uint | uint8 | uint16 | uint32 | uint64
}

type ViewMiddleware

type ViewMiddleware func(*Request, HandlerFunc) any // 视图中间件

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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