core

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: MIT Imports: 33 Imported by: 0

README

FBS 系统内核

基于Gin的二次封装, 对常用模块进行封装, 开发仅着重于业务层开发, 从而实现项目的快速开发和部署.
模块
  • 日志模块
  • 缓存模块
  • 数据库模块
  • 定时作业模块
  • HTTP服务模块
  • 系统管理中心模块
  • 系统资源管理模块
  • 接口调试模块
特性
  • 支持从Web页面进行系统配置, 包括数据库, 缓存, 端口, 超管用户等.
  • 系统配置中心模块集成了服务器资源一栏, 后台服务管理, 方便管理员对系统进行操作
  • 基于gin的路由封装, 可以自动完成简单的查询构造
  • 自动生成接口一览, 及调试画面(开发中)
  • 自动生成资源一览, 用于权限分配(开发中)
  • 支持本地缓存和redis(待开发)
  • 支持sqlite, mysql, postgresql 三款数据库
启动
package main

import (
	"fmt"
	"os"

	"github.com/fbs-io/core"
)

func main() {
    c, err := core.New()
    if err != nil {
        fmt.Println("初始化失败, 错误:", err)
        os.Exit(2)
    }

    ajax := c.Group("ajax")

    dim := ajax.Group("dim", "字典数据")

    pkl := dim.Group("picklist", "码值表")

    pkl.GET("list", "获取码值列表", params{}, func(ctx core.Context) {
        ctx.JSON(errno.ERRNO_OK.ToMapWithData("请求成功"))

    })

    c.Run()

}

Documentation

Overview

  • @Author: reel

  • @Date: 2023-05-21 20:49:21

  • @LastEditors: reel

  • @LastEditTime: 2024-03-27 04:47:04

  • @Description: 用于管理在core上的各个模块, 如cache, db, handle等

  • @Author: reel

  • @Date: 2023-06-15 07:35:00

  • @LastEditors: reel

  • @LastEditTime: 2024-03-27 04:47:12

  • @Description: 基于gin的上下文进行封装

  • @Author: reel

  • @Date: 2023-05-11 23:25:29

  • @LastEditors: reel

  • @LastEditTime: 2024-03-27 04:41:41

  • @Description: 管理核心组件的启动和运行

- @Author: reel - @Date: 2023-05-16 20:17:56 - @LastEditors: reel - @LastEditTime: 2024-03-19 06:44:10 - @Description: 系统配置相关操作

  • @Author: reel

  • @Date: 2023-05-11 23:32:22

  • @LastEditors: reel

  • @LastEditTime: 2023-06-14 21:27:34

  • @Description: 管理接口

  • @Author: reel

  • @Date: 2023-07-19 00:08:08

  • @LastEditors: reel

  • @LastEditTime: 2024-03-27 05:54:30

  • @Description: 常用的中间件

  • @Author: reel

  • @Date: 2023-06-16 05:57:22

* @LastEditors: reel * @LastEditTime: 2024-03-27 04:44:07

  • @Description: 系统资源model, 用于管理API及菜单

Index

Constants

