yao/sui/core/interfaces.go
Max 19c2d6b547 Implement dynamic route resolution using rewrite rules
- Introduce a new `RewriteRule` type and associated functions for managing URL rewrite rules in `service/static.go`.
- Add `GetRewriteRules` and `ResolveRoute` functions to facilitate the retrieval and application of rewrite rules.
- Update the `sui/api/run.go` to utilize the new route resolver for handling dynamic routes when the config file is not found.
- Define `RouteResolver` in `sui/core/interfaces.go` to allow for dynamic route resolution across the application.
2026-02-13 23:44:28 +08:00

136 lines
4 KiB
Go

package core
import (
"io"
"net/url"
"regexp"
)
// SUIs the loaded SUI instances
var SUIs = map[string]SUI{}
// RouteMatchers the route matchers for the SUI instance
var RouteMatchers = map[*regexp.Regexp][][]*Matcher{}
// RouteExactMatchers the route exact matchers for the SUI instance
var RouteExactMatchers = map[string][][]*Matcher{}
// RouteRegexp the regexp for the route
var RouteRegexp = regexp.MustCompile(`([a-z0-9A-Z_\-]+)`)
// RouteResolver is a function that resolves a dynamic route path via rewrite rules.
// It takes an incoming route (e.g., /agents/yao.keeper/entry/abc123) and returns
// the resolved template path (e.g., /agents/yao.keeper/entry/[id]) and matched values.
// Set by the service package during initialization.
var RouteResolver func(route string) (string, []string)
// SUI is the interface for the SUI
type SUI interface {
Setting() (*Setting, error)
GetTemplates() ([]ITemplate, error)
GetTemplate(name string) (ITemplate, error)
UploadTemplate(src string, dst string) (ITemplate, error)
WithSid(sid string)
GetSid() string
PublicRootMatcher() *Matcher
GetPublic() *Public
PublicRootWithSid(sid string) (string, error)
PublicRoot(data map[string]any) (string, error)
}
// ITemplate is the interface for the ITemplate
type ITemplate interface {
Pages() ([]IPage, error)
PageTree(route string) ([]*PageTreeNode, error)
Page(route string) (IPage, error)
PageExist(route string) bool
CreatePage(html string) IPage
CreateEmptyPage(route string, setting *PageSetting) (IPage, error)
RemovePage(route string) error
GetPageFromAsset(asset string) (IPage, error)
Blocks() ([]IBlock, error)
BlockLayoutItems() (*BlockLayoutItems, error)
BlockMedia(id string) (*Asset, error)
Block(name string) (IBlock, error)
Components() ([]IComponent, error)
Component(name string) (IComponent, error)
Assets() []string
Locales() []SelectOption
Themes() []SelectOption
Asset(file string, width, height uint) (*Asset, error)
AssetUpload(reader io.Reader, name string) (string, error)
MediaSearch(query url.Values, page int, pageSize int) (MediaSearchResult, error)
Build(option *BuildOption) ([]string, error)
SyncAssets(option *BuildOption) error
SyncAssetFile(file string, option *BuildOption) error
GetRoot() string
ExecBeforeBuildScripts() []TemplateScirptResult
ExecAfterBuildScripts() []TemplateScirptResult
Trans(option *BuildOption) ([]string, error)
GlobRoutes(patterns []string, unique ...bool) ([]string, error)
}
// IPage is the interface for the page
type IPage interface {
Load() error
SUI() (SUI, error)
Sid() (string, error)
Get() *Page
GetConfig() *PageConfig
SaveAs(route string, setting *PageSetting) (IPage, error)
Save(request *RequestSource) error
SaveTemp(request *RequestSource) error
Remove() error
EditorRender() (*ResponseEditorRender, error)
EditorPageSource() SourceData
EditorScriptSource() SourceData
EditorStyleSource() SourceData
EditorDataSource() SourceData
PreviewRender(referer string) (string, error)
AssetScript() (*Asset, error)
AssetStyle() (*Asset, error)
Build(globalCtx *GlobalBuildContext, option *BuildOption) ([]string, error)
BuildAsComponent(globalCtx *GlobalBuildContext, option *BuildOption) ([]string, error)
Trans(globalCtx *GlobalBuildContext, option *BuildOption) ([]string, error)
}
// IBlock is the interface for the block
type IBlock interface {
Compile() (string, error)
Load() error
Source() string
Get() *Block
}
// IComponent is the interface for the component
type IComponent interface {
Compile() (string, error)
Load() error
Source() string
}
// IWatchDirs is an optional interface for templates that need to watch multiple directories
type IWatchDirs interface {
// GetWatchDirs returns all directories that should be watched for changes
// The returned paths are relative to the application source root (not data root)
GetWatchDirs() []string
// GetWatchRoot returns the root directory for watch paths
// Returns "app" for application source root, "data" for data root
GetWatchRoot() string
}