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:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user