View Source
const (
	CTX_TX                  = "ctx_tx"                       // 上下文的数据库信息
	CTX_PARAMS              = "ctx_params"                   // 上下文的参数
	CTX_AUTH                = consts.CTX_AUTH                // 上下文的操作用户
	CTX_REFLECT_VALUE       = "reflect_value"                // 上下文中的反射值,用于自动校验并生成参数
	CTX_SHARDING_KEY        = consts.CTX_SHARDING_KEY        // 上下文的数据分区
	CTX_DATA_PERMISSION_KEY = consts.CTX_DATA_PERMISSION_KEY // 上下文的数据权限

	// 通过ctx生成查询tx的方式
	// 适用于表中有id的查询, 通过子查询优化分页性能
	TX_QRY_MODE_SUBID = "subid"
	TX_QRY_DELETE     = true
)
View Source
const (
	COOKIE_SID               = "ASID"
	SINGULAR_TYPE_COOKIE     = "cookie"
	SINGULAR_TYPE_SID        = "sid"
	SINGULAR_TYPE_TOKEN      = "token"
	SINGULAR_TYPE_CSRF_TOKEN = "CSRF-TOKEN"
)
View Source
const (
	SOURCE_TYPE_LIMITED      int8 = iota // 受限, 前端不可访问
	SOURCE_TYPE_UNLIMITED                // 不受限菜单或按钮/接口
	SOURCE_TYPE_MENU                     // 菜单权限
	SOURCE_TYPE_UNMENU                   // 不受限菜单
	SOURCE_TYPE_PERMISSION               // 按钮/接口权限
	SOURCE_TYPE_UNPERMISSION             // 不受限按钮/接口

	CONST_MENU   = "menu"
	CONST_BUTTON = "button"

	// 判断是否时路由
	SOURCE_ROUTER_NAN int8 = 0 // 不返回路由
	SOURCE_ROUTER_IS  int8 = 1 // 返回路由, 默认菜单返回路由, 也可以单独设置按钮作为路由返回
)

Variables

View Source
var (
	STATIC_PATH_PREFIX = "/static/"
)

Functions

func AddAllowSource

func AddAllowSource(resoures ...string)

请使用 method:path 的方式定义资源 比如 POST:/ajax/login

func CorsMiddleware

func CorsMiddleware(c Core) gin.HandlerFunc

跨域处理中间件

func GetAllowSource

func GetAllowSource(ctx *gin.Context) bool

func LimiterMiddleware

func LimiterMiddleware(c Core) gin.HandlerFunc

限流器

func LogMiddleware

func LogMiddleware(c Core) gin.HandlerFunc

日志中间件

func ParamsMiddleware

func ParamsMiddleware(c Core) gin.HandlerFunc

参数自动生成中间件

会生成参数结构体以及gorm.DB

同时根据约束, 自动完成参数校验

func SetStaticPathPrefix

func SetStaticPathPrefix(prefix string)

func SignatureMiddleware

func SignatureMiddleware(c Core, sop ...SignatureOptFunc) gin.HandlerFunc

校验签名中间件 如果没有登陆, 则会给一个默认的签名 Singular: 默认 token 模式, 同时可以选择cookie,sid, csrftoken方式

Types

type Context

type Context interface {
	// ShouldBindQuery 反序列化 querystring
	// tag: `form:"xxx"` (注:不要写成 query)
	ShouldBindQuery(obj interface{}) error

	// ShouldBindPostForm 反序列化 postform (querystring会被忽略)
	// tag: `form:"xxx"`
	ShouldBindPostForm(obj interface{}) error

	// ShouldBindForm 同时反序列化 querystring 和 postform;
	// 当 querystring 和 postform 存在相同字段时,postform 优先使用。
	// tag: `form:"xxx"`
	ShouldBindForm(obj interface{}) error

	// ShouldBindJSON 反序列化 postjson
	// tag: `json:"xxx"`
	ShouldBindJSON(obj interface{}) error

	// ShouldBindURI 反序列化 path 参数(如路由路径为 /user/:name)
	// tag: `uri:"xxx"`
	ShouldBindURI(obj interface{}) error

	// Redirect 重定向
	Redirect(code int, location string)

	// HTML 返回界面
	HTML(name string, obj interface{})

	// 返回 Json
	JSON(data interface{})

	// Header 获取 Header 对象
	Header() http.Header

	// GetHeader 获取 Header
	GetHeader(key string) string

	// SetHeader 设置 Header
	SetHeader(key, value string)

	// SetCookie 设置cookie
	SetCookie(key, value string)

	// Cookie 根据cookie的key获取值
	Cookie(key string) (value string, err error)

	// RequestInputParams 获取所有参数
	RequestInputParams() url.Values

	// RequestPostFormParams  获取 PostForm 参数
	RequestPostFormParams() url.Values

	// Request 获取 Request 对象
	Request() *http.Request

	// Method 获取 Request.Method
	Method() string
	// Host 获取 Request.Host
	Host() string
	// Path 获取 请求的路径 Request.URL.Path (不附带 querystring)
	Path() string
	// URI 获取 unescape 后的 Request.URL.RequestURI()
	URI() string
	// resource 获取 请求方式和全路径拼接好的字符串
	// 如GET:/api/v1/userlist
	Resource() string

	// 终止并返回信息
	AbortWithError(interface{})

	// CtxGet 获取上下文自定义的一些参数
	CtxGet(key string) interface{}

	// 设置自定义参数在上下文中
	CtxSet(key string, v interface{})

	// 获取二次封装的参数
	CtxGetParams() any

	Next()

	Ctx() *gin.Context

	// 返回通过参数构建好查询参数参数的gorm.DB
	TX(optFunc ...TxOptsFunc) *gorm.DB

	Core() Core

	// 获取用户
	Auth() string

	// 生成新的db查询
	NewTX(optFunc ...TxOptsFunc) *gorm.DB
}

