api

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2025 License: MIT Imports: 28 Imported by: 1

Documentation

Index

Constants

View Source
const (
	ErrInvalidOp   = `invalid patch op: "%s"`
	ErrInvalidPath = `invalid patch path: "%s"`
)
View Source
const (
	Version string = "v0.19.0"
	Success string = "success"
)
View Source
const AutoDownloadKey string = "auto_download"
View Source
const AutoSaveKey string = "auto_save"
View Source
const JWTSecretKeyKey string = "jwt_secret_key"
View Source
const MagicUIDKey string = "_magic_uid_key_"

Variables

View Source
var (
	ErrOpenAPINotExist = errors.New("webhook/api: OpenAPI does not exist")

	ErrAuthNotExist   = errors.New("webhook/api: Authorization does not exist")
	ErrBlogNotExist   = errors.New("webhook/api: blog does not exist")
	ErrUserNotExist   = errors.New("webhook/api: user does not exist")
	ErrTaskNotExist   = errors.New("webhook/api: task does not exist")
	ErrFilterNotExist = errors.New("webhook/api: filter does not exist")

	ErrExpired    = errors.New("webhook/api: token is expired")
	ErrPermDenied = errors.New("webhook/api: permission denied")

	ErrUserRegistered = errors.New("webhook/api: user registered")
	ErrIncorrectPwd   = errors.New("webhook/api: incorrect password")
	ErrBanned         = errors.New("webhook/api: user has been banned")
)
View Source
var AutoDownload bool

自动下载

开启会极大的占用带宽,建议发送完所有 hook 请求后再下载

View Source
var AutoSave bool

自动另存数据库

View Source
var Backend = group.Group{
	Middlewares: group.M{group.CORS, Index},
	CustomFunc: func(r gin.IRouter) {
		downloader = utils.NewDownloader(config.Path.Full.Public)
		r.StaticFS("/public", downloader)
	},
	HandlerMap: map[string]group.HandlerFunc{
		"/forward/*url": AnyForwardURL,
	},
	Groups: group.G{api},
}

后端

实现了前端页面、获取资源、请求转发

View Source
var JWTSecretKey []byte

用于 JWT 加密的密钥

View Source
var JWTSecretKeyFn = func(*jwt.Token) (any, error) {
	return JWTSecretKey, nil
}
View Source
var UserDB, BlogDB *gorm.DB

Functions

func AnyForwardURL added in v0.19.0

func AnyForwardURL(ctx *gin.Context) (any, error)

请求转发 https://blog.csdn.net/qq_29799655/article/details/113841064

func CloseDB added in v0.15.0

func CloseDB() error

func DeduplicateFilters added in v0.19.0

func DeduplicateFilters(filters []model.Filter) (result []model.Filter)

筛选条件去重

func DeleteTaskID added in v0.15.0

func DeleteTaskID(ctx *gin.Context) (any, error)

移除任务

func DownloadAssets added in v0.18.0

func DownloadAssets(blog *model.Blog) error

下载资源

func Error added in v0.10.0

func Error(ctx *gin.Context, err error)

func GetBlogID added in v0.15.0

func GetBlogID(ctx *gin.Context) (any, error)

获取单条博文

func GetBlogs added in v0.15.0

func GetBlogs(ctx *gin.Context) (any, error)

获取博文

func GetExecute added in v0.17.0

func GetExecute(ctx *gin.Context) (any, error)

执行命令

func GetFollowing added in v0.19.0

func GetFollowing(ctx *gin.Context) (any, error)

获取关注的博文

func GetOnline added in v0.14.0

func GetOnline(ctx *gin.Context) (any, error)

获取在线状态

func GetPing added in v0.14.0

func GetPing(ctx *gin.Context) (any, error)

更新在线时间

func GetShutdown added in v0.15.0

func GetShutdown(ctx *gin.Context) (any, error)

优雅关机

func GetTaskID added in v0.15.0

func GetTaskID(ctx *gin.Context) (any, error)

获取任务

func GetTasks added in v0.19.0

func GetTasks(ctx *gin.Context) (any, error)

获取任务集

func GetToken

func GetToken(ctx *gin.Context) (data any, err error)

获取 Token

func GetUID added in v0.15.0

func GetUID(ctx *gin.Context) string

func GetUser

func GetUser(ctx *gin.Context) (any, error)

获取自身信息

func GetUserUID added in v0.15.0

func GetUserUID(ctx *gin.Context) (any, error)

获取用户信息

func GetValid added in v0.16.0

func GetValid(ctx *gin.Context) (any, error)

校验鉴权码

func GetVersion added in v0.14.0

func GetVersion(ctx *gin.Context) (any, error)

获取版本信息

func Index added in v0.18.0

func Index(ctx *gin.Context)

func Info added in v0.15.0

func Info(ctx *gin.Context)

func Initial added in v0.19.0

func Initial(cfg *Config) error

func IsAdmin added in v0.14.0

func IsAdmin(ctx *gin.Context)

func IsOwner added in v0.15.0

func IsOwner(ctx *gin.Context)

func IsUser added in v0.15.0

func IsUser(ctx *gin.Context)

func JWTAuth added in v0.15.0

func JWTAuth(ctx *gin.Context) (uid string, err error)

JWT 校验

func LoadDir added in v0.18.0

func LoadDir(root, path string) error

func LoadFile added in v0.18.0

func LoadFile(root, file string)

func LoadOrStore added in v0.19.0

func LoadOrStore[T any](extra map[string]any, key string, value T) (actual T, loaded bool)

从配置中读取值,如果不存在则写入

func LoggerConvertor added in v0.19.0

func LoggerConvertor(f group.HandlerFunc) gin.HandlerFunc

func PatchTaskID added in v0.15.0

