goweber

package module
v1.1.5 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: MIT Imports: 19 Imported by: 0

README

goweber

介绍

goweber是一個GO編寫的WEB框架,主要用於API服務。 支持功能:

  • 路由
  • 日志服務
  • 配置服务
  • 基於IP的限流
  • 中間件服務,支持全局和路由級
  • 自定義JWT
  • 文件上傳
  • 查詢緩存
  • 暴力破解防護
  • 跨域支持
  • 靜態目錄
數據結構

数据结构

安装教程
go get github.com/sonkwl/goweber
使用说明
package goweber

import (
    "fmt"
    "net/http"
    "github.com/sonkwl/goweber"
)
func main() { 
    app := goweber.New()
    defer app.Close() // 關閉file和chan
    app.Get("/", func(w http.ResponseWriter, r *http.Request) { 
        fmt.Fprintf(w, "Hello World!")
    })
    app.Run()
}

配置文件

請保證config.ini與執行文件同目錄下

[server]
# 網站端口
# Website port
port = 8080

#  網絡日誌 logfile:目錄,logmax:文件最大大小
# Network log logfile: directory, logmax: maximum file size
logfile = access.log
logmax = 1024000000

# 緩存器,單位Mb
# Cache, unit Mb
cache = 1

# v1.0.3以前的版本使用
# 限流 ipmax監控最大IP數量>0,開啓限流,ratelimit每秒訪問次數
# Rate limiting ipmax: maximum number of monitored IPs > 0, enable rate limiting, ratelimit: visits per second
ipmax=1000
ratelimit=20

# v1.1.4引入靜態默認路徑
static=static

# v1.1.0以上版本功能
[rate]
# 是否開啓限流
enable=1
# 監控秒
second=1
# 404最大錯誤量
errmax=5
# 監控最大IP數量
ipmax=10000
# 封禁時間,單位分鐘
blockminute=5

# 自定義JWT
# self-defined JWT
[jwt]
rint = 1
rstr = WHSS
version = V1
exp = 8

# 文件上傳
# File upload
[file]
# 单位MB
# Unit MB
size = 20
# 最大批量上传数量
# Maximum batch upload quantity
max = 2
# 文件保存目录
# File save directory
path = ./files
# 文件类型
# File types
type = .jpg,.png

Documentation

Overview

* 暴力破解監控 * 舉例:同一個對/login,在一段時間内報錯多次,判定暴力破解,禁用IP 24小時 * 設定:1分鐘內,10次錯誤,則禁用IP 24小時

限流器 基於IP的限流器,如果1秒内出現100請求都是404,則封禁IP 5分鐘

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Apper

type Apper struct {

	// 配置信息结构体指针
	// Configuration information struct pointer
	Config *Configer
	// contains filtered or unexported fields
}

Apper 是应用程序的主结构体,包含路由映射、配置信息、端口、日志等信息 Apper is the main struct of the application, containing route mappings, configuration information, port, logs, etc.

func New

func New() *Apper

New 创建并初始化一个新的Apper实例 New creates and initializes a new Apper instance

func (*Apper) Close

func (this *Apper) Close()

Close 关闭应用程序资源,包括日志文件和消息通道 Close closes application resources, including log file and message channel

func (*Apper) Get

func (this *Apper) Get(path string, f http.HandlerFunc, mid ...MiddlewareFunc)

Get 注册GET请求的路由处理函数 Get registers the route handler function for GET requests

func (*Apper) GetClientIP

func (this *Apper) GetClientIP(r *http.Request) string

GetClientIP 获取客户端真实IP地址 GetClientIP get client real IP address

func (*Apper) Logger

func (this *Apper) Logger()

Logger 处理日志记录,监听消息通道并将日志写入文件或控制台 Logger handles log recording, listens to the message channel and writes logs to file or console

func (*Apper) Post

func (this *Apper) Post(path string, f http.HandlerFunc, mid ...MiddlewareFunc)

Post 注册POST请求的路由处理函数 Post registers the route handler function for POST requests

func (*Apper) Print added in v1.1.0

func (this *Apper) Print(key string)

打印私有參數

func (*Apper) Route

func (this *Apper) Route(method string, path string, f http.HandlerFunc, mid ...MiddlewareFunc)

Route 注册指定HTTP方法的路由处理函数 Route registers the route handler function for the specified HTTP method

func (*Apper) Run

func (this *Apper) Run()

Run 启动HTTP服务器 Run starts the HTTP server

func (*Apper) RunTLS

func (this *Apper) RunTLS(certFile, keyFile string)

RunTLS 启动HTTPS服务器 RunTLS starts the HTTPS server

func (*Apper) ServeHTTP

func (this *Apper) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP 实现http.Handler接口,处理HTTP请求 ServeHTTP implements the http.Handler interface to handle HTTP requests

func (*Apper) SetConfig

func (this *Apper) SetConfig()

SetConfig 从config.ini文件中读取配置信息 SetConfig reads configuration information from config.ini file

func (*Apper) SetLog

func (this *Apper) SetLog()

SetLog 根据配置设置日志记录器 SetLog sets up the logger according to configuration

func (*Apper) SetPort

func (this *Apper) SetPort()