func NewCtx added in v1.1.0

func NewCtx(c Core, ctx *gin.Context) Context

新建一个上下文

type Core

type Core interface {

	// 封装了服务启动和关闭
	// 方便快速启动
	Run()

	// 关闭整个服务
	Shutdown()

	// gin的engine, 用于原生gin方法
	// 可以更灵活的实现开发
	Engine() *gin.Engine

	Group(elativePath string, handlers ...HandlerFunc) RouterGroup

	// 缓存
	Cache() cache.Store

	// 关系数据库
	RDB() rdb.Store

	// session
	Session() session.Session

	// 限流器
	Limiter() *rate.Limiter

	// 配置
	Config() *config.Config

	// 添加启动时的一些作业
	AddStartJobList(...mux.StartJobFunc)
	// contains filtered or unexported methods
}

func New

func New(funcs ...FuncCores) (Core, error)

type FuncCores

type FuncCores func(*options)

func SetLimitNumber

func SetLimitNumber(limitNumber int) FuncCores

设置每秒令牌生成个数

func SetLimitSize

func SetLimitSize(limitSize int) FuncCores

设置令牌桶大小

type HandlerFunc

type HandlerFunc func(Context)

type IRoutes

type IRoutes interface {
	// Any(string, ...HandlerFunc)
	//需要填写相对路由路径, 名称, 参数, 及中间件, 用于在 api 文档和菜单中注册
	//参数为如果为空, 该方法不会在 api 文档中进行注册
	GET(relativePath, pathName string, params interface{}, handlers ...HandlerFunc) (source *Sources)
	//需要填写相对路由路径, 名称, 参数, 及中间件, 用于在 api 文档和菜单中注册
	//参数为如果为空, 该方法不会在 api 文档中进行注册
	PUT(relativePath, pathName string, params interface{}, handlers ...HandlerFunc) (source *Sources)
	//需要填写相对路由路径, 名称, 参数, 及中间件, 用于在 api 文档和菜单中注册
	//参数为如果为空, 该方法不会在 api 文档中进行注册
	POST(relativePath, pathName string, params interface{}, handlers ...HandlerFunc) (source *Sources)
	//需要填写相对路由路径, 名称, 参数, 及中间件, 用于在 api 文档和菜单中注册
	//参数为如果为空, 该方法不会在 api 文档中进行注册
	DELETE(relativePath, pathName string, params interface{}, handlers ...HandlerFunc) (source *Sources)
}

IRoutes 包装gin的IRoutes

type RouterGroup

type RouterGroup interface {
	Group(api, apiName string, handlers ...HandlerFunc) RouterGroup
	IRoutes
	RouterSource
	Core() Core
}

RouterGroup 包装gin的RouterGroup

传入相对路由路径和名称, 用于在菜单中进行注册

type RouterSource

