feat: add ax serve command with JSON API backed by NodeService

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-01 13:04:29 +02:00
parent 9e5194893e
commit 3dfc46c3ff
5 changed files with 256 additions and 2 deletions

View File

@@ -6,6 +6,11 @@ type Alias struct {
Description string `json:"description,omitempty"`
}
type ServerConfig struct {
Host string `json:"host"`
Port int `json:"port"`
}
type Config interface {
GetUser() string
SetUser(username string) error
@@ -13,5 +18,6 @@ type Config interface {
SetAlias(alias *Alias) error
DeleteAlias(name string) error
ListAliases() ([]*Alias, error)
GetServerConfig() ServerConfig
Save() error
}

View File

@@ -11,8 +11,9 @@ import (
type fileConfig struct {
path string
User string `json:"user"`
UserAliases []*Alias `json:"aliases"`
User string `json:"user"`
UserAliases []*Alias `json:"aliases"`
Serve ServerConfig `json:"serve"`
}
var defaultAliases = []*Alias{
@@ -140,6 +141,18 @@ func (c *fileConfig) ListAliases() ([]*Alias, error) {
return result, nil
}
func (c *fileConfig) GetServerConfig() ServerConfig {
host := c.Serve.Host
if host == "" {
host = "localhost"
}
port := c.Serve.Port
if port == 0 {
port = 7000
}
return ServerConfig{Host: host, Port: port}
}
func (c *fileConfig) Save() error {
if err := os.MkdirAll(filepath.Dir(c.path), 0755); err != nil {
return err

View File

@@ -82,3 +82,14 @@ func GetNodeService(cfg Config) (NodeService, error) {
}
return &nodeServiceImpl{store: st, userID: user}, nil
}
func GetNodeServiceForUser(user string) (NodeService, error) {
if user == "" {
return nil, fmt.Errorf("user is required")
}
st, err := store.FindAndOpenSQLiteStore()
if err != nil {
return nil, err
}
return &nodeServiceImpl{store: st, userID: user}, nil
}