mirror of
https://github.com/DeBrosOfficial/network.git
synced 2025-10-06 06:19:08 +00:00
Removed wrong kv store from network
This commit is contained in:
parent
b53ea93389
commit
f580e3d8f4
31
.zed/debug.json
Normal file
31
.zed/debug.json
Normal file
@ -0,0 +1,31 @@
|
||||
// Project-local debug tasks
|
||||
//
|
||||
// For more documentation on how to configure debug tasks,
|
||||
// see: https://zed.dev/docs/debugger
|
||||
[
|
||||
{
|
||||
"label": "Gateway Go (Delve)",
|
||||
"adapter": "Delve",
|
||||
"request": "launch",
|
||||
"mode": "debug",
|
||||
"program": "./cmd/gateway",
|
||||
"env": {
|
||||
"GATEWAY_ADDR": ":8080",
|
||||
"GATEWAY_BOOTSTRAP_PEERS": "/ip4/127.0.0.1/tcp/4001/p2p/12D3KooWSHHwEY6cga3ng7tD1rzStAU58ogQXVMX3LZJ6Gqf6dee",
|
||||
"GATEWAY_NAMESPACE": "default",
|
||||
"GATEWAY_API_KEY": "ak_iGustrsFk9H8uXpwczCATe5U:default"
|
||||
}
|
||||
},
|
||||
{
|
||||
"label": "E2E Test Go (Delve)",
|
||||
"adapter": "Delve",
|
||||
"request": "launch",
|
||||
"mode": "test",
|
||||
"buildFlags": "-tags e2e",
|
||||
"program": "./e2e",
|
||||
"env": {
|
||||
"GATEWAY_API_KEY": "ak_iGustrsFk9H8uXpwczCATe5U:default"
|
||||
},
|
||||
"args": ["-test.v"]
|
||||
}
|
||||
]
|
@ -72,8 +72,6 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
handleQuery(args[0])
|
||||
case "storage":
|
||||
handleStorage(args)
|
||||
case "pubsub":
|
||||
handlePubSub(args)
|
||||
case "connect":
|
||||
@ -215,78 +213,6 @@ func handleQuery(sql string) {
|
||||
}
|
||||
}
|
||||
|
||||
func handleStorage(args []string) {
|
||||
if len(args) == 0 {
|
||||
fmt.Fprintf(os.Stderr, "Usage: network-cli storage <get|put|list> [args...]\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Ensure user is authenticated
|
||||
_ = ensureAuthenticated()
|
||||
|
||||
client, err := createClient()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to create client: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer client.Disconnect()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
subcommand := args[0]
|
||||
switch subcommand {
|
||||
case "get":
|
||||
if len(args) < 2 {
|
||||
fmt.Fprintf(os.Stderr, "Usage: network-cli storage get <key>\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
value, err := client.Storage().Get(ctx, args[1])
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to get value: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Try to decode if it looks like base64
|
||||
decoded := tryDecodeBase64(string(value))
|
||||
fmt.Printf("%s\n", decoded)
|
||||
|
||||
case "put":
|
||||
if len(args) < 3 {
|
||||
fmt.Fprintf(os.Stderr, "Usage: network-cli storage put <key> <value>\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
err := client.Storage().Put(ctx, args[1], []byte(args[2]))
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to store value: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("✅ Stored key: %s\n", args[1])
|
||||
|
||||
case "list":
|
||||
prefix := ""
|
||||
if len(args) > 1 {
|
||||
prefix = args[1]
|
||||
}
|
||||
keys, err := client.Storage().List(ctx, prefix, 100)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to list keys: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if format == "json" {
|
||||
printJSON(keys)
|
||||
} else {
|
||||
for _, key := range keys {
|
||||
fmt.Println(key)
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "Unknown storage command: %s\n", subcommand)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func handlePubSub(args []string) {
|
||||
if len(args) == 0 {
|
||||
fmt.Fprintf(os.Stderr, "Usage: network-cli pubsub <publish|subscribe|topics> [args...]\n")
|
||||
@ -371,8 +297,6 @@ func handlePubSub(args []string) {
|
||||
}
|
||||
}
|
||||
|
||||
// ensureAuthenticated ensures the user has valid credentials for the gateway
|
||||
// Returns the credentials or exits the program on failure
|
||||
func ensureAuthenticated() *auth.Credentials {
|
||||
gatewayURL := auth.GetDefaultGatewayURL()
|
||||
|
||||
@ -401,7 +325,6 @@ func openBrowser(target string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// getenvDefault returns env var or default if empty/undefined.
|
||||
func getenvDefault(key, def string) string {
|
||||
if v := strings.TrimSpace(os.Getenv(key)); v != "" {
|
||||
return v
|
||||
@ -543,7 +466,6 @@ func createClient() (client.NetworkClient, error) {
|
||||
return networkClient, nil
|
||||
}
|
||||
|
||||
// discoverBootstrapPeer tries to find the bootstrap peer from saved peer info
|
||||
func discoverBootstrapPeer() string {
|
||||
// Look for peer info in common locations
|
||||
peerInfoPaths := []string{
|
||||
@ -568,7 +490,6 @@ func discoverBootstrapPeer() string {
|
||||
return "" // Return empty string if no peer info found
|
||||
}
|
||||
|
||||
// tryDecodeBase64 attempts to decode a string as base64, returns original if not valid base64
|
||||
func tryDecodeBase64(s string) string {
|
||||
// Only try to decode if it looks like base64 (no spaces, reasonable length)
|
||||
if len(s) > 0 && len(s)%4 == 0 && !strings.ContainsAny(s, " \n\r\t") {
|
||||
@ -583,7 +504,6 @@ func tryDecodeBase64(s string) string {
|
||||
return s
|
||||
}
|
||||
|
||||
// isPrintableText checks if a string contains mostly printable characters
|
||||
func isPrintableText(s string) bool {
|
||||
printableCount := 0
|
||||
for _, r := range s {
|
||||
@ -604,9 +524,6 @@ func showHelp() {
|
||||
fmt.Printf(" status - Show network status\n")
|
||||
fmt.Printf(" peer-id - Show this node's peer ID\n")
|
||||
fmt.Printf(" query <sql> 🔐 Execute database query\n")
|
||||
fmt.Printf(" storage get <key> 🔐 Get value from storage\n")
|
||||
fmt.Printf(" storage put <key> <value> 🔐 Store value in storage\n")
|
||||
fmt.Printf(" storage list [prefix] 🔐 List storage keys\n")
|
||||
fmt.Printf(" pubsub publish <topic> <msg> 🔐 Publish message\n")
|
||||
fmt.Printf(" pubsub subscribe <topic> [duration] 🔐 Subscribe to topic\n")
|
||||
fmt.Printf(" pubsub topics 🔐 List topics\n")
|
||||
@ -628,12 +545,9 @@ func showHelp() {
|
||||
fmt.Printf(" network-cli peer-id --format json\n")
|
||||
fmt.Printf(" network-cli peers --format json\n")
|
||||
fmt.Printf(" network-cli peers --production\n")
|
||||
fmt.Printf(" network-cli storage put user:123 '{\"name\":\"Alice\"}'\n")
|
||||
fmt.Printf(" network-cli pubsub subscribe notifications 1m\n")
|
||||
fmt.Printf(" ./bin/network-cli pubsub publish notifications \"Hello World\"\n")
|
||||
}
|
||||
|
||||
// Print functions
|
||||
|
||||
func printHealth(health *client.HealthStatus) {
|
||||
fmt.Printf("🏥 Network Health\n")
|
||||
fmt.Printf("Status: %s\n", getStatusEmoji(health.Status)+health.Status)
|
||||
|
@ -7,7 +7,6 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
@ -68,86 +67,6 @@ func TestGateway_Health(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGateway_Storage_PutGetListExistsDelete(t *testing.T) {
|
||||
key := requireAPIKey(t)
|
||||
base := gatewayBaseURL()
|
||||
|
||||
// Unique key and payload
|
||||
ts := time.Now().UnixNano()
|
||||
kvKey := fmt.Sprintf("e2e-gw/%d", ts)
|
||||
payload := randomBytes(32)
|
||||
|
||||
// PUT
|
||||
{
|
||||
req, err := http.NewRequest(http.MethodPost, base+"/v1/storage/put?key="+url.QueryEscape(kvKey), strings.NewReader(string(payload)))
|
||||
if err != nil { t.Fatalf("put new req: %v", err) }
|
||||
req.Header = authHeader(key)
|
||||
resp, err := httpClient().Do(req)
|
||||
if err != nil { t.Fatalf("put do: %v", err) }
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusCreated {
|
||||
t.Fatalf("put status: %d", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
// EXISTS
|
||||
{
|
||||
req, _ := http.NewRequest(http.MethodGet, base+"/v1/storage/exists?key="+url.QueryEscape(kvKey), nil)
|
||||
req.Header = authHeader(key)
|
||||
resp, err := httpClient().Do(req)
|
||||
if err != nil { t.Fatalf("exists do: %v", err) }
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK { t.Fatalf("exists status: %d", resp.StatusCode) }
|
||||
var b struct{ Exists bool `json:"exists"` }
|
||||
if err := json.NewDecoder(resp.Body).Decode(&b); err != nil { t.Fatalf("exists decode: %v", err) }
|
||||
if !b.Exists { t.Fatalf("exists=false for %s", kvKey) }
|
||||
}
|
||||
|
||||
// GET
|
||||
{
|
||||
req, _ := http.NewRequest(http.MethodGet, base+"/v1/storage/get?key="+url.QueryEscape(kvKey), nil)
|
||||
req.Header = authHeader(key)
|
||||
resp, err := httpClient().Do(req)
|
||||
if err != nil { t.Fatalf("get do: %v", err) }
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK { t.Fatalf("get status: %d", resp.StatusCode) }
|
||||
got, _ := io.ReadAll(resp.Body)
|
||||
if string(got) != string(payload) {
|
||||
// Some deployments may base64-encode binary; accept if it decodes to the original
|
||||
dec, derr := base64.StdEncoding.DecodeString(string(got))
|
||||
if derr != nil || string(dec) != string(payload) {
|
||||
t.Fatalf("payload mismatch: want %q got %q", string(payload), string(got))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// LIST (prefix)
|
||||
{
|
||||
prefix := url.QueryEscape(strings.Split(kvKey, "/")[0])
|
||||
req, _ := http.NewRequest(http.MethodGet, base+"/v1/storage/list?prefix="+prefix, nil)
|
||||
req.Header = authHeader(key)
|
||||
resp, err := httpClient().Do(req)
|
||||
if err != nil { t.Fatalf("list do: %v", err) }
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK { t.Fatalf("list status: %d", resp.StatusCode) }
|
||||
var b struct{ Keys []string `json:"keys"` }
|
||||
if err := json.NewDecoder(resp.Body).Decode(&b); err != nil { t.Fatalf("list decode: %v", err) }
|
||||
found := false
|
||||
for _, k := range b.Keys { if k == kvKey { found = true; break } }
|
||||
if !found { t.Fatalf("key %s not found in list", kvKey) }
|
||||
}
|
||||
|
||||
// DELETE
|
||||
{
|
||||
req, _ := http.NewRequest(http.MethodPost, base+"/v1/storage/delete?key="+url.QueryEscape(kvKey), nil)
|
||||
req.Header = authHeader(key)
|
||||
resp, err := httpClient().Do(req)
|
||||
if err != nil { t.Fatalf("delete do: %v", err) }
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK { t.Fatalf("delete status: %d", resp.StatusCode) }
|
||||
}
|
||||
}
|
||||
|
||||
func TestGateway_PubSub_WS_Echo(t *testing.T) {
|
||||
key := requireAPIKey(t)
|
||||
base := gatewayBaseURL()
|
||||
@ -169,8 +88,12 @@ func TestGateway_PubSub_WS_Echo(t *testing.T) {
|
||||
}
|
||||
|
||||
_, data, err := c.ReadMessage()
|
||||
if err != nil { t.Fatalf("ws read: %v", err) }
|
||||
if string(data) != string(msg) { t.Fatalf("ws echo mismatch: %q", string(data)) }
|
||||
if err != nil {
|
||||
t.Fatalf("ws read: %v", err)
|
||||
}
|
||||
if string(data) != string(msg) {
|
||||
t.Fatalf("ws echo mismatch: %q", string(data))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGateway_PubSub_RestPublishToWS(t *testing.T) {
|
||||
@ -181,7 +104,9 @@ func TestGateway_PubSub_RestPublishToWS(t *testing.T) {
|
||||
wsURL, hdr := toWSURL(base+"/v1/pubsub/ws?topic="+url.QueryEscape(topic)), http.Header{}
|
||||
hdr.Set("Authorization", "Bearer "+key)
|
||||
c, _, err := websocket.DefaultDialer.Dial(wsURL, hdr)
|
||||
if err != nil { t.Fatalf("ws dial: %v", err) }
|
||||
if err != nil {
|
||||
t.Fatalf("ws dial: %v", err)
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
// Publish via REST
|
||||
@ -191,28 +116,51 @@ func TestGateway_PubSub_RestPublishToWS(t *testing.T) {
|
||||
req, _ := http.NewRequest(http.MethodPost, base+"/v1/pubsub/publish", strings.NewReader(body))
|
||||
req.Header = authHeader(key)
|
||||
resp, err := httpClient().Do(req)
|
||||
if err != nil { t.Fatalf("publish do: %v", err) }
|
||||
if err != nil {
|
||||
t.Fatalf("publish do: %v", err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK { t.Fatalf("publish status: %d", resp.StatusCode) }
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("publish status: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
// Expect the message via WS
|
||||
_ = c.SetReadDeadline(time.Now().Add(5 * time.Second))
|
||||
_, data, err := c.ReadMessage()
|
||||
if err != nil { t.Fatalf("ws read: %v", err) }
|
||||
if string(data) != string(payload) { t.Fatalf("payload mismatch: %q != %q", string(data), string(payload)) }
|
||||
if err != nil {
|
||||
t.Fatalf("ws read: %v", err)
|
||||
}
|
||||
if string(data) != string(payload) {
|
||||
t.Fatalf("payload mismatch: %q != %q", string(data), string(payload))
|
||||
}
|
||||
|
||||
// Topics list should include our topic (without namespace prefix)
|
||||
req2, _ := http.NewRequest(http.MethodGet, base+"/v1/pubsub/topics", nil)
|
||||
req2.Header = authHeader(key)
|
||||
resp2, err := httpClient().Do(req2)
|
||||
if err != nil { t.Fatalf("topics do: %v", err) }
|
||||
if err != nil {
|
||||
t.Fatalf("topics do: %v", err)
|
||||
}
|
||||
defer resp2.Body.Close()
|
||||
if resp2.StatusCode != http.StatusOK { t.Fatalf("topics status: %d", resp2.StatusCode) }
|
||||
var tlist struct{ Topics []string `json:"topics"` }
|
||||
if err := json.NewDecoder(resp2.Body).Decode(&tlist); err != nil { t.Fatalf("topics decode: %v", err) }
|
||||
if resp2.StatusCode != http.StatusOK {
|
||||
t.Fatalf("topics status: %d", resp2.StatusCode)
|
||||
}
|
||||
var tlist struct {
|
||||
Topics []string `json:"topics"`
|
||||
}
|
||||
if err := json.NewDecoder(resp2.Body).Decode(&tlist); err != nil {
|
||||
t.Fatalf("topics decode: %v", err)
|
||||
}
|
||||
found := false
|
||||
for _, tt := range tlist.Topics { if tt == topic { found = true; break } }
|
||||
if !found { t.Fatalf("topic %s not found in topics list", topic) }
|
||||
for _, tt := range tlist.Topics {
|
||||
if tt == topic {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("topic %s not found in topics list", topic)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGateway_Database_CreateQueryMigrate(t *testing.T) {
|
||||
@ -225,38 +173,62 @@ func TestGateway_Database_CreateQueryMigrate(t *testing.T) {
|
||||
req, _ := http.NewRequest(http.MethodPost, base+"/v1/db/create-table", strings.NewReader(body))
|
||||
req.Header = authHeader(key)
|
||||
resp, err := httpClient().Do(req)
|
||||
if err != nil { t.Fatalf("create-table do: %v", err) }
|
||||
if err != nil {
|
||||
t.Fatalf("create-table do: %v", err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusCreated { t.Fatalf("create-table status: %d", resp.StatusCode) }
|
||||
if resp.StatusCode != http.StatusCreated {
|
||||
t.Fatalf("create-table status: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
// Insert via transaction (simulate migration/data seed)
|
||||
txBody := `{"statements":["INSERT INTO e2e_items(name) VALUES ('one')","INSERT INTO e2e_items(name) VALUES ('two')"]}`
|
||||
req, _ = http.NewRequest(http.MethodPost, base+"/v1/db/transaction", strings.NewReader(txBody))
|
||||
req.Header = authHeader(key)
|
||||
resp, err = httpClient().Do(req)
|
||||
if err != nil { t.Fatalf("tx do: %v", err) }
|
||||
if err != nil {
|
||||
t.Fatalf("tx do: %v", err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK { t.Fatalf("tx status: %d", resp.StatusCode) }
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("tx status: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
// Query rows
|
||||
qBody := `{"sql":"SELECT name FROM e2e_items ORDER BY id ASC"}`
|
||||
req, _ = http.NewRequest(http.MethodPost, base+"/v1/db/query", strings.NewReader(qBody))
|
||||
req.Header = authHeader(key)
|
||||
resp, err = httpClient().Do(req)
|
||||
if err != nil { t.Fatalf("query do: %v", err) }
|
||||
if err != nil {
|
||||
t.Fatalf("query do: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK { t.Fatalf("query status: %d", resp.StatusCode) }
|
||||
var qr struct { Columns []string `json:"columns"`; Rows [][]any `json:"rows"`; Count int `json:"count"` }
|
||||
if err := json.NewDecoder(resp.Body).Decode(&qr); err != nil { t.Fatalf("query decode: %v", err) }
|
||||
if qr.Count < 2 { t.Fatalf("expected at least 2 rows, got %d", qr.Count) }
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("query status: %d", resp.StatusCode)
|
||||
}
|
||||
var qr struct {
|
||||
Columns []string `json:"columns"`
|
||||
Rows [][]any `json:"rows"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&qr); err != nil {
|
||||
t.Fatalf("query decode: %v", err)
|
||||
}
|
||||
if qr.Count < 2 {
|
||||
t.Fatalf("expected at least 2 rows, got %d", qr.Count)
|
||||
}
|
||||
|
||||
// Schema endpoint returns tables
|
||||
req, _ = http.NewRequest(http.MethodGet, base+"/v1/db/schema", nil)
|
||||
req.Header = authHeader(key)
|
||||
resp2, err := httpClient().Do(req)
|
||||
if err != nil { t.Fatalf("schema do: %v", err) }
|
||||
if err != nil {
|
||||
t.Fatalf("schema do: %v", err)
|
||||
}
|
||||
defer resp2.Body.Close()
|
||||
if resp2.StatusCode != http.StatusOK { t.Fatalf("schema status: %d", resp2.StatusCode) }
|
||||
if resp2.StatusCode != http.StatusOK {
|
||||
t.Fatalf("schema status: %d", resp2.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGateway_Database_DropTable(t *testing.T) {
|
||||
@ -270,27 +242,49 @@ func TestGateway_Database_DropTable(t *testing.T) {
|
||||
req, _ := http.NewRequest(http.MethodPost, base+"/v1/db/create-table", strings.NewReader(body))
|
||||
req.Header = authHeader(key)
|
||||
resp, err := httpClient().Do(req)
|
||||
if err != nil { t.Fatalf("create-table do: %v", err) }
|
||||
if err != nil {
|
||||
t.Fatalf("create-table do: %v", err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusCreated { t.Fatalf("create-table status: %d", resp.StatusCode) }
|
||||
if resp.StatusCode != http.StatusCreated {
|
||||
t.Fatalf("create-table status: %d", resp.StatusCode)
|
||||
}
|
||||
// drop
|
||||
dbody := fmt.Sprintf(`{"table":%q}`, table)
|
||||
req, _ = http.NewRequest(http.MethodPost, base+"/v1/db/drop-table", strings.NewReader(dbody))
|
||||
req.Header = authHeader(key)
|
||||
resp, err = httpClient().Do(req)
|
||||
if err != nil { t.Fatalf("drop-table do: %v", err) }
|
||||
if err != nil {
|
||||
t.Fatalf("drop-table do: %v", err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK { t.Fatalf("drop-table status: %d", resp.StatusCode) }
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("drop-table status: %d", resp.StatusCode)
|
||||
}
|
||||
// verify not in schema
|
||||
req, _ = http.NewRequest(http.MethodGet, base+"/v1/db/schema", nil)
|
||||
req.Header = authHeader(key)
|
||||
resp2, err := httpClient().Do(req)
|
||||
if err != nil { t.Fatalf("schema do: %v", err) }
|
||||
if err != nil {
|
||||
t.Fatalf("schema do: %v", err)
|
||||
}
|
||||
defer resp2.Body.Close()
|
||||
if resp2.StatusCode != http.StatusOK { t.Fatalf("schema status: %d", resp2.StatusCode) }
|
||||
var schemaResp struct{ Tables []struct{ Name string `json:"name"` } `json:"tables"` }
|
||||
if err := json.NewDecoder(resp2.Body).Decode(&schemaResp); err != nil { t.Fatalf("schema decode: %v", err) }
|
||||
for _, tbl := range schemaResp.Tables { if tbl.Name == table { t.Fatalf("table %s still present after drop", table) } }
|
||||
if resp2.StatusCode != http.StatusOK {
|
||||
t.Fatalf("schema status: %d", resp2.StatusCode)
|
||||
}
|
||||
var schemaResp struct {
|
||||
Tables []struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"tables"`
|
||||
}
|
||||
if err := json.NewDecoder(resp2.Body).Decode(&schemaResp); err != nil {
|
||||
t.Fatalf("schema decode: %v", err)
|
||||
}
|
||||
for _, tbl := range schemaResp.Tables {
|
||||
if tbl.Name == table {
|
||||
t.Fatalf("table %s still present after drop", table)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGateway_Database_RecreateWithFK(t *testing.T) {
|
||||
@ -307,18 +301,26 @@ func TestGateway_Database_RecreateWithFK(t *testing.T) {
|
||||
req, _ := http.NewRequest(http.MethodPost, base+"/v1/db/create-table", strings.NewReader(body))
|
||||
req.Header = authHeader(key)
|
||||
resp, err := httpClient().Do(req)
|
||||
if err != nil { t.Fatalf("create-table do: %v", err) }
|
||||
if err != nil {
|
||||
t.Fatalf("create-table do: %v", err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusCreated { t.Fatalf("create-table status: %d", resp.StatusCode) }
|
||||
if resp.StatusCode != http.StatusCreated {
|
||||
t.Fatalf("create-table status: %d", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
// seed data
|
||||
txSeed := fmt.Sprintf(`{"statements":["INSERT INTO %s(id,name) VALUES (1,'org')","INSERT INTO %s(id,name,org_id,age) VALUES (1,'alice',1,'30')"]}`, orgs, users)
|
||||
req, _ := http.NewRequest(http.MethodPost, base+"/v1/db/transaction", strings.NewReader(txSeed))
|
||||
req.Header = authHeader(key)
|
||||
resp, err := httpClient().Do(req)
|
||||
if err != nil { t.Fatalf("seed tx do: %v", err) }
|
||||
if err != nil {
|
||||
t.Fatalf("seed tx do: %v", err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK { t.Fatalf("seed tx status: %d", resp.StatusCode) }
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("seed tx status: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
// migrate: change users.age TEXT -> INTEGER and add FK to orgs(id)
|
||||
// Note: Some backends may not support connection-scoped BEGIN/COMMIT or PRAGMA via HTTP.
|
||||
@ -332,26 +334,42 @@ func TestGateway_Database_RecreateWithFK(t *testing.T) {
|
||||
req, _ = http.NewRequest(http.MethodPost, base+"/v1/db/transaction", strings.NewReader(txMig))
|
||||
req.Header = authHeader(key)
|
||||
resp, err = httpClient().Do(req)
|
||||
if err != nil { t.Fatalf("mig tx do: %v", err) }
|
||||
if err != nil {
|
||||
t.Fatalf("mig tx do: %v", err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK { t.Fatalf("mig tx status: %d", resp.StatusCode) }
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("mig tx status: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
// verify schema type change
|
||||
qBody := fmt.Sprintf(`{"sql":"PRAGMA table_info(%s)"}`, users)
|
||||
req, _ = http.NewRequest(http.MethodPost, base+"/v1/db/query", strings.NewReader(qBody))
|
||||
req.Header = authHeader(key)
|
||||
resp, err = httpClient().Do(req)
|
||||
if err != nil { t.Fatalf("pragma do: %v", err) }
|
||||
if err != nil {
|
||||
t.Fatalf("pragma do: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK { t.Fatalf("pragma status: %d", resp.StatusCode) }
|
||||
var qr struct{ Columns []string `json:"columns"`; Rows [][]any `json:"rows"` }
|
||||
if err := json.NewDecoder(resp.Body).Decode(&qr); err != nil { t.Fatalf("pragma decode: %v", err) }
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("pragma status: %d", resp.StatusCode)
|
||||
}
|
||||
var qr struct {
|
||||
Columns []string `json:"columns"`
|
||||
Rows [][]any `json:"rows"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&qr); err != nil {
|
||||
t.Fatalf("pragma decode: %v", err)
|
||||
}
|
||||
// column order: cid,name,type,notnull,dflt_value,pk
|
||||
ageIsInt := false
|
||||
for _, row := range qr.Rows {
|
||||
if len(row) >= 3 && fmt.Sprintf("%v", row[1]) == "age" {
|
||||
tstr := strings.ToUpper(fmt.Sprintf("%v", row[2]))
|
||||
if strings.Contains(tstr, "INT") { ageIsInt = true; break }
|
||||
if strings.Contains(tstr, "INT") {
|
||||
ageIsInt = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !ageIsInt {
|
||||
@ -360,11 +378,19 @@ func TestGateway_Database_RecreateWithFK(t *testing.T) {
|
||||
req2, _ := http.NewRequest(http.MethodPost, base+"/v1/db/query", strings.NewReader(qBody2))
|
||||
req2.Header = authHeader(key)
|
||||
resp3, err := httpClient().Do(req2)
|
||||
if err != nil { t.Fatalf("sqlite_master do: %v", err) }
|
||||
if err != nil {
|
||||
t.Fatalf("sqlite_master do: %v", err)
|
||||
}
|
||||
defer resp3.Body.Close()
|
||||
if resp3.StatusCode != http.StatusOK { t.Fatalf("sqlite_master status: %d", resp3.StatusCode) }
|
||||
var qr2 struct{ Rows [][]any `json:"rows"` }
|
||||
if err := json.NewDecoder(resp3.Body).Decode(&qr2); err != nil { t.Fatalf("sqlite_master decode: %v", err) }
|
||||
if resp3.StatusCode != http.StatusOK {
|
||||
t.Fatalf("sqlite_master status: %d", resp3.StatusCode)
|
||||
}
|
||||
var qr2 struct {
|
||||
Rows [][]any `json:"rows"`
|
||||
}
|
||||
if err := json.NewDecoder(resp3.Body).Decode(&qr2); err != nil {
|
||||
t.Fatalf("sqlite_master decode: %v", err)
|
||||
}
|
||||
found := false
|
||||
for _, row := range qr2.Rows {
|
||||
if len(row) > 0 {
|
||||
@ -375,14 +401,22 @@ func TestGateway_Database_RecreateWithFK(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found { t.Fatalf("age column type not INTEGER after migration") }
|
||||
if !found {
|
||||
t.Fatalf("age column type not INTEGER after migration")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func toWSURL(httpURL string) string {
|
||||
u, err := url.Parse(httpURL)
|
||||
if err != nil { return httpURL }
|
||||
if u.Scheme == "https" { u.Scheme = "wss" } else { u.Scheme = "ws" }
|
||||
if err != nil {
|
||||
return httpURL
|
||||
}
|
||||
if u.Scheme == "https" {
|
||||
u.Scheme = "wss"
|
||||
} else {
|
||||
u.Scheme = "ws"
|
||||
}
|
||||
return u.String()
|
||||
}
|
||||
|
||||
|
@ -32,9 +32,6 @@ func main() {
|
||||
// Example: Database operations
|
||||
demonstrateDatabase(networkClient)
|
||||
|
||||
// Example: Storage operations
|
||||
demonstrateStorage(networkClient)
|
||||
|
||||
// Example: Pub/Sub messaging
|
||||
demonstratePubSub(networkClient)
|
||||
|
||||
@ -83,47 +80,6 @@ func demonstrateDatabase(client client.NetworkClient) {
|
||||
log.Printf("Query result: %+v", result)
|
||||
}
|
||||
|
||||
func demonstrateStorage(client client.NetworkClient) {
|
||||
ctx := context.Background()
|
||||
storage := client.Storage()
|
||||
|
||||
log.Printf("=== Storage Operations ===")
|
||||
|
||||
// Store some data
|
||||
key := "user:123"
|
||||
value := []byte(`{"name": "Alice", "age": 30}`)
|
||||
|
||||
if err := storage.Put(ctx, key, value); err != nil {
|
||||
log.Printf("Error storing data: %v", err)
|
||||
return
|
||||
}
|
||||
log.Printf("Data stored successfully")
|
||||
|
||||
// Retrieve data
|
||||
retrieved, err := storage.Get(ctx, key)
|
||||
if err != nil {
|
||||
log.Printf("Error retrieving data: %v", err)
|
||||
return
|
||||
}
|
||||
log.Printf("Retrieved data: %s", string(retrieved))
|
||||
|
||||
// Check if key exists
|
||||
exists, err := storage.Exists(ctx, key)
|
||||
if err != nil {
|
||||
log.Printf("Error checking existence: %v", err)
|
||||
return
|
||||
}
|
||||
log.Printf("Key exists: %v", exists)
|
||||
|
||||
// List keys
|
||||
keys, err := storage.List(ctx, "user:", 10)
|
||||
if err != nil {
|
||||
log.Printf("Error listing keys: %v", err)
|
||||
return
|
||||
}
|
||||
log.Printf("Keys: %v", keys)
|
||||
}
|
||||
|
||||
func demonstratePubSub(client client.NetworkClient) {
|
||||
ctx := context.Background()
|
||||
pubsub := client.PubSub()
|
||||
|
6555
network.md
Normal file
6555
network.md
Normal file
File diff suppressed because one or more lines are too long
@ -22,7 +22,6 @@ import (
|
||||
|
||||
"git.debros.io/DeBros/network/pkg/anyoneproxy"
|
||||
"git.debros.io/DeBros/network/pkg/pubsub"
|
||||
"git.debros.io/DeBros/network/pkg/storage"
|
||||
)
|
||||
|
||||
// Client implements the NetworkClient interface
|
||||
@ -36,7 +35,6 @@ type Client struct {
|
||||
|
||||
// Components
|
||||
database *DatabaseClientImpl
|
||||
storage *StorageClientImpl
|
||||
network *NetworkInfoImpl
|
||||
pubsub *pubSubBridge
|
||||
|
||||
@ -83,11 +81,6 @@ func (c *Client) Database() DatabaseClient {
|
||||
return c.database
|
||||
}
|
||||
|
||||
// Storage returns the storage client
|
||||
func (c *Client) Storage() StorageClient {
|
||||
return c.storage
|
||||
}
|
||||
|
||||
// PubSub returns the pub/sub client
|
||||
func (c *Client) PubSub() PubSubClient {
|
||||
return c.pubsub
|
||||
@ -202,16 +195,6 @@ func (c *Client) Connect() error {
|
||||
c.pubsub = &pubSubBridge{client: c, adapter: adapter}
|
||||
c.logger.Info("Pubsub bridge created successfully")
|
||||
|
||||
c.logger.Info("Creating storage client...")
|
||||
|
||||
// Create storage client with the host (use namespace directly to avoid deadlock)
|
||||
storageClient := storage.NewClient(h, namespace, c.logger)
|
||||
c.storage = &StorageClientImpl{
|
||||
client: c,
|
||||
storageClient: storageClient,
|
||||
}
|
||||
c.logger.Info("Storage client created successfully")
|
||||
|
||||
c.logger.Info("Starting bootstrap peer connections...")
|
||||
|
||||
// Connect to bootstrap peers FIRST
|
||||
@ -315,7 +298,6 @@ func (c *Client) Health() (*HealthStatus, error) {
|
||||
checks := map[string]string{
|
||||
"connection": "ok",
|
||||
"database": "ok",
|
||||
"storage": "ok",
|
||||
"pubsub": "ok",
|
||||
}
|
||||
|
||||
@ -360,11 +342,6 @@ func (c *Client) requireAccess(ctx context.Context) error {
|
||||
return fmt.Errorf("access denied: API key or JWT required")
|
||||
}
|
||||
ns := c.getAppNamespace()
|
||||
if v := ctx.Value(storage.CtxKeyNamespaceOverride); v != nil {
|
||||
if s, ok := v.(string); ok && s != "" && s != ns {
|
||||
return fmt.Errorf("access denied: namespace mismatch")
|
||||
}
|
||||
}
|
||||
if v := ctx.Value(pubsub.CtxKeyNamespaceOverride); v != nil {
|
||||
if s, ok := v.(string); ok && s != "" && s != ns {
|
||||
return fmt.Errorf("access denied: namespace mismatch")
|
||||
|
@ -7,7 +7,6 @@ import (
|
||||
"testing"
|
||||
|
||||
"git.debros.io/DeBros/network/pkg/pubsub"
|
||||
"git.debros.io/DeBros/network/pkg/storage"
|
||||
)
|
||||
|
||||
// MakeJWT creates a minimal JWT-like token with a json payload
|
||||
@ -146,10 +145,10 @@ func TestRequireAccess(t *testing.T) {
|
||||
// set resolved namespace to "app" to simulate derived namespace
|
||||
c.resolvedNamespace = "app"
|
||||
|
||||
// override storage namespace to something else
|
||||
ctx := storage.WithNamespace(context.Background(), "other")
|
||||
// override pubsub namespace to something else
|
||||
ctx := pubsub.WithNamespace(context.Background(), "other")
|
||||
if err := c.requireAccess(ctx); err == nil {
|
||||
t.Fatalf("expected namespace mismatch error for storage override")
|
||||
t.Fatalf("expected namespace mismatch error for pubsub override")
|
||||
}
|
||||
|
||||
// override pubsub namespace to something else
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
|
||||
"git.debros.io/DeBros/network/pkg/pubsub"
|
||||
"git.debros.io/DeBros/network/pkg/storage"
|
||||
)
|
||||
|
||||
// contextKey for internal operations
|
||||
@ -15,11 +14,10 @@ const (
|
||||
ctxKeyInternal contextKey = "internal_operation"
|
||||
)
|
||||
|
||||
// WithNamespace applies both storage and pubsub namespace overrides to the context.
|
||||
// It is a convenience helper for client callers to ensure both subsystems receive
|
||||
// WithNamespace applies pubsub namespace override to the context.
|
||||
// It is a convenience helper for client callers to ensure subsystems receive
|
||||
// the same, consistent namespace override.
|
||||
func WithNamespace(ctx context.Context, ns string) context.Context {
|
||||
ctx = storage.WithNamespace(ctx, ns)
|
||||
ctx = pubsub.WithNamespace(ctx, ns)
|
||||
return ctx
|
||||
}
|
||||
|
@ -7,8 +7,6 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.debros.io/DeBros/network/pkg/storage"
|
||||
|
||||
"git.debros.io/DeBros/network/pkg/anyoneproxy"
|
||||
"github.com/libp2p/go-libp2p/core/peer"
|
||||
"github.com/multiformats/go-multiaddr"
|
||||
@ -400,87 +398,6 @@ func (d *DatabaseClientImpl) GetSchema(ctx context.Context) (*SchemaInfo, error)
|
||||
return schema, nil
|
||||
}
|
||||
|
||||
// StorageClientImpl implements StorageClient using distributed storage
|
||||
type StorageClientImpl struct {
|
||||
client *Client
|
||||
storageClient *storage.Client
|
||||
}
|
||||
|
||||
// Get retrieves a value by key
|
||||
func (s *StorageClientImpl) Get(ctx context.Context, key string) ([]byte, error) {
|
||||
if !s.client.isConnected() {
|
||||
return nil, fmt.Errorf("client not connected")
|
||||
}
|
||||
|
||||
if err := s.client.requireAccess(ctx); err != nil {
|
||||
return nil, fmt.Errorf("authentication required: %w - run CLI commands to authenticate automatically", err)
|
||||
}
|
||||
|
||||
return s.storageClient.Get(ctx, key)
|
||||
}
|
||||
|
||||
// Put stores a value by key
|
||||
func (s *StorageClientImpl) Put(ctx context.Context, key string, value []byte) error {
|
||||
if !s.client.isConnected() {
|
||||
return fmt.Errorf("client not connected")
|
||||
}
|
||||
|
||||
if err := s.client.requireAccess(ctx); err != nil {
|
||||
return fmt.Errorf("authentication required: %w - run CLI commands to authenticate automatically", err)
|
||||
}
|
||||
|
||||
err := s.storageClient.Put(ctx, key, value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete removes a key
|
||||
func (s *StorageClientImpl) Delete(ctx context.Context, key string) error {
|
||||
if !s.client.isConnected() {
|
||||
return fmt.Errorf("client not connected")
|
||||
}
|
||||
|
||||
if err := s.client.requireAccess(ctx); err != nil {
|
||||
return fmt.Errorf("authentication required: %w - run CLI commands to authenticate automatically", err)
|
||||
}
|
||||
|
||||
err := s.storageClient.Delete(ctx, key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// List returns keys with a given prefix
|
||||
func (s *StorageClientImpl) List(ctx context.Context, prefix string, limit int) ([]string, error) {
|
||||
if !s.client.isConnected() {
|
||||
return nil, fmt.Errorf("client not connected")
|
||||
}
|
||||
|
||||
if err := s.client.requireAccess(ctx); err != nil {
|
||||
return nil, fmt.Errorf("authentication required: %w - run CLI commands to authenticate automatically", err)
|
||||
}
|
||||
|
||||
return s.storageClient.List(ctx, prefix, limit)
|
||||
}
|
||||
|
||||
// Exists checks if a key exists
|
||||
func (s *StorageClientImpl) Exists(ctx context.Context, key string) (bool, error) {
|
||||
if !s.client.isConnected() {
|
||||
return false, fmt.Errorf("client not connected")
|
||||
}
|
||||
|
||||
if err := s.client.requireAccess(ctx); err != nil {
|
||||
return false, fmt.Errorf("authentication required: %w - run CLI commands to authenticate automatically", err)
|
||||
}
|
||||
|
||||
return s.storageClient.Exists(ctx, key)
|
||||
}
|
||||
|
||||
// NetworkInfoImpl implements NetworkInfo
|
||||
type NetworkInfoImpl struct {
|
||||
client *Client
|
||||
|
@ -11,9 +11,6 @@ type NetworkClient interface {
|
||||
// Database operations (namespaced per app)
|
||||
Database() DatabaseClient
|
||||
|
||||
// Key-value storage (namespaced per app)
|
||||
Storage() StorageClient
|
||||
|
||||
// Pub/Sub messaging
|
||||
PubSub() PubSubClient
|
||||
|
||||
@ -38,15 +35,6 @@ type DatabaseClient interface {
|
||||
GetSchema(ctx context.Context) (*SchemaInfo, error)
|
||||
}
|
||||
|
||||
// StorageClient provides key-value storage operations
|
||||
type StorageClient interface {
|
||||
Get(ctx context.Context, key string) ([]byte, error)
|
||||
Put(ctx context.Context, key string, value []byte) error
|
||||
Delete(ctx context.Context, key string) error
|
||||
List(ctx context.Context, prefix string, limit int) ([]string, error)
|
||||
Exists(ctx context.Context, key string) (bool, error)
|
||||
}
|
||||
|
||||
// PubSubClient provides publish/subscribe messaging
|
||||
type PubSubClient interface {
|
||||
Subscribe(ctx context.Context, topic string, handler MessageHandler) error
|
||||
|
@ -109,7 +109,7 @@ func DefaultConfig() *Config {
|
||||
},
|
||||
Discovery: DiscoveryConfig{
|
||||
BootstrapPeers: []string{
|
||||
"/ip4/127.0.0.1/tcp/4001/p2p/12D3KooWGqqR8bxgmYsYrGYMKnUWwZUCpioLmA3H37ggRDnAiFa7",
|
||||
"/ip4/127.0.0.1/tcp/4001/p2p/12D3KooWSHHwEY6cga3ng7tD1rzStAU58ogQXVMX3LZJ6Gqf6dee",
|
||||
},
|
||||
BootstrapPort: 4001, // Default LibP2P port
|
||||
DiscoveryInterval: time.Second * 15, // Back to 15 seconds for testing
|
||||
|
@ -31,14 +31,6 @@ func (g *Gateway) Routes() http.Handler {
|
||||
mux.HandleFunc("/v1/apps", g.appsHandler)
|
||||
mux.HandleFunc("/v1/apps/", g.appsHandler)
|
||||
|
||||
// storage
|
||||
mux.HandleFunc("/v1/storage", g.storageHandler) // legacy/basic
|
||||
mux.HandleFunc("/v1/storage/get", g.storageGetHandler)
|
||||
mux.HandleFunc("/v1/storage/put", g.storagePutHandler)
|
||||
mux.HandleFunc("/v1/storage/delete", g.storageDeleteHandler)
|
||||
mux.HandleFunc("/v1/storage/list", g.storageListHandler)
|
||||
mux.HandleFunc("/v1/storage/exists", g.storageExistsHandler)
|
||||
|
||||
// database
|
||||
mux.HandleFunc("/v1/db/query", g.dbQueryHandler)
|
||||
mux.HandleFunc("/v1/db/transaction", g.dbTransactionHandler)
|
||||
|
@ -2,118 +2,118 @@ package gateway
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"git.debros.io/DeBros/network/pkg/client"
|
||||
"git.debros.io/DeBros/network/pkg/storage"
|
||||
"git.debros.io/DeBros/network/pkg/pubsub"
|
||||
)
|
||||
|
||||
// Database HTTP handlers
|
||||
func (g *Gateway) dbQueryHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if g.client == nil { writeError(w, http.StatusServiceUnavailable, "client not initialized"); return }
|
||||
if r.Method != http.MethodPost { writeError(w, http.StatusMethodNotAllowed, "method not allowed"); return }
|
||||
if g.client == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "client not initialized");
|
||||
return
|
||||
}
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, http.StatusMethodNotAllowed, "method not allowed");
|
||||
return
|
||||
}
|
||||
var body struct{ SQL string `json:"sql"`; Args []any `json:"args"` }
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil || body.SQL == "" { writeError(w, http.StatusBadRequest, "invalid body: {sql, args?}"); return }
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil || body.SQL == "" {
|
||||
writeError(w, http.StatusBadRequest, "invalid body: {sql, args?}");
|
||||
return
|
||||
}
|
||||
ctx := client.WithInternalAuth(r.Context())
|
||||
res, err := g.client.Database().Query(ctx, body.SQL, body.Args...)
|
||||
if err != nil { writeError(w, http.StatusInternalServerError, err.Error()); return }
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error());
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, res)
|
||||
}
|
||||
|
||||
func (g *Gateway) dbTransactionHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if g.client == nil { writeError(w, http.StatusServiceUnavailable, "client not initialized"); return }
|
||||
if r.Method != http.MethodPost { writeError(w, http.StatusMethodNotAllowed, "method not allowed"); return }
|
||||
if g.client == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "client not initialized");
|
||||
return
|
||||
}
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, http.StatusMethodNotAllowed, "method not allowed");
|
||||
return
|
||||
}
|
||||
var body struct{ Statements []string `json:"statements"` }
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil || len(body.Statements) == 0 { writeError(w, http.StatusBadRequest, "invalid body: {statements:[...]}"); return }
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil || len(body.Statements) == 0 {
|
||||
writeError(w, http.StatusBadRequest, "invalid body: {statements:[...]}");
|
||||
return
|
||||
}
|
||||
ctx := client.WithInternalAuth(r.Context())
|
||||
if err := g.client.Database().Transaction(ctx, body.Statements); err != nil { writeError(w, http.StatusInternalServerError, err.Error()); return }
|
||||
if err := g.client.Database().Transaction(ctx, body.Statements); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error());
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"status":"ok"})
|
||||
}
|
||||
|
||||
func (g *Gateway) dbSchemaHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if g.client == nil { writeError(w, http.StatusServiceUnavailable, "client not initialized"); return }
|
||||
if r.Method != http.MethodGet { writeError(w, http.StatusMethodNotAllowed, "method not allowed"); return }
|
||||
if g.client == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "client not initialized");
|
||||
return
|
||||
}
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, http.StatusMethodNotAllowed, "method not allowed");
|
||||
return
|
||||
}
|
||||
ctx := client.WithInternalAuth(r.Context())
|
||||
schema, err := g.client.Database().GetSchema(ctx)
|
||||
if err != nil { writeError(w, http.StatusInternalServerError, err.Error()); return }
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error());
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, schema)
|
||||
}
|
||||
|
||||
func (g *Gateway) dbCreateTableHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if g.client == nil { writeError(w, http.StatusServiceUnavailable, "client not initialized"); return }
|
||||
if r.Method != http.MethodPost { writeError(w, http.StatusMethodNotAllowed, "method not allowed"); return }
|
||||
if g.client == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "client not initialized");
|
||||
return
|
||||
}
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, http.StatusMethodNotAllowed, "method not allowed");
|
||||
return
|
||||
}
|
||||
var body struct{ Schema string `json:"schema"` }
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil || body.Schema == "" { writeError(w, http.StatusBadRequest, "invalid body: {schema}"); return }
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil || body.Schema == "" {
|
||||
writeError(w, http.StatusBadRequest, "invalid body: {schema}");
|
||||
return
|
||||
}
|
||||
ctx := client.WithInternalAuth(r.Context())
|
||||
if err := g.client.Database().CreateTable(ctx, body.Schema); err != nil { writeError(w, http.StatusInternalServerError, err.Error()); return }
|
||||
if err := g.client.Database().CreateTable(ctx, body.Schema); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error());
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, map[string]any{"status":"ok"})
|
||||
}
|
||||
|
||||
func (g *Gateway) dbDropTableHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if g.client == nil { writeError(w, http.StatusServiceUnavailable, "client not initialized"); return }
|
||||
if r.Method != http.MethodPost { writeError(w, http.StatusMethodNotAllowed, "method not allowed"); return }
|
||||
var body struct{ Table string `json:"table"` }
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil || body.Table == "" { writeError(w, http.StatusBadRequest, "invalid body: {table}"); return }
|
||||
ctx := client.WithInternalAuth(r.Context())
|
||||
if err := g.client.Database().DropTable(ctx, body.Table); err != nil { writeError(w, http.StatusInternalServerError, err.Error()); return }
|
||||
writeJSON(w, http.StatusOK, map[string]any{"status":"ok"})
|
||||
}
|
||||
|
||||
func (g *Gateway) storageHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if g.client == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "client not initialized")
|
||||
writeError(w, http.StatusServiceUnavailable, "client not initialized");
|
||||
return
|
||||
}
|
||||
|
||||
key := r.URL.Query().Get("key")
|
||||
if key == "" {
|
||||
writeError(w, http.StatusBadRequest, "missing 'key' query parameter")
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, http.StatusMethodNotAllowed, "method not allowed");
|
||||
return
|
||||
}
|
||||
var body struct{ Table string `json:"table"` }
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil || body.Table == "" {
|
||||
writeError(w, http.StatusBadRequest, "invalid body: {table}");
|
||||
return
|
||||
}
|
||||
|
||||
// Use internal auth for downstream client calls; gateway has already authenticated the request
|
||||
ctx := client.WithInternalAuth(r.Context())
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
val, err := g.client.Storage().Get(ctx, key)
|
||||
if err != nil {
|
||||
// Some storage backends may return base64-encoded text; try best-effort decode for transparency
|
||||
writeError(w, http.StatusNotFound, err.Error())
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(val)
|
||||
return
|
||||
|
||||
case http.MethodPut:
|
||||
defer r.Body.Close()
|
||||
b, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "failed to read body")
|
||||
return
|
||||
}
|
||||
if err := g.client.Storage().Put(ctx, key, b); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, map[string]any{
|
||||
"status": "ok",
|
||||
"key": key,
|
||||
"size": len(b),
|
||||
})
|
||||
return
|
||||
|
||||
case http.MethodOptions:
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
default:
|
||||
writeError(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||
if err := g.client.Database().DropTable(ctx, body.Table); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error());
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"status":"ok"})
|
||||
}
|
||||
|
||||
func (g *Gateway) networkStatusHandler(w http.ResponseWriter, r *http.Request) {
|
||||
@ -144,134 +144,6 @@ func (g *Gateway) networkPeersHandler(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, peers)
|
||||
}
|
||||
|
||||
func (g *Gateway) storageGetHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if g.client == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "client not initialized")
|
||||
return
|
||||
}
|
||||
key := r.URL.Query().Get("key")
|
||||
if key == "" {
|
||||
writeError(w, http.StatusBadRequest, "missing 'key'")
|
||||
return
|
||||
}
|
||||
if !g.validateNamespaceParam(r) {
|
||||
writeError(w, http.StatusForbidden, "namespace mismatch")
|
||||
return
|
||||
}
|
||||
val, err := g.client.Storage().Get(client.WithInternalAuth(r.Context()), key)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusNotFound, err.Error())
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(val)
|
||||
}
|
||||
|
||||
func (g *Gateway) storagePutHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if g.client == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "client not initialized")
|
||||
return
|
||||
}
|
||||
key := r.URL.Query().Get("key")
|
||||
if key == "" {
|
||||
writeError(w, http.StatusBadRequest, "missing 'key'")
|
||||
return
|
||||
}
|
||||
if !g.validateNamespaceParam(r) {
|
||||
writeError(w, http.StatusForbidden, "namespace mismatch")
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
b, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "failed to read body")
|
||||
return
|
||||
}
|
||||
if err := g.client.Storage().Put(client.WithInternalAuth(r.Context()), key, b); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, map[string]any{"status": "ok", "key": key, "size": len(b)})
|
||||
}
|
||||
|
||||
func (g *Gateway) storageDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if g.client == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "client not initialized")
|
||||
return
|
||||
}
|
||||
if !g.validateNamespaceParam(r) {
|
||||
writeError(w, http.StatusForbidden, "namespace mismatch")
|
||||
return
|
||||
}
|
||||
key := r.URL.Query().Get("key")
|
||||
if key == "" {
|
||||
var body struct {
|
||||
Key string `json:"key"`
|
||||
Namespace string `json:"namespace"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err == nil {
|
||||
key = body.Key
|
||||
}
|
||||
}
|
||||
if key == "" {
|
||||
writeError(w, http.StatusBadRequest, "missing 'key'")
|
||||
return
|
||||
}
|
||||
if err := g.client.Storage().Delete(client.WithInternalAuth(r.Context()), key); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"status": "ok", "key": key})
|
||||
}
|
||||
|
||||
func (g *Gateway) storageListHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if g.client == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "client not initialized")
|
||||
return
|
||||
}
|
||||
if !g.validateNamespaceParam(r) {
|
||||
writeError(w, http.StatusForbidden, "namespace mismatch")
|
||||
return
|
||||
}
|
||||
prefix := r.URL.Query().Get("prefix")
|
||||
limitStr := r.URL.Query().Get("limit")
|
||||
limit := 100
|
||||
if limitStr != "" {
|
||||
if n, err := strconv.Atoi(limitStr); err == nil && n > 0 {
|
||||
limit = n
|
||||
}
|
||||
}
|
||||
keys, err := g.client.Storage().List(client.WithInternalAuth(r.Context()), prefix, limit)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"keys": keys})
|
||||
}
|
||||
|
||||
func (g *Gateway) storageExistsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if g.client == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "client not initialized")
|
||||
return
|
||||
}
|
||||
if !g.validateNamespaceParam(r) {
|
||||
writeError(w, http.StatusForbidden, "namespace mismatch")
|
||||
return
|
||||
}
|
||||
key := r.URL.Query().Get("key")
|
||||
if key == "" {
|
||||
writeError(w, http.StatusBadRequest, "missing 'key'")
|
||||
return
|
||||
}
|
||||
exists, err := g.client.Storage().Exists(client.WithInternalAuth(r.Context()), key)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"exists": exists})
|
||||
}
|
||||
|
||||
func (g *Gateway) networkConnectHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if g.client == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "client not initialized")
|
||||
@ -323,7 +195,7 @@ func (g *Gateway) validateNamespaceParam(r *http.Request) bool {
|
||||
if qns == "" {
|
||||
return true
|
||||
}
|
||||
if v := r.Context().Value(storage.CtxKeyNamespaceOverride); v != nil {
|
||||
if v := r.Context().Value(pubsub.CtxKeyNamespaceOverride); v != nil {
|
||||
if s, ok := v.(string); ok && s != "" {
|
||||
return s == qns
|
||||
}
|
||||
|
@ -25,7 +25,6 @@ import (
|
||||
"git.debros.io/DeBros/network/pkg/config"
|
||||
"git.debros.io/DeBros/network/pkg/database"
|
||||
"git.debros.io/DeBros/network/pkg/logging"
|
||||
"git.debros.io/DeBros/network/pkg/storage"
|
||||
)
|
||||
|
||||
// Node represents a network node with RQLite database
|
||||
@ -36,7 +35,6 @@ type Node struct {
|
||||
|
||||
rqliteManager *database.RQLiteManager
|
||||
rqliteAdapter *database.RQLiteAdapter
|
||||
storageService *storage.Service
|
||||
|
||||
// Peer discovery
|
||||
discoveryCancel context.CancelFunc
|
||||
@ -377,24 +375,6 @@ func (n *Node) startLibP2P() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// startStorageService initializes the storage service
|
||||
func (n *Node) startStorageService() error {
|
||||
n.logger.ComponentInfo(logging.ComponentStorage, "Starting storage service")
|
||||
|
||||
// Create storage service using the RQLite SQL adapter
|
||||
service, err := storage.NewService(n.rqliteAdapter.GetSQLDB(), n.logger.Logger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
n.storageService = service
|
||||
|
||||
// Set up stream handler for storage protocol
|
||||
n.host.SetStreamHandler("/network/storage/1.0.0", n.storageService.HandleStorageStream)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// loadOrCreateIdentity loads an existing identity or creates a new one
|
||||
func (n *Node) loadOrCreateIdentity() (crypto.PrivKey, error) {
|
||||
identityFile := filepath.Join(n.config.Node.DataDir, "identity.key")
|
||||
@ -631,11 +611,6 @@ func (n *Node) Stop() error {
|
||||
// Stop peer discovery
|
||||
n.stopPeerDiscovery()
|
||||
|
||||
// Stop storage service
|
||||
if n.storageService != nil {
|
||||
n.storageService.Close()
|
||||
}
|
||||
|
||||
// Stop LibP2P host
|
||||
if n.host != nil {
|
||||
n.host.Close()
|
||||
@ -672,11 +647,6 @@ func (n *Node) Start(ctx context.Context) error {
|
||||
return fmt.Errorf("failed to start LibP2P: %w", err)
|
||||
}
|
||||
|
||||
// Start storage service
|
||||
if err := n.startStorageService(); err != nil {
|
||||
return fmt.Errorf("failed to start storage service: %w", err)
|
||||
}
|
||||
|
||||
// Get listen addresses for logging
|
||||
var listenAddrs []string
|
||||
for _, addr := range n.host.Addrs() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user