diff --git a/service/api_client.go b/service/api_client.go new file mode 100644 index 0000000..6640994 --- /dev/null +++ b/service/api_client.go @@ -0,0 +1,122 @@ +package service + +import ( + "axolotl/models" + "bytes" + "encoding/json" + "fmt" + "net/http" + "net/url" +) + +type apiClient struct { + base string + user string + http *http.Client +} + +func (c *apiClient) User() string { return c.user } + +func (c *apiClient) do(method, path string, body any) (*http.Response, error) { + var buf bytes.Buffer + if body != nil { + if err := json.NewEncoder(&buf).Encode(body); err != nil { + return nil, err + } + } + req, err := http.NewRequest(method, c.base+path, &buf) + if err != nil { + return nil, err + } + req.Header.Set("X-Ax-User", c.user) + if body != nil { + req.Header.Set("Content-Type", "application/json") + } + return c.http.Do(req) +} + +func apiDecode[T any](resp *http.Response) (T, error) { + var v T + defer resp.Body.Close() + if resp.StatusCode >= 400 { + var e struct{ Error string } + json.NewDecoder(resp.Body).Decode(&e) + return v, fmt.Errorf("%s", e.Error) + } + return v, json.NewDecoder(resp.Body).Decode(&v) +} + +func (c *apiClient) GetByID(id string) (*models.Node, error) { + resp, err := c.do("GET", "/nodes/"+id, nil) + if err != nil { + return nil, err + } + return apiDecode[*models.Node](resp) +} + +func (c *apiClient) List(filter ListFilter) ([]*models.Node, error) { + q := url.Values{} + for _, r := range filter.Rels { + if r.Target == "" { + q.Add("rel", string(r.Type)) + } else { + q.Add("rel", string(r.Type)+":"+r.Target) + } + } + path := "/nodes" + if len(q) > 0 { + path += "?" + q.Encode() + } + resp, err := c.do("GET", path, nil) + if err != nil { + return nil, err + } + return apiDecode[[]*models.Node](resp) +} + +func (c *apiClient) Add(input AddInput) (*models.Node, error) { + resp, err := c.do("POST", "/nodes", input) + if err != nil { + return nil, err + } + return apiDecode[*models.Node](resp) +} + +func (c *apiClient) Update(id string, input UpdateInput) (*models.Node, error) { + resp, err := c.do("PATCH", "/nodes/"+id, input) + if err != nil { + return nil, err + } + return apiDecode[*models.Node](resp) +} + +func (c *apiClient) Delete(id string) error { + resp, err := c.do("DELETE", "/nodes/"+id, nil) + if err != nil { + return err + } + defer resp.Body.Close() + if resp.StatusCode >= 400 { + var e struct{ Error string } + json.NewDecoder(resp.Body).Decode(&e) + return fmt.Errorf("%s", e.Error) + } + return nil +} + +func (c *apiClient) ListUsers() ([]*models.Node, error) { + resp, err := c.do("GET", "/users", nil) + if err != nil { + return nil, err + } + return apiDecode[[]*models.Node](resp) +} + +func (c *apiClient) AddUser(name string) (*models.Node, error) { + resp, err := c.do("POST", "/users", map[string]string{"name": name}) + if err != nil { + return nil, err + } + return apiDecode[*models.Node](resp) +} + diff --git a/service/config.go b/service/config.go index 34f9dd6..798f5b5 100644 --- a/service/config.go +++ b/service/config.go @@ -19,5 +19,7 @@ type Config interface { DeleteAlias(name string) error ListAliases() ([]*Alias, error) GetServerConfig() ServerConfig + // GetRemoteConfig returns the remote server address and whether remote mode is enabled. + GetRemoteConfig() (ServerConfig, bool) Save() error } diff --git a/service/config_file.go b/service/config_file.go index edfe654..4f6f20d 100644 --- a/service/config_file.go +++ b/service/config_file.go @@ -14,6 +14,7 @@ type fileConfig struct { User string `json:"user"` UserAliases []*Alias `json:"aliases"` Serve ServerConfig `json:"serve"` + Remote ServerConfig `json:"remote"` } var defaultAliases = []*Alias{ @@ -141,6 +142,17 @@ func (c *fileConfig) ListAliases() ([]*Alias, error) { return result, nil } +func (c *fileConfig) GetRemoteConfig() (ServerConfig, bool) { + if c.Remote.Host == "" { + return ServerConfig{}, false + } + port := c.Remote.Port + if port == 0 { + port = 7000 + } + return ServerConfig{Host: c.Remote.Host, Port: port}, true +} + func (c *fileConfig) GetServerConfig() ServerConfig { host := c.Serve.Host if host == "" { diff --git a/service/node_service.go b/service/node_service.go index c901116..e5f7929 100644 --- a/service/node_service.go +++ b/service/node_service.go @@ -4,6 +4,7 @@ import ( "axolotl/models" "axolotl/store" "fmt" + "net/http" ) // NodeService is the single entry point for all node operations. @@ -76,6 +77,10 @@ func GetNodeService(cfg Config) (NodeService, error) { if user == "" { return nil, fmt.Errorf("no user configured: run 'ax user set ' first") } + if rc, ok := cfg.GetRemoteConfig(); ok { + base := fmt.Sprintf("http://%s:%d", rc.Host, rc.Port) + return &apiClient{base: base, user: user, http: &http.Client{}}, nil + } st, err := store.FindAndOpenSQLiteStore() if err != nil { return nil, err