type RouterSource interface {

	// 用于设置某些路由不必写入资源库
	NotWithSource() RouterGroup

	// 用于设置设置模块/api权限
	//
	// 默认所有菜单,api都需要权限设置
	//
	// 对于通用模块如用户个人设置等信息, 可以设置为权限例外, 可以灵活的在初始化阶段完成权限配置
	WithPermission(t int8) RouterGroup

	// 设置为前端路由或非路由
	//
	// 模块部分默认为前端路由(菜单), 默认值 SOURCE_ROUTER_IS = 1, api默认非路由, 为按钮权限, 默认值 SOURCE_ROUTER_NAN=0
	//
	// 通过该方法可以灵活的配置接口/模块的显示规则
	WithRouter(t int8) RouterGroup

	// 去除前端菜单路由前缀,
	//
	// 例如api接口转为前端路由: /ajax/user/list => /user/list
	WithMenuNotPrefix(prefix string) RouterGroup

	// 设置路由隐藏
	WithHidden() RouterGroup

	// 设置前端Meta信息
	WithMeta(key string, value interface{}) RouterGroup
}

用于设置路由和资源的关系

type SignatureOptFunc added in v1.1.0

type SignatureOptFunc func(*signatureOption)

func SetSeesionType added in v1.1.0

func SetSeesionType(sessionType string) SignatureOptFunc

type Sources

type Sources struct {
	SourcesBase
	rdb.Model
	Children []*Sources `json:"children" gorm:"-"`
}

数据库字段

对SourcesBase进行的封装

func (*Sources) BeforeCreate

func (s *Sources) BeforeCreate(tx *gorm.DB) error

func (*Sources) GenRequestKey

func (s *Sources) GenRequestKey() string

拼接请求参数和路由

主要用于权限校验

func (*Sources) SetDescription

func (s *Sources) SetDescription(des string) *Sources

用于外部设置souces

func (*Sources) TableName

func (s *Sources) TableName() string

func (*Sources) WithAllowSignature

func (s *Sources) WithAllowSignature() *Sources

设置允许通过的登陆签名校验的接口

默认所有接口需要签名校验

通过该方法可以设置例外接口, 如登陆接口

func (*Sources) WithHidden

func (s *Sources) WithHidden() *Sources

设置路由隐藏

func (*Sources) WithMenuNotPrefix

func (s *Sources) WithMenuNotPrefix(prefix string) *Sources

去除前端菜单路由前缀

例如api接口转为前端路由: /ajax/user/list => /user/list

func (*Sources) WithMeta

func (s *Sources) WithMeta(key string, value interface{}) *Sources

设置前端Meta信息

func (*Sources) WithPermission

func (s *Sources) WithPermission(t int8) *Sources

用于外部设置souces, 请通过 core.SOURCE_TYPE_* 进行设置

0: 受限资源, 无法访问, 该资源下在子集, 自动去除菜单和组件的前缀; 1:不受限, api和菜单都可访问

2: 受限菜单, 可通过权限设置访问; 3: 不受限菜单, 登陆用户均可访问

4: 受限api, 可通过权限设置访问; 5: 不受限api, 登陆用户均可访问

func (*Sources) WithRouter

func (s *Sources) WithRouter(t int8) *Sources

用于外部设置路由

type SourcesBase

type SourcesBase struct {
	Code  string `json:"code" gorm:"comment:资源代码;uniqueIndex"`           // 资源code
	Name  string `json:"name" gorm:"comment:资源名称;index"`                 // 资源名称,
	Desc  string `json:"desc" gorm:"comment:资源说明"`                       // 资源描述,可用作title
	PCode string `json:"pcode" gorm:"comment:上层资源code;index"`            // 父级code
	Level int8   `json:"level" gorm:"comment:资源层级;index"`                // 层级, 方便定位数据
	Api   string `json:"api" gorm:"comment:资源路径;index"`                  // 资源访问api
	Type  int8   `json:"type" gorm:"comment:资源类型,0表示都可以显示, 1表示受限;index"` // 用于区分资源类型, 可以设置那些是用做权限配置的
	Sort  string `json:"sort" gorm:"comment:资源排序"`                       // 前端菜单顺序
	// API文档用, 请求方法
	Method     string `json:"method" gorm:"comment:后台接口方法"`        // api接口路径
	Params     string `json:"params" gorm:"comment:前端请求参数"`        // db中存储的参数字符串
	AcceptType string `json:"accept_type" gorm:"comment:前端请求参数类型"` // 约束接口传参方式
	// 前端路由菜单用
	IsRouter  int8            `json:"is_router" gorm:"comment:前端用路由判断;index"`            // 主要用于某些button需要展示路由上
	Path      string          `json:"path" gorm:"comment:前端用路径;index"`                   // 前端用组件方法
	Component string          `json:"component" gorm:"comment:组件名称"`                     // 前端组件名称
	Meta      rdb.ModeMapJson `json:"meta" gorm:"type:varchar(1000);comment:前端用路由参数元信息"` // 前端组件原信息
}

