miknas

package
v0.0.0-...-b03310d Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnyToStr

func AnyToStr(s any) string

func CheckConvInt

func CheckConvInt(value any) (any, error)

func CheckConvMap

func CheckConvMap(value any) (any, error)

func CheckConvStr

func CheckConvStr(value any) (any, error)

func CreateSlogLogger

func CreateSlogLogger(app *App, name string, withConsole bool) *slog.Logger

func DecomposeFsid

func DecomposeFsid(fsid string) (Fstype, fssubid string)

func HandleFailRetMiddleware

func HandleFailRetMiddleware(c *gin.Context)

func LooseCalcFolderSize

func LooseCalcFolderSize(folder string) int64

func VueHandler

func VueHandler(f http.FileSystem) gin.HandlerFunc

Types

type App

type App struct {
	ConfMgr *ConfigManager
	AuthMgr *AuthResMgr

	Db         *gorm.DB
	WorkSpace  IDiskFsDriver
	FileSpaces map[string]IFileSpace
	// contains filtered or unexported fields
}

func NewApp

func NewApp(r *gin.RouterGroup) *App

func (*App) AddExt

func (a *App) AddExt(ext IExtension)

register one extension

func (*App) GetExt

func (a *App) GetExt(extid string) IExtension

func (*App) GetExtids

func (a *App) GetExtids() []string

func (*App) GetLogger

func (a *App) GetLogger(name string) *slog.Logger

func (*App) InjectToCtxMiddleware

func (a *App) InjectToCtxMiddleware() gin.HandlerFunc

a middleware for inject cur app to gin context

func (*App) RegFileSpace

func (a *App) RegFileSpace(filespace IFileSpace) error

注册文件空间

func (*App) StartInit

func (a *App) StartInit()

type AuthResId

type AuthResId string

权限资源Id的类型,建议用Extension.Res生成

type AuthResItem

type AuthResItem struct {
	// 资源id,统一格式为"extid/resouce[@w][:action]",
	// 中括号里面是可选的,带有@w表示是白名单权限,默认值为true
	ResId      AuthResId
	Desc       string
	SendClient bool
	// indicate who register it
	ExtId string
	// default的值其实是看资源里面是否带有@w,这里是缓存起来避免再次计算
	Default bool
}

AuthResItem need to be declare before use. We assume that all config value are jsonable, because sometime we need to send to client!

type AuthResMgr

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

全局的 权限资源 管理器,只管资源的注册,不管权限认证

func NewAuthResMgr

func NewAuthResMgr() *AuthResMgr

func (*AuthResMgr) FilterDict

func (m *AuthResMgr) FilterDict(authDict map[AuthResId]bool) map[AuthResId]bool

func (*AuthResMgr) GetItem

func (m *AuthResMgr) GetItem(resid AuthResId) *AuthResItem

func (*AuthResMgr) HasRes

func (m *AuthResMgr) HasRes(resid AuthResId) bool

func (*AuthResMgr) PackAllAuthInfo

func (m *AuthResMgr) PackAllAuthInfo() gin.H

func (*AuthResMgr) PackClientDict

func (m *AuthResMgr) PackClientDict(ua IUserAuth) gin.H

func (*AuthResMgr) PrintAllAuthInfo

func (m *AuthResMgr) PrintAllAuthInfo()

func (*AuthResMgr) RegAuthItem

func (m *AuthResMgr) RegAuthItem(item AuthResItem) error

type BaseFileSpace

type BaseFileSpace struct {
	Fstype string

	ResId AuthResId
	Ext   IExtension
	// contains filtered or unexported fields
}

func NewBaseFileSpace

func NewBaseFileSpace(fstype, rootPath string, resid AuthResId) *BaseFileSpace

func (*BaseFileSpace) Ensure

func (fsp *BaseFileSpace) Ensure(ch *ContextHelper, mode string)

func (*BaseFileSpace) GetAddr

func (fsp *BaseFileSpace) GetAddr(self IFileSpace, ch *ContextHelper, fssubid string, fspath string) string

func (*BaseFileSpace) GetFstype

func (fsp *BaseFileSpace) GetFstype() string

func (*BaseFileSpace) GetRelExt

func (fsp *BaseFileSpace) GetRelExt() IExtension

func (*BaseFileSpace) ModeRes

func (fsp *BaseFileSpace) ModeRes(mode string) AuthResId

func (*BaseFileSpace) NewFsDriver

func (fsp *BaseFileSpace) NewFsDriver(ch *ContextHelper, fssubid string) IFsDriver

func (*BaseFileSpace) NewFsDriverByAddr

func (fsp *BaseFileSpace) NewFsDriverByAddr(fsaddr string) IFsDriver

func (*BaseFileSpace) RegAuth

func (fsp *BaseFileSpace) RegAuth(folderDesc string, sendClient bool)

