e7738de6d2
CI / Deep Native Runtime Cases (1/6) (push) Has been skipped
CI / Native Preflight (push) Failing after 1s
CI / Native Runtime Cases (1/2) (push) Failing after 0s
CI / Native Runtime Cases (2/2) (push) Failing after 1s
CI / Native Metadata Reports (push) Failing after 0s
CI / Native Direct Backend Artifacts (push) Failing after 0s
CI / Native Sanitizer Smoke (push) Failing after 1s
CI / Command Contract Snapshots (push) Failing after 1s
CI / Deep Conformance Suite (push) Has been skipped
CI / Graph Build Perf (push) Failing after 1s
CI / Deep Native Preflight (push) Has been skipped
CI / Deep Native Runtime Cases (2/6) (push) Has been skipped
CI / Deep Native Runtime Cases (3/6) (push) Has been skipped
CI / Conformance Suite (push) Failing after 1s
CI / Workspace Checks (push) Failing after 0s
CI / Deep Native Runtime Cases (5/6) (push) Has been skipped
CI / Deep Native Runtime Cases (6/6) (push) Has been skipped
CI / Deep Native Runtime Cases (4/6) (push) Has been skipped
CI / Deep Graph Build Perf (push) Has been skipped
57 lines
2.7 KiB
Plaintext
57 lines
2.7 KiB
Plaintext
fn handle(request: Span<u8>, response: MutSpan<u8>) -> Maybe<Span<u8>> {
|
|
if std.http.requestIsOptions(request, "/users") {
|
|
return std.http.writeCorsPreflight(response, "*", "GET, POST, OPTIONS", "content-type, authorization")
|
|
}
|
|
if std.http.requestIsGet(request, "/health") {
|
|
return std.http.writeJsonOk(response, "{\"ok\":true}")
|
|
}
|
|
let segment_count: usize = std.http.requestPathSegmentCount(request)
|
|
let first: Maybe<Span<u8>> = std.http.requestPathSegment(request, 0)
|
|
let second: Maybe<Span<u8>> = std.http.requestPathSegment(request, 1)
|
|
if std.http.requestMethodIs(request, "DELETE") && segment_count == 2 && first.has && second.has && std.mem.eql(first.value, "users") {
|
|
return std.http.writeResponse(response, 204_u16, "")
|
|
}
|
|
if std.http.requestIsPost(request, "/users") {
|
|
let tenant: Maybe<Span<u8>> = std.http.requestQueryValue(request, "tenant")
|
|
let body: Maybe<Span<u8>> = std.http.requestJsonBodyWithin(request, 64)
|
|
var id: Maybe<u32> = null
|
|
if body.has {
|
|
id = std.json.u32(body.value, "id")
|
|
}
|
|
if tenant.has && std.mem.eql(tenant.value, "demo") && body.has && id.has {
|
|
return std.http.writeJsonCreated(response, "{\"created\":true}")
|
|
}
|
|
return std.http.writeJsonBadRequest(response, "{\"error\":\"invalid\"}")
|
|
}
|
|
if segment_count == 2 && first.has && second.has && std.mem.eql(first.value, "users") {
|
|
return std.http.writeJsonNotFound(response, "{\"error\":\"not_found\",\"resource\":\"user\"}")
|
|
}
|
|
return std.http.writeJsonNotFound(response, "{\"error\":\"not_found\"}")
|
|
}
|
|
|
|
pub fn main(world: World) -> Void raises {
|
|
let maybe_request: Maybe<String> = std.args.get(1)
|
|
if !maybe_request.has {
|
|
check world.err.write("usage: pass HTTP request envelope\n")
|
|
return
|
|
}
|
|
var response: [256]u8 = [0_u8; 256]
|
|
let output: Maybe<Span<u8>> = handle(std.mem.span(maybe_request.value), response)
|
|
if !output.has {
|
|
check world.err.write("json api router failed\n")
|
|
return
|
|
}
|
|
let content_type: HttpHeaderValue = std.http.headerValue(output.value, "content-type")
|
|
let header: Maybe<Span<u8>> = std.http.headerBytes(output.value, content_type)
|
|
let body: Maybe<Span<u8>> = std.http.responseBodyBytes(output.value)
|
|
if header.has && std.mem.eql(header.value, "application/json") && std.str.contains(output.value, "201 Created") && body.has && std.mem.eql(body.value, "{\"created\":true}") {
|
|
check world.out.write("json api router ok\n")
|
|
return
|
|
}
|
|
if std.str.contains(output.value, "204 No Content") {
|
|
check world.out.write("json api router ok\n")
|
|
return
|
|
}
|
|
check world.err.write("json api router failed\n")
|
|
}
|