系统资源表

用于API文档, 菜单, 权限控制等

当使用core中的路由接口生成路由时, 系统资源会自动注册到这张表中

type TxOptsFunc

type TxOptsFunc func(*txOpts)

func QryDelete

func QryDelete() TxOptsFunc

func SetTxMode

func SetTxMode(mode string) TxOptsFunc

设置查询方式

TX_QRY_MODE_SUBID 表示带id的子查询, 注意: 使用TX_QRY_MODE_SUBID, 必须配合使用 SetTxSubTable 设置表名

func SetTxSubTable

func SetTxSubTable(table string) TxOptsFunc

设置子查询表明

Directories

Path Synopsis
* @Author: reel * @Date: 2023-06-18 10:32:27 * @LastEditors: reel * @LastEditTime: 2023-09-09 19:36:56 * @Description: 入口函数, 用于测试和示例, 不作为项目使用
* @Author: reel * @Date: 2023-06-18 10:32:27 * @LastEditors: reel * @LastEditTime: 2023-09-09 19:36:56 * @Description: 入口函数, 用于测试和示例, 不作为项目使用
* @Author: reel * @Date: 2023-05-16 20:44:40 * @LastEditors: reel * @LastEditTime: 2023-09-12 06:04:50 * @Description: 配置定时任务
* @Author: reel * @Date: 2023-05-16 20:44:40 * @LastEditors: reel * @LastEditTime: 2023-09-12 06:04:50 * @Description: 配置定时任务
internal
config
* @Author: reel * @Date: 2023-05-16 20:17:56 * @LastEditors: reel * @LastEditTime: 2023-09-09 11:33:23 * @Description: 系统配置相关操作
* @Author: reel * @Date: 2023-05-16 20:17:56 * @LastEditors: reel * @LastEditTime: 2023-09-09 11:33:23 * @Description: 系统配置相关操作
msc
pem
* @Author: reel * @Date: 2023-05-16 21:37:52 * @LastEditors: reel * @LastEditTime: 2023-09-10 07:04:41 * @Description: 设置项目唯一标识, 用于记录项目是否启动
* @Author: reel * @Date: 2023-05-16 21:37:52 * @LastEditors: reel * @LastEditTime: 2023-09-10 07:04:41 * @Description: 设置项目唯一标识, 用于记录项目是否启动
pkg
consts
* @Author: reel * @Date: 2023-06-04 22:29:07 * @LastEditors: reel * @LastEditTime: 2023-12-30 11:21:29 * @Description: 请填写简介
* @Author: reel * @Date: 2023-06-04 22:29:07 * @LastEditors: reel * @LastEditTime: 2023-12-30 11:21:29 * @Description: 请填写简介
encrypt
* @Author: reel * @Date: 2023-05-16 21:19:26 * @LastEditors: reel * @LastEditTime: 2023-06-04 22:35:53 * @Description: base62加密解密封装
* @Author: reel * @Date: 2023-05-16 21:19:26 * @LastEditors: reel * @LastEditTime: 2023-06-04 22:35:53 * @Description: base62加密解密封装
env
* @Author: reel * @Date: 2023-05-11 19:52:08 * @LastEditors: reel * @LastEditTime: 2023-06-24 06:45:53 * @Description: 用于配置app环境变量
* @Author: reel * @Date: 2023-05-11 19:52:08 * @LastEditors: reel * @LastEditTime: 2023-06-24 06:45:53 * @Description: 用于配置app环境变量
errno
* @Author: reel * @Date: 2023-05-11 22:19:24 * @LastEditors: reel * @LastEditTime: 2024-03-27 04:45:36 * @Description: 定义常用的错误代码
* @Author: reel * @Date: 2023-05-11 22:19:24 * @LastEditors: reel * @LastEditTime: 2024-03-27 04:45:36 * @Description: 定义常用的错误代码
errorx
* @Author: reel * @Date: 2023-06-04 22:09:34 * @LastEditors: reel * @LastEditTime: 2023-06-04 22:09:36 * @Description: 请填写简介
* @Author: reel * @Date: 2023-06-04 22:09:34 * @LastEditors: reel * @LastEditTime: 2023-06-04 22:09:36 * @Description: 请填写简介
filex
* @Author: reel * @Date: 2023-05-11 22:28:37 * @LastEditors: reel * @LastEditTime: 2023-05-16 21:42:36 * @Description: 请填写简介
* @Author: reel * @Date: 2023-05-11 22:28:37 * @LastEditors: reel * @LastEditTime: 2023-05-16 21:42:36 * @Description: 请填写简介
mux
* @Description: 用于开启一个 http 服务 * @Params: 可变参数, 可以指定端口号, 服务名称, * @Author: LenLee * @Date: 2022-06-24 21:59:45 * @LastEditTime: 2024-03-27 04:46:10 * @LastEditors: reel * @FilePath:
* @Description: 用于开启一个 http 服务 * @Params: 可变参数, 可以指定端口号, 服务名称, * @Author: LenLee * @Date: 2022-06-24 21:59:45 * @LastEditTime: 2024-03-27 04:46:10 * @LastEditors: reel * @FilePath:
* @Author: reel * @Date: 2023-05-16 21:09:14 * @LastEditors: reel * @LastEditTime: 2023-09-09 20:55:19 * @Description: 请填写简介
* @Author: reel * @Date: 2023-05-16 21:09:14 * @LastEditors: reel * @LastEditTime: 2023-09-09 20:55:19 * @Description: 请填写简介
* @Author: reel * @Date: 2023-06-06 22:40:43 * @LastEditors: reel * @LastEditTime: 2024-03-26 07:01:09 * @Description: session初始化相关设置
* @Author: reel * @Date: 2023-06-06 22:40:43 * @LastEditors: reel * @LastEditTime: 2024-03-26 07:01:09 * @Description: session初始化相关设置
store
cache
* @Author: reel * @Date: 2023-05-16 07:50:35 * @LastEditors: reel * @LastEditTime: 2024-03-27 04:48:15 * @Description: 请填写简介
* @Author: reel * @Date: 2023-05-16 07:50:35 * @LastEditors: reel * @LastEditTime: 2024-03-27 04:48:15 * @Description: 请填写简介
dsn
* @Author: reel * @Date: 2023-05-16 20:02:15 * @LastEditors: reel * @LastEditTime: 2024-01-13 23:26:46 * @Description: 配置数据库,缓存的链接, 支持本地缓存和本地数据库
* @Author: reel * @Date: 2023-05-16 20:02:15 * @LastEditors: reel * @LastEditTime: 2024-01-13 23:26:46 * @Description: 配置数据库,缓存的链接, 支持本地缓存和本地数据库
rdb
* @Author: reel * @Date: 2023-10-15 07:48:02 * @LastEditors: reel * @LastEditTime: 2024-01-20 14:13:22 * @Description: 回掉函数
* @Author: reel * @Date: 2023-10-15 07:48:02 * @LastEditors: reel * @LastEditTime: 2024-01-20 14:13:22 * @Description: 回掉函数

Jump to

Keyboard shortcuts

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