display.Service now exposes a SchemeHandler interface; new scheme_handler.go implements the core:// dispatcher covering the 7 RFC routes: - settings, store, network, models → core.QUERY dispatch - agent, wallet, identity → core.ACTION dispatch Validates URL shape (rejects paths beyond the scheme host, malformed URLs), unknown routes return a named error. Good/Bad/Ugly tests + godoc example. AssetServer startup wiring deferred — no tracked Wails bootstrap (application.New / wails.Run / AssetServer config) exists in this worktree yet; handler is ready for wiring when that lands. Closes tasks.lthn.sh/view.php?id=15 Co-authored-by: Codex <noreply@openai.com> Co-Authored-By: Virgil <virgil@lethean.io>
133 lines
3.5 KiB
Go
133 lines
3.5 KiB
Go
package display
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
"strings"
|
|
|
|
core "dappco.re/go/core"
|
|
coreerr "dappco.re/go/core/log"
|
|
)
|
|
|
|
type routeDispatchKind uint8
|
|
|
|
const (
|
|
routeDispatchQuery routeDispatchKind = iota
|
|
routeDispatchAction
|
|
)
|
|
|
|
var coreRouteDispatch = map[string]routeDispatchKind{
|
|
"settings": routeDispatchQuery,
|
|
"store": routeDispatchQuery,
|
|
"network": routeDispatchQuery,
|
|
"models": routeDispatchQuery,
|
|
"agent": routeDispatchAction,
|
|
"wallet": routeDispatchAction,
|
|
"identity": routeDispatchAction,
|
|
}
|
|
|
|
type coreSchemeHandler struct {
|
|
core *core.Core
|
|
}
|
|
|
|
// NewCoreSchemeHandler returns the RFC route dispatcher for `core://` URLs.
|
|
//
|
|
// handler := display.NewCoreSchemeHandler(c)
|
|
func NewCoreSchemeHandler(c *core.Core) RouteSchemeHandler {
|
|
return coreSchemeHandler{core: c}
|
|
}
|
|
|
|
// SchemeHandler exposes the RFC route dispatcher for the active display service.
|
|
//
|
|
// handler := svc.SchemeHandler()
|
|
func (s *Service) SchemeHandler() RouteSchemeHandler {
|
|
if s == nil || s.ServiceRuntime == nil {
|
|
return coreSchemeHandler{}
|
|
}
|
|
return NewCoreSchemeHandler(s.Core())
|
|
}
|
|
|
|
func (h coreSchemeHandler) Handle(rawURL *url.URL) core.Result {
|
|
if h.core == nil {
|
|
return core.Result{
|
|
Value: coreerr.E("display.coreSchemeHandler.Handle", "core runtime unavailable", nil),
|
|
OK: false,
|
|
}
|
|
}
|
|
|
|
route, dispatch, result := resolveCoreSchemeRoute(rawURL)
|
|
if !result.OK {
|
|
return result
|
|
}
|
|
|
|
target := "core." + route
|
|
switch dispatch {
|
|
case routeDispatchAction:
|
|
return h.core.Action(target).Run(context.Background(), core.NewOptions())
|
|
case routeDispatchQuery:
|
|
result = h.core.Query(target)
|
|
if result.OK {
|
|
return result
|
|
}
|
|
return core.Result{
|
|
Value: coreerr.E("display.coreSchemeHandler.Handle", "query not handled: "+target, nil),
|
|
OK: false,
|
|
}
|
|
default:
|
|
return core.Result{
|
|
Value: coreerr.E("display.coreSchemeHandler.Handle", "unsupported dispatch kind", nil),
|
|
OK: false,
|
|
}
|
|
}
|
|
}
|
|
|
|
func resolveCoreSchemeRoute(rawURL *url.URL) (string, routeDispatchKind, core.Result) {
|
|
if rawURL == nil {
|
|
return "", routeDispatchQuery, core.Result{
|
|
Value: coreerr.E("display.resolveCoreSchemeRoute", "scheme URL is required", nil),
|
|
OK: false,
|
|
}
|
|
}
|
|
if !strings.EqualFold(strings.TrimSpace(rawURL.Scheme), "core") {
|
|
return "", routeDispatchQuery, core.Result{
|
|
Value: coreerr.E("display.resolveCoreSchemeRoute", "unsupported scheme: "+rawURL.Scheme, nil),
|
|
OK: false,
|
|
}
|
|
}
|
|
if strings.TrimSpace(rawURL.Opaque) != "" {
|
|
return "", routeDispatchQuery, core.Result{
|
|
Value: coreerr.E("display.resolveCoreSchemeRoute", "malformed core URL", nil),
|
|
OK: false,
|
|
}
|
|
}
|
|
if rawURL.User != nil || strings.TrimSpace(rawURL.Fragment) != "" || rawURL.Port() != "" {
|
|
return "", routeDispatchQuery, core.Result{
|
|
Value: coreerr.E("display.resolveCoreSchemeRoute", "malformed core URL", nil),
|
|
OK: false,
|
|
}
|
|
}
|
|
if path := strings.TrimSpace(rawURL.Path); path != "" && path != "/" {
|
|
return "", routeDispatchQuery, core.Result{
|
|
Value: coreerr.E("display.resolveCoreSchemeRoute", "malformed core URL", nil),
|
|
OK: false,
|
|
}
|
|
}
|
|
|
|
route := strings.ToLower(strings.TrimSpace(rawURL.Hostname()))
|
|
if route == "" {
|
|
return "", routeDispatchQuery, core.Result{
|
|
Value: coreerr.E("display.resolveCoreSchemeRoute", "malformed core URL", nil),
|
|
OK: false,
|
|
}
|
|
}
|
|
|
|
dispatch, ok := coreRouteDispatch[route]
|
|
if !ok {
|
|
return "", routeDispatchQuery, core.Result{
|
|
Value: coreerr.E("display.resolveCoreSchemeRoute", "unknown core route: "+route, nil),
|
|
OK: false,
|
|
}
|
|
}
|
|
|
|
return route, dispatch, core.Result{OK: true}
|
|
}
|