func (*BaseFileSpace) SetRelExt

func (fsp *BaseFileSpace) SetRelExt(ext IExtension)

type BaseFsDriver

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

func NewBaseFsDriver

func NewBaseFsDriver(rootPath string, checkExist bool) *BaseFsDriver

func (*BaseFsDriver) CalcDirSize

func (*BaseFsDriver) CalcDirSize(self IFsDriver, fspath string) (int64, error)

func (*BaseFsDriver) CopyAll

func (*BaseFsDriver) CopyAll(self IFsDriver, fspath string, toFspath string) error

func (*BaseFsDriver) GetRoot

func (fsh *BaseFsDriver) GetRoot() string

func (*BaseFsDriver) ListDir

func (*BaseFsDriver) ListDir(self IFsDriver, fspath string, needDirSize bool) ([]fs.FileInfo, error)

读取文件夹内容

func (*BaseFsDriver) MkdirAll

func (*BaseFsDriver) MkdirAll(self IFsDriver, fspath string) error

func (*BaseFsDriver) MoveAll

func (*BaseFsDriver) MoveAll(self IFsDriver, fspath string, toFspath string) error

func (*BaseFsDriver) MustAbs

func (fsh *BaseFsDriver) MustAbs(relpath string) string

根据相对于 workspace 的相对路径,获得对应的绝对路径

func (*BaseFsDriver) MustRel

func (fsh *BaseFsDriver) MustRel(fullpath string) string

根据相对于 workspace 的相对路径,获得对应的绝对路径

func (*BaseFsDriver) ReadFile

func (*BaseFsDriver) ReadFile(self IFsDriver, fspath string) ([]byte, error)

func (*BaseFsDriver) RemoveAll

func (*BaseFsDriver) RemoveAll(self IFsDriver, fspath string) error

func (*BaseFsDriver) Stat

func (*BaseFsDriver) Stat(self IFsDriver, fspath string, needDirSize bool) (fs.FileInfo, error)

读取某个文件

func (*BaseFsDriver) WebDownload

func (*BaseFsDriver) WebDownload(self IFsDriver, ch *ContextHelper, fspath string)

func (*BaseFsDriver) WebSaveUploadedFile

func (*BaseFsDriver) WebSaveUploadedFile(self IFsDriver, ch *ContextHelper, file *multipart.FileHeader, fspath string) error

func (*BaseFsDriver) WebView

func (*BaseFsDriver) WebView(self IFsDriver, ch *ContextHelper, fspath string)

func (*BaseFsDriver) WriteFile

func (*BaseFsDriver) WriteFile(self IFsDriver, fspath string, data []byte) error

type ConfItem

type ConfItem struct {
	Key        string
	Default    any
	Desc       string
	SendClient bool
	// indicate who register it
	ExtId string
	// validate input value and convert to the right value.
	CheckConv func(any) (any, error)
}

Config Item need to be declare before use. We assume that all config value are jsonable, because sometime we need to send to client!

type ConfigManager

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

func NewConfigManager

func NewConfigManager() *ConfigManager

func (*ConfigManager) Get

func (m *ConfigManager) Get(k string) any

func (*ConfigManager) IsConfKey

func (m *ConfigManager) IsConfKey(k string) bool

func (*ConfigManager) PackClientDict

func (m *ConfigManager) PackClientDict() gin.H

func (*ConfigManager) PrintConfigs

func (m *ConfigManager) PrintConfigs()

func (*ConfigManager) RegConfItem

func (m *ConfigManager) RegConfItem(item ConfItem) error

func (*ConfigManager) UpdateFromEnv

func (m *ConfigManager) UpdateFromEnv()

func (*ConfigManager) UpdateFromMap

func (m *ConfigManager) UpdateFromMap(obj map[string]any)

func (*ConfigManager) UpdateValue

func (m *ConfigManager) UpdateValue(k string, v any) error

type ContextHelper

type ContextHelper struct {
	Ctx *gin.Context
}

func MakeCtxHelper

func MakeCtxHelper(c *gin.Context) *ContextHelper

func (*ContextHelper) BindJSON

func (ch *ContextHelper) BindJSON(obj any)

func (*ContextHelper) ClientUrl

func (ch *ContextHelper) ClientUrl(suburl string) string

func (*ContextHelper) Ensure

func (ch *ContextHelper) Ensure(resid AuthResId)

func (*ContextHelper) EnsureNoErr

func (ch *ContextHelper) EnsureNoErr(err error)

if err happen, stop curent request and return err massage to client

func (*ContextHelper) FailResp

func (ch *ContextHelper) FailResp(format string, a ...any)

func (*ContextHelper) FailRespWithStatus

func (ch *ContextHelper) FailRespWithStatus(status int, format string, a ...any)

func (*ContextHelper) GetApp

func (ch *ContextHelper) GetApp() *App