SetPort 从配置中设置服务器端口,默认为8080 SetPort sets the server port from configuration, default is 8080

func (*Apper) SetRate added in v1.1.0

func (this *Apper) SetRate()

SetLimit 設置限流 SetLimit set rate limiting

func (*Apper) SetStatic added in v1.1.5

func (this *Apper) SetStatic()

SetStatic 设置静态文件目录 SetStatic sets the static file directory

func (*Apper) Use

func (this *Apper) Use(middleware ...MiddlewareFunc)

全局中間件處理 Global middleware processing

type Bruter added in v1.1.2

type Bruter struct {
	// 1分鐘內,10次錯誤,則禁用IP 24小時
	MaxIp    int // 監控IP數量
	MaxErr   int // 監控錯誤數量
	MaxTime  int // 監控時間秒
	ErrIps   map[string]*BruterIP
	BlockIps map[string]time.Time
	sync.RWMutex
}

func NewBruter added in v1.1.2

func NewBruter() *Bruter

func (*Bruter) Clear added in v1.1.2

func (this *Bruter) Clear()

* 清除過期IP

func (*Bruter) GetClientIP added in v1.1.2

func (this *Bruter) GetClientIP(r *http.Request) string

獲得IP

func (*Bruter) IsBlocked added in v1.1.2

func (this *Bruter) IsBlocked(ip string) bool

* 檢查IP是否被禁用

func (*Bruter) SetStatus added in v1.1.2

func (this *Bruter) SetStatus(ip string)

* 改變IP狀態

type BruterIP added in v1.1.2

type BruterIP struct {
	IP       string
	Err      int
	LastTime time.Time
}

type Cacher

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

func NewCacher

func NewCacher(m int64) *Cacher

* 新建緩存,m為空間大小,單位 m

func (*Cacher) IsCache

func (this *Cacher) IsCache(w http.ResponseWriter, r *http.Request) bool

* 判斷緩存是否存在,存在則返回緩存數據

func (*Cacher) SetCache

func (this *Cacher) SetCache(r *http.Request, m int64, data string)

* 根據url定義緩存時間和緩存數據,m為分鐘

func (*Cacher) Size

func (this *Cacher) Size() int64

* 獲取占用空間大小

type Configer

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

func (*Configer) Get

func (this *Configer) Get(title string, key string) string

func (*Configer) ReadFile

func (this *Configer) ReadFile() error

func (*Configer) SetFile

func (this *Configer) SetFile(f *os.File) error

type FileUploader

type FileUploader struct {
	MaxSize      int64
	AllowedTypes []string
	SavePath     string
	MaxFiles     int    //一次最大上传文件数量
	FieldName    string // 添加单文件字段名
	FieldNames   string // 添加多文件字段名
	Keyword      string // 添加文件名关键字
}

func NewFileUploader

func NewFileUploader(maxSize int64, allowedTypes []string, savePath string) *FileUploader

func (*FileUploader) HandleUpload

func (f *FileUploader) HandleUpload(r *http.Request) ([]string, error)

HandleUpload 修改为支持批量文件上传

type IpData added in v1.1.0

type IpData struct {
	Count    int
	LastTime time.Time
}

監控IP數據結構

type Jwter

type Jwter struct {
	Rint    int
	Rstr    string
	Exphour int
	Version string
	Key     string
}

func NewJwter

func NewJwter() *Jwter

func (*Jwter) Decode

func (this *Jwter) Decode(token string) (string, error)

func (*Jwter) Encode

func (this *Jwter) Encode() (string, error)

func (*Jwter) GetArr

func (this *Jwter) GetArr(tokenstr string) ([]string, error)

func (*Jwter) GetJoin

func (this *Jwter) GetJoin(key string, exp string, code string) (string, error)

func (*Jwter) OneToTwo

func (this *Jwter) OneToTwo(val string) string

func (*Jwter) Validate

func (this *Jwter) Validate(token string) error

type MiddlewareFunc

type MiddlewareFunc func(r *http.Request) error

中間件類型 Middleware type

type Rater added in v1.1.0

type Rater struct {
	sync.RWMutex                      // 添加读写锁
	Start        int                  // 是否開啓,0為關閉,1為開啓
	Second       int                  // 監控秒
	BlockMinute  int                  // 封禁分鐘
	ErrMax       int                  // 最大請求錯誤次數
	IpMax        int                  // 最大監控IP數量
	ErrorIps     map[string]*IpData   // 監控IP
	BlockIps     map[string]time.Time // 封禁IP
}

限流器數據結構

func NewRater added in v1.1.0

func NewRater() *Rater

實例化

func (*Rater) ClearBlockIps added in v1.1.0

func (this *Rater) ClearBlockIps()

* 鎖定列表清理

func (*Rater) ClearErrorIps added in v1.1.0

func (this *Rater) ClearErrorIps()

* 監控列表清理

func (*Rater) IsBlocked added in v1.1.0

func (this *Rater) IsBlocked(ip string) bool

判斷是否鎖定中

func (*Rater) SetStatus added in v1.1.0

func (this *Rater) SetStatus(ip string)

判斷IP監控狀態

type Token

type Token struct {
	Id   string
	Exp  string
	Code string
}

Directories

Path Synopsis
*

Jump to

Keyboard shortcuts

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