goweber

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2025 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()
    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
# apper中有Cache结构指针
# apper has Cache structure pointer
cache = 1

# 限流 ipmax監控最大IP數量>0,開啓限流,ratelimit每秒訪問次數
# Rate limiting ipmax: maximum number of monitored IPs > 0, enable rate limiting, ratelimit: visits per second
ipmax=1000
ratelimit=10

# apper中有Jwt结构指针
# apper has Jwt structure pointer
[jwt]
rint = 1
rstr = WHSS
version = V1
exp = 8

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Apper

type Apper struct {

	// 緩存器
	// Cache
	Cache *Cacher

	// jwt处理
	// JWT processing
	Jwt *Jwter
	// 文件上传
	// File upload
	File *FileUploader
	// 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) 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) SetCache

func (this *Apper) SetCache()

SetCache 缓存设置 SetCache cache settings

func (*Apper) SetConfig

func (this *Apper) SetConfig()

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

func (*Apper) SetFile added in v0.1.4

func (this *Apper) SetFile()

SetFile 设置上传文件 SetFile set file upload

func (*Apper) SetJwt added in v0.1.4

func (this *Apper) SetJwt()

jwt 设置 JWT settings

func (*Apper) SetLimit

func (this *Apper) SetLimit()

SetLimit 設置限流 SetLimit set rate limiting

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

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

全局中間件處理 Global middleware processing

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)

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 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 Token

type Token struct {
	Id   string
	Exp  string
	Code string
}

Jump to

Keyboard shortcuts

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