REST API
A complete REST API: 5 endpoints (list/create/get/update/delete), middleware chain (logging + CORS), and nyx-kv as the persistence layer. This is the production pattern for all JSON APIs on nyxlang.com.
Code
// Full REST API — CRUD with nyx-serve + nyx-kv
import "std/http"
import "std/web"
fn resp_cmd(parts: Array) -> String {
var sb: StringBuilder = StringBuilder.new()
sb.append("*" + int_to_string(parts.length()) + "\r\n")
var i: int = 0
while i < parts.length() {
let p: String = parts[i]
sb.append("$" + int_to_string(p.length()) + "\r\n" + p + "\r\n")
i = i + 1
}
return sb.to_string()
}
fn kv_hset(key: String, field: String, val: String) -> int {
let fd: int = tcp_connect("127.0.0.1", 6380)
if fd < 0 { return -1 }
tcp_write(fd, resp_cmd(["HSET", key, field, val]))
tcp_read_line(fd)
tcp_close(fd)
return 0
}
fn list_users(req: Request) -> Response {
return response_json(200, "[{\"id\": 1, \"name\": \"Alice\"}]")
}
fn create_user(req: Request) -> Response {
// Parse JSON body (simplified — use std/json in real code)
let name: String = "from_body"
kv_hset("user:1", "name", name)
return response_json(201, "{\"id\": 1, \"created\": true}")
}
fn get_user(req: Request) -> Response {
let id: String = req.params.get("id")
return response_json(200, "{\"id\": " + id + "}")
}
fn update_user(req: Request) -> Response {
let id: String = req.params.get("id")
kv_hset("user:" + id, "updated_at", int_to_string(time()))
return response_json(200, "{\"updated\": true}")
}
fn delete_user(req: Request) -> Response {
return response_new(204, "")
}
fn main() -> int {
let app: App = app_new()
app_use(app, mw_logging)
app_use(app, mw_cors)
// RESTful CRUD
app_get(app, "/api/users", list_users)
app_post(app, "/api/users", create_user)
app_get(app, "/api/users/{id}", get_user)
app_put(app, "/api/users/{id}", update_user)
app_delete(app, "/api/users/{id}", delete_user)
print("REST API with 5 endpoints + middleware + nyx-kv backend")
return 0
}
Output
REST API with 5 endpoints + middleware + nyx-kv backend
Explanation
This is the template every Nyx web service follows. nyx-serve exposes the familiar app_get / app_post / app_put / app_delete verbs, URL patterns with {id} capture, and a middleware chain via app_use. The handlers return Response values (not side effects), which keeps them testable. Persistence is a TCP hop to nyx-kv on port 6380 — a Hash for each entity, so you get O(1) lookups without running a separate database. Swap in nyx-db if you need SQL queries.