libgc

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2024 License: GPL-3.0 Imports: 32 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

LibGC

一个中间件组件库。

LICENSE

GNU Generic Public License V3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func APIBuilder

func APIBuilder(handlers ...func(*gin.RouterGroup) *gin.RouterGroup) func(gin.IRouter, string) *gin.RouterGroup

为Gin router 添加CRUD

func AddCRUD

func AddCRUD[T any](router gin.IRouter, path string, db *gorm.DB) *gin.RouterGroup

func AddCRUDNew

func AddCRUDNew[T any](router gin.IRouter, path string, db *gorm.DB, processGet func(*gorm.DB, *gin.Context) *gorm.DB, processGetAll func(*gorm.DB, *gin.Context) *gorm.DB, processCreate func(*gorm.DB, *T) *gorm.DB) *gin.RouterGroup

func AddCRUDWithAuth

func AddCRUDWithAuth[T any](router gin.IRouter, path string, db *gorm.DB, permLo, permHi int) *gin.RouterGroup

func AddCaptchaAPI

func AddCaptchaAPI(router gin.IRouter, path string, conf1 MailConfig, conf2 CaptchaConfig, rdb *redis.Client) *gin.RouterGroup

func AddLoginAPI

func AddLoginAPI(router gin.IRouter, path string, db *gorm.DB) *gin.RouterGroup

func AddStatic

func AddStatic(router *gin.Engine, staticFileDir []string)

func AddStaticFS

func AddStaticFS(router *gin.Engine, fs embed.FS)

func AuthPermission

func AuthPermission(permLo, permHi int) func(c *gin.Context, token UserClaim) error

验证权限 权限位于[permLo, permHi]之间则为合理

func CheckPasswordHash

func CheckPasswordHash(password, hash string) error

func ConstructQuery

func ConstructQuery[T any](db *gorm.DB, searchStruct T) *gorm.DB

Recursive function to construct the query based on the fields provided in the struct

func CorsMiddleware

func CorsMiddleware() gin.HandlerFunc

跨域中间件

func Create

func Create[T any](db *gorm.DB, process func(*gorm.DB, *T) *gorm.DB) func(c *gin.Context)

handlers for gorm

func Delete

func Delete[T any](db *gorm.DB) func(c *gin.Context)

func GenerateCaptcha

func GenerateCaptcha(length int, charset string) (string, string)

生成验证码 返回验证码id,验证码

func GenerateShortLink(url string) string

func GenerateToken

func GenerateToken(userClaim *UserClaim) (string, error)

生成token

func GenerateUUID

func GenerateUUID() string

生成uuid

func Get

func Get[T any](db *gorm.DB, process func(*gorm.DB, *gin.Context) *gorm.DB) func(c *gin.Context)

func GetAll

func GetAll[T any](db *gorm.DB, process func(*gorm.DB, *gin.Context) *gorm.DB) func(c *gin.Context)

func GetLocalIP

func GetLocalIP() string

func HandleCaptchaVerify

func HandleCaptchaVerify(rdb *redis.Client) func(*gin.Context)

func HandleLogin

func HandleLogin(db *gorm.DB) func(*gin.Context)

login service

func HandleMailSendCaptcha

func HandleMailSendCaptcha(mailConfig MailConfig, captchaConfig CaptchaConfig, rdb *redis.Client) func(*gin.Context)

验证码服务,使用redis存储

func HandleRegister

func HandleRegister(db *gorm.DB) func(*gin.Context)

func HashedPassword

func HashedPassword(password string) string

func JWTMiddleware

func JWTMiddleware(authToken func(*gin.Context, UserClaim) error) gin.HandlerFunc

JWT中间件 支持添加权限校验(返回error表示校验失败),以及上下文操作

func LoadCSV

func LoadCSV(filePath string) (map[string]map[string]string, error)

LoadCSV loads data from a CSV file and returns a slice of maps representing the rows.

func LoadConfig

func LoadConfig[Config any]() *Config

配置管理

func LoggerMiddleware

func LoggerMiddleware(logFile string) gin.HandlerFunc

Gin中间件

func NewDB

func NewDB(config *DatabaseConfig, migrator func(*gorm.DB) error, gormConfig *gorm.Config) *gorm.DB
 连接数据库
 config: 数据库配置
 migrator: 数据库迁移函数, 为nil则不迁移
 gormConfig: gorm配置
 迁移函数示例:

	func Migrate(db *gorm.DB) error {
		return db.AutoMigrate(&User{})
	}

func NewLoger

func NewLoger(logFile string) *log.Logger

new logger, default to append mode

func NewRedis

func NewRedis(config *RedisConfig) *redis.Client

func PaginatedResults

func PaginatedResults(c *gin.Context) func(*gorm.DB) *gorm.DB

Paginate the results

func RandPort

func RandPort() int

func RefreshToken

func RefreshToken(db *gorm.DB) func(*gin.Context)

func RunmDnsBroadcast

func RunmDnsBroadcast(serviceName, serviceDomain, instanceName string, text []string, servicePort int)

mDNS广播

func SaveConfig added in v0.1.1

func SaveConfig[Config any](config *Config)

TODO: SaveConfig[Config any]

func Update

func Update[T any](db *gorm.DB) func(c *gin.Context)

func VerifyCaptcha

func VerifyCaptcha(id string, captcha string, db *redis.Client) bool

验证验证码

Types

type CaptchaConfig

type CaptchaConfig struct {
	CaptchaLength int `json:"captcha_length"`
	CaptchaAlive  int `json:"captcha_alive"`
}

type Client

type Client struct {
	ID     string
	IP     string
	Port   int
	Status string
	Peers  map[string]*Client
	// contains filtered or unexported fields
}

func NewClient

func NewClient(ip string, port int) *Client

func (*Client) Listen

func (c *Client) Listen(path string, handler func(ctx *gin.Context, src *Client))

func (*Client) Send

func (c *Client) Send(path string, content gin.H) (gin.H, error)

send message to client, path should start with '/'

func (*Client) StartDiscover

func (c *Client) StartDiscover()

type DatabaseConfig

type DatabaseConfig struct {
	Type     string `json:"type"` // 数据库类型
	Host     string `json:"host"`
	Port     string `json:"port"`
	User     string `json:"user"`
	Password string `json:"password"`
	DB       string `json:"db"` // 数据库名
	Migrate  bool   `json:"migrate"`
}

type MailConfig

type MailConfig struct {
	MailUserName   string `json:"mail_username"`
	MailServer     string `json:"mail_server"`
	MailServerPort string `json:"mail_server_port"`
	MailPassword   string `json:"mail_password"`
}

type Pagination

type Pagination struct {
	PageSize int
	PageNum  int
}

分页数据

type RedisConfig

type RedisConfig struct {
	Addr     string `json:"addr"`
	Password string `json:"password"`
}

type ServerConfig

type ServerConfig struct {
	Port string `json:"port"`
}

通用配置

type User

type User struct {
	ID         uint   `gorm:"primary_key" json:"id"`
	Username   string `json:"username"`
	Email      string `json:"email"`
	Password   string `json:"password"`
	Permission int    `json:"permission"`
}

type UserClaim

type UserClaim struct {
	ID         int       `json:"id"`
	Permission int       `json:"permission"`
	Name       string    `json:"name"`
	Expire     time.Time `json:"expire"` // token过期时间
}

func ParseToken

func ParseToken(token string) (*UserClaim, error)

解析token

func (*UserClaim) Valid

func (uc *UserClaim) Valid() error

验证token

Jump to

Keyboard shortcuts

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