func (*ContextHelper) GetFixSession

func (ch *ContextHelper) GetFixSession() sessions.Session

func (*ContextHelper) GetRelExt

func (ch *ContextHelper) GetRelExt() IExtension

func (*ContextHelper) GetSession

func (ch *ContextHelper) GetSession() sessions.Session

func (*ContextHelper) GetSessionId

func (ch *ContextHelper) GetSessionId() string

func (*ContextHelper) GetUserAuth

func (ch *ContextHelper) GetUserAuth() IUserAuth

func (*ContextHelper) Jump

func (ch *ContextHelper) Jump(message, jumpurl string, cd int, flag bool)

func (*ContextHelper) Logger

func (ch *ContextHelper) Logger() *slog.Logger

func (*ContextHelper) MustBind

func (ch *ContextHelper) MustBind(obj any)

func (*ContextHelper) OpenFs

func (ch *ContextHelper) OpenFs(fsid string, mode string) IFsDriver

func (*ContextHelper) Redirect

func (ch *ContextHelper) Redirect(location string)

func (*ContextHelper) SetFailStatus

func (ch *ContextHelper) SetFailStatus(status int)

func (*ContextHelper) SucResp

func (ch *ContextHelper) SucResp(ret any)

func (*ContextHelper) UsLog

func (ch *ContextHelper) UsLog(msg string, args ...any)

type Extension

type Extension struct {
	ExtId  string
	App    *App
	Router *gin.RouterGroup
	// contains filtered or unexported fields
}

func NewExtension

func NewExtension(ExtId string) Extension

func (*Extension) ClientUrl

func (r *Extension) ClientUrl(suburl string) string

func (*Extension) GET

func (r *Extension) GET(relativePath string, handlers ...HandlerFunc) gin.IRoutes

func (*Extension) GetApp

func (r *Extension) GetApp() *App

func (*Extension) GetId

func (r *Extension) GetId() string

func (*Extension) GetLogger

func (r *Extension) GetLogger(name string) *slog.Logger

func (*Extension) GetRouter

func (r *Extension) GetRouter() *gin.RouterGroup

func (*Extension) GetUserAuth

func (r *Extension) GetUserAuth(ch *ContextHelper) IUserAuth

如果是开发替代默认的权限校验功能的扩展必须实现这个,用户修改 MIKNAS_AUTH_EXTS 的配置即可

func (*Extension) Logger

func (r *Extension) Logger() *slog.Logger

func (*Extension) OnBind

func (r *Extension) OnBind()

在App注册扩展的时候

func (*Extension) OnInit

func (r *Extension) OnInit()

在加载完配置等之后,App初始化完后

func (*Extension) POST

func (r *Extension) POST(relativePath string, handlers ...HandlerFunc) gin.IRoutes

func (*Extension) RegAuth

func (r *Extension) RegAuth(resid AuthResId, desc string, sendClient bool) error

func (*Extension) RegConf

func (r *Extension) RegConf(item ConfItem) error

func (*Extension) RegFileSpace

func (r *Extension) RegFileSpace(filespace IFileSpace)

func (*Extension) RegIntConf

func (r *Extension) RegIntConf(key string, defv any, desc string, sendClient bool) error

func (*Extension) RegMapConf

func (r *Extension) RegMapConf(key string, defv any, desc string, sendClient bool) error

func (*Extension) RegStrConf

func (r *Extension) RegStrConf(key string, defv any, desc string, sendClient bool) error

func (*Extension) Res

func (r *Extension) Res(resource string) AuthResId

func (*Extension) ServerUrl

func (r *Extension) ServerUrl(suburl string) string

type FailRet

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

func (*FailRet) Error

func (p *FailRet) Error() string

func (*FailRet) MakeRespond

func (p *FailRet) MakeRespond(c *gin.Context)

type H

type H map[string]any

type HandlerFunc

type HandlerFunc func(*ContextHelper)

func UseReqPool

func UseReqPool(key string, cap int) HandlerFunc

type IDiskFsDriver

type IDiskFsDriver interface {
	// 硬盘上的文件系统
	IFsDriver
	MustAbs(fspath string) string   // 根据fspath返回在真实文件系统中的路径
	MustRel(fullpath string) string // 根据真实文件系统的路径来计算在Fs中的路径
}

type IExtension

type IExtension interface {
	GetApp() *App
	GetId() string
	GetRouter() *gin.RouterGroup
	GetUserAuth(*ContextHelper) IUserAuth
	ClientUrl(string) string
	// 注册权限(资源名称,权限描述,是否发送给客户端)
	RegAuth(AuthResId, string, bool) error
	OnBind()
	OnInit()
	GetLogger(string) *slog.Logger
	Logger() *slog.Logger
	Res(string) AuthResId
	// contains filtered or unexported methods
}

type IFailRet