func PatchTaskID(ctx *gin.Context) (any, error)

修改任务

func PatchUserBan added in v0.15.0

func PatchUserBan(ctx *gin.Context, me, user *model.User, patch PatchBody) error

func PatchUserName added in v0.15.0

func PatchUserName(ctx *gin.Context, me, user *model.User, patch PatchBody) error

func PatchUserNickname added in v0.15.0

func PatchUserNickname(ctx *gin.Context, me, user *model.User, patch PatchBody) error

func PatchUserRole added in v0.15.0

func PatchUserRole(ctx *gin.Context, me, user *model.User, patch PatchBody) error

func PatchUserUID added in v0.19.0

func PatchUserUID(ctx *gin.Context) (any, error)

修改用户信息

func PostBlog added in v0.15.0

func PostBlog(ctx *gin.Context) (any, error)

提交博文

func PostBlogs added in v0.19.0

func PostBlogs(ctx *gin.Context) (any, error)

获取筛选后博文

func PostTask added in v0.15.0

func PostTask(ctx *gin.Context) (any, error)

提交任务

func PostTest added in v0.15.0

func PostTest(ctx *gin.Context) (any, error)

测试任务

func PostUpload added in v0.18.0

func PostUpload(ctx *gin.Context) (any, error)

上传文件

func PostUser added in v0.19.0

func PostUser(ctx *gin.Context) (any, error)

注册账户

Types

type Condition added in v0.19.0

type Condition struct {
	// 是否包含转发
	Reply bool `json:"reply" form:"reply"`

	// 是否包含评论
	Comments bool `json:"comments" form:"comments"`

	// 查询排列顺序
	Order string `json:"order" form:"order"`

	// 查询行数
	Limit int `json:"limit" form:"limit"`

	// 查询偏移
	Offset int `json:"offset" form:"offset"`

	// 其他条件
	Conds []string `json:"conds" form:"conds"`
}

查询条件

func (*Condition) Find added in v0.19.0

func (c *Condition) Find(tx *gorm.DB, dest any, filters ...model.Filter) error

条件查询博文

func (*Condition) Finds added in v0.19.0

func (c *Condition) Finds(tx *gorm.DB, filters ...model.Filter) (blogs []model.Blog, err error)

type Config added in v0.19.0

type Config struct {
	Filename string         `toml:"-"      yaml:"-"      json:"-"`      // 配置文件保存路径
	Path     Path           `toml:"path"   yaml:"path"   json:"path"`   // 各文件路径
	Server   Server         `toml:"server" yaml:"server" json:"server"` // 服务器参数
	Role     Role           `toml:"role"   yaml:"role"   json:"role "`  // 权限组
	Extra    map[string]any `toml:"extra"  yaml:"extra"  json:"extra"`  // 额外项
}

webhook 配置

func (*Config) AfterInitial added in v0.19.0

func (c *Config) AfterInitial()

func (*Config) Export added in v0.19.0

func (c *Config) Export() (err error)

func (*Config) Import added in v0.19.0

func (c *Config) Import() (err error)

type PatchBody added in v0.15.0

type PatchBody struct {
	Op    string `json:"op"` // [replace, add, remove, move, copy, test]
	Path  string `json:"path"`
	Value string `json:"value,omitempty"`
	From  string `json:"from,omitempty"`
}

type Path added in v0.19.0

type Path struct {
	Root   string `toml:"root"   yaml:"root"   json:"root"   default:"resource"`               // 程序根目录
	Logs   string `toml:"logs"   yaml:"logs"   json:"logs"   default:"logs"     join:"Root"`   // 记录文件夹
	UserDB string `toml:"userDB" yaml:"userDB" json:"userDB" default:"users.db" join:"Root"`   // 用户数据库文件
	BlogDB string `toml:"blogDB" yaml:"blogDB" json:"blogDB" default:"blogs.db" join:"Root"`   // 博文数据库文件
	Public string `toml:"public" yaml:"public" json:"public" default:"public"   join:"Root"`   // 公开文件夹
	Backup string `toml:"backup" yaml:"backup" json:"backup" default:"backup"   join:"Public"` // 博文数据库备份文件夹
	Upload string `toml:"upload" yaml:"upload" json:"upload" default:"upload"   join:"Public"` // 上传文件文件夹

	Full *Path `toml:"-" yaml:"-" json:"-"` // 以上字段的全路径 通过 utils.NewJoin 函数拼接
}

func (*Path) AfterInitial added in v0.19.0

func (p *Path) AfterInitial() (err error)

func (*Path) CopyBlogDB added in v0.19.0

func (p *Path) CopyBlogDB()

type Role added in v0.19.0

type Role struct {
	Owner string   `toml:"owner" yaml:"owner" json:"owner"` // 所有者
	Admin []string `toml:"admin" yaml:"admin" json:"admin"` // 管理员
}

type Server added in v0.19.0

type Server struct {
	Mode string `toml:"mode" yaml:"mode" json:"mode" default:"release"` // 模式
	Host string `toml:"host" yaml:"host" json:"host" default:"0.0.0.0"` // 主机
	Port uint16 `toml:"port" yaml:"port" json:"port" default:"9000"`    // 端口
}

func (Server) Addr added in v0.19.0

func (s Server) Addr() string

type UserClaims added in v0.15.0

type UserClaims struct {
	UID      string `json:"uid" gorm:"primaryKey"`
	IssuedAt int64  `json:"iat"`
}

用户声明

func (UserClaims) TableName added in v0.17.3

func (UserClaims) TableName() string

func (UserClaims) Token added in v0.15.0

func (c UserClaims) Token() (string, error)

func (UserClaims) Valid added in v0.15.0

func (c UserClaims) Valid() error

Jump to

Keyboard shortcuts

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