type IFailRet interface {
	error
	MakeRespond(c *gin.Context)
}

func NewFailRet

func NewFailRet(format string, a ...any) IFailRet

type IFileSpace

type IFileSpace interface {
	GetFstype() string
	GetRelExt() IExtension
	SetRelExt(ext IExtension)
	// mode can be "r" or "w"
	Ensure(ch *ContextHelper, mode string)
	NewFsDriver(ch *ContextHelper, fssubid string) IFsDriver
	NewFsDriverByAddr(fsaddr string) IFsDriver
	GetAddr(self IFileSpace, ch *ContextHelper, fssubid string, fspath string) string
}

type IFsDriver

type IFsDriver interface {
	Stat(self IFsDriver, fspath string, needDirSize bool) (fs.FileInfo, error)
	ListDir(self IFsDriver, fspath string, needDirSize bool) ([]fs.FileInfo, error)
	CalcDirSize(self IFsDriver, fspath string) (int64, error)
	ReadFile(self IFsDriver, fspath string) ([]byte, error)
	WriteFile(self IFsDriver, fspath string, data []byte) error
	MkdirAll(self IFsDriver, fspath string) error
	RemoveAll(self IFsDriver, fspath string) error
	MoveAll(self IFsDriver, fspath string, toFspath string) error
	CopyAll(self IFsDriver, fspath string, toFspath string) error

	// 请求相关的
	WebDownload(self IFsDriver, ch *ContextHelper, fspath string)
	WebView(self IFsDriver, ch *ContextHelper, fspath string)
	WebSaveUploadedFile(self IFsDriver, ch *ContextHelper, file *multipart.FileHeader, fspath string) error
}

func NewCommFsDriver

func NewCommFsDriver(rootPath string) IFsDriver

func NewSingleFileFsDriver

func NewSingleFileFsDriver(rootPath string, checkExist bool) IFsDriver

type IUserAuth

type IUserAuth interface {
	GetUid() string
	MustGetUid() string
	CanAccess(AuthResId) bool
	Refresh()
}

所有需要替代默认权限管理的都需要实现该接口

type MyRSA

type MyRSA struct {
	PrivKey *rsa.PrivateKey
	PubKey  *rsa.PublicKey
}

func MustGenMyRSA

func MustGenMyRSA() MyRSA

func (*MyRSA) Decrypt

func (mr *MyRSA) Decrypt(ciphertext string) (string, error)

decrypt

func (*MyRSA) DumpPrivateKeyBase64

func (mr *MyRSA) DumpPrivateKeyBase64() (string, error)

Dump private key to base64 string Compared with DumpPrivateKeyBuffer this output:

  1. Have no header/tailer line
  2. Key content is merged into one-line format

The output is:

MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2y8mEdCRE8siiI7udpge......2QIDAQAB

func (*MyRSA) DumpPublicKeyBase64

func (mr *MyRSA) DumpPublicKeyBase64() (string, error)

func (*MyRSA) Encrypt

func (mr *MyRSA) Encrypt(plaintext string) (string, error)

encrypt

func (*MyRSA) GenerateKey

func (mr *MyRSA) GenerateKey() error

Generate RSA private/public key

func (*MyRSA) LoadPrivateKeyBase64

func (mr *MyRSA) LoadPrivateKeyBase64(base64key string) error

Load private key from base64

func (*MyRSA) LoadPublicKeyBase64

func (mr *MyRSA) LoadPublicKeyBase64(base64key string) error

type PrimaryStringModel

type PrimaryStringModel struct {
	Sid       string `gorm:"primarykey"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

type PrimaryUintModel

type PrimaryUintModel struct {
	ID        uint `gorm:"primarykey"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

type SimpleFileSpace

type SimpleFileSpace struct {
	BaseFileSpace
}

只需要单个权限的工作空间

func NewSimpleFileSpace

func NewSimpleFileSpace(fstype, rootPath string, resid AuthResId) *SimpleFileSpace

func (*SimpleFileSpace) Ensure

func (fsp *SimpleFileSpace) Ensure(ch *ContextHelper, mode string)

func (*SimpleFileSpace) RegAuth

func (fsp *SimpleFileSpace) RegAuth(folderDesc string, sendClient bool)

type SingleFileFsDriver

type SingleFileFsDriver struct {
	BaseFsDriver
}

单文件的一个Fstype

func (*SingleFileFsDriver) ListDir

func (fsd *SingleFileFsDriver) ListDir(self IFsDriver, fspath string, needDirSize bool) ([]fs.FileInfo, error)

读取文件夹内容

func (*SingleFileFsDriver) MustAbs

func (fsd *SingleFileFsDriver) MustAbs(relpath string) string

func (*SingleFileFsDriver) MustRel

func (fsd *SingleFileFsDriver) MustRel(fullpath string) string

Jump to

Keyboard shortcuts

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