|
| 1 | +///| |
| 2 | +/// Assemble the base middleware onion — CORS, request-id, tracing, structured |
| 3 | +/// logging, request logging, and recovery — over a two-route app, then drive a |
| 4 | +/// normal request (watching the injected response headers and the JSON access log) |
| 5 | +/// and one that raises (watching recovery answer 500 instead of crashing). |
| 6 | +/// |
| 7 | +/// moon run --target native examples/02-middleware |
| 8 | +#coverage.skip |
| 9 | +async fn main { |
| 10 | + let app = @moonapi.App::new() |
| 11 | + let api = @moonzero.Group::new(app, "/api/v1") |
| 12 | + api.get("/ping", _ctx => @moonapi.text(200, "pong")) |
| 13 | + api.get("/boom", _ctx => raise Boom) |
| 14 | + |
| 15 | + // An incrementing clock so the access log shows a real elapsed duration. |
| 16 | + let tick : Ref[Int64] = { val: 0L } |
| 17 | + let clock = @moonzero.Clock::new(() => { |
| 18 | + let now = tick.val |
| 19 | + tick.val = now + 5L |
| 20 | + now |
| 21 | + }) |
| 22 | + let conf = @moonzero.ServiceConf::new( |
| 23 | + name="greet", |
| 24 | + host="127.0.0.1", |
| 25 | + port=8888, |
| 26 | + ) |
| 27 | + let onion = @moonzero.Server::new(conf, app) |
| 28 | + .use_(@moonzero.cors(@moonzero.CorsConf::new())) |
| 29 | + .use_(@moonzero.request_id()) |
| 30 | + .use_(@moonzero.tracing()) |
| 31 | + .use_(@moonzero.structured_logging(clock)) |
| 32 | + .use_(@moonzero.logging) |
| 33 | + .use_(@moonzero.recovery) |
| 34 | + println(onion.describe()) |
| 35 | + let handler = onion.to_asgi() |
| 36 | + |
| 37 | + // A normal request: 200, with the middleware's response headers stamped on. |
| 38 | + let (status, headers, body) = drive(handler, "GET", "/api/v1/ping", []) |
| 39 | + println("ping -> " + status.to_string() + " body=" + text(body)) |
| 40 | + println(" x-request-id=" + hget(headers, "x-request-id")) |
| 41 | + println(" x-trace-id=" + hget(headers, "x-trace-id")) |
| 42 | + println(" traceparent=" + hget(headers, "traceparent")) |
| 43 | + println( |
| 44 | + " access-control-allow-origin=" + |
| 45 | + hget(headers, "access-control-allow-origin"), |
| 46 | + ) |
| 47 | + |
| 48 | + // A client-supplied request id is reused verbatim rather than minted. |
| 49 | + let (_, reused, _) = drive(handler, "GET", "/api/v1/ping", [ |
| 50 | + ("x-request-id", "heke1228"), |
| 51 | + ]) |
| 52 | + println("supplied request id reused: " + hget(reused, "x-request-id")) |
| 53 | + |
| 54 | + // A handler that raises: recovery answers 500 instead of letting it escape. |
| 55 | + let (boom_status, _, boom_body) = drive(handler, "GET", "/api/v1/boom", []) |
| 56 | + println("boom -> " + boom_status.to_string() + " body=" + text(boom_body)) |
| 57 | + |
| 58 | + // The structured access-log record on its own, rendered as one JSON line. |
| 59 | + let entry = @moonzero.RequestLog::{ |
| 60 | + http_method: "GET", |
| 61 | + path: "/api/v1/ping", |
| 62 | + status: 200, |
| 63 | + duration_ms: 5L, |
| 64 | + request_id: "req-1", |
| 65 | + client_ip: "127.0.0.1", |
| 66 | + user_agent: "lfanke", |
| 67 | + } |
| 68 | + println("RequestLog: " + entry.render()) |
| 69 | +} |
| 70 | + |
| 71 | +///| |
| 72 | +/// A local error a route raises to exercise recovery. |
| 73 | +suberror Boom |
| 74 | + |
| 75 | +///| |
| 76 | +/// Feed one request through an assembled ASGI onion in-process, capturing the |
| 77 | +/// response status, the `HttpResponseStart` headers, and the body. |
| 78 | +async fn drive( |
| 79 | + handler : @moonasgi.AsgiApp, |
| 80 | + verb : String, |
| 81 | + path : String, |
| 82 | + headers : Array[(String, String)], |
| 83 | +) -> (Int, Array[(String, String)], Bytes) { |
| 84 | + let scope = @moonasgi.Scope::Http( |
| 85 | + @moonasgi.HttpScope::new(http_method=verb, path~, headers~), |
| 86 | + ) |
| 87 | + let sent : Ref[Bool] = { val: false } |
| 88 | + let receive : @moonasgi.Receive = () => { |
| 89 | + if sent.val { |
| 90 | + @moonasgi.Event::HttpDisconnect |
| 91 | + } else { |
| 92 | + sent.val = true |
| 93 | + @moonasgi.Event::HttpRequest(body=b"", more_body=false) |
| 94 | + } |
| 95 | + } |
| 96 | + let status : Ref[Int] = { val: 0 } |
| 97 | + let out : Ref[Array[(String, String)]] = { val: [] } |
| 98 | + let body = Buffer() |
| 99 | + let sink : @moonasgi.Send = event => { |
| 100 | + match event { |
| 101 | + HttpResponseStart(status=s, headers=h, ..) => { |
| 102 | + status.val = s |
| 103 | + out.val = h |
| 104 | + } |
| 105 | + HttpResponseBody(body=b, ..) => body.write_bytes(b) |
| 106 | + _ => () |
| 107 | + } |
| 108 | + } |
| 109 | + handler(scope, receive, sink) |
| 110 | + (status.val, out.val, body.to_bytes()) |
| 111 | +} |
| 112 | + |
| 113 | +///| |
| 114 | +fn hget(headers : Array[(String, String)], name : String) -> String { |
| 115 | + for pair in headers { |
| 116 | + if pair.0 == name { |
| 117 | + return pair.1 |
| 118 | + } |
| 119 | + } |
| 120 | + "(none)" |
| 121 | +} |
| 122 | + |
| 123 | +///| |
| 124 | +fn text(b : Bytes) -> String { |
| 125 | + let sb = StringBuilder::new() |
| 126 | + for i = 0; i < b.length(); i = i + 1 { |
| 127 | + sb.write_char(b[i].to_int().unsafe_to_char()) |
| 128 | + } |
| 129 | + sb.to_string() |
| 130 | +} |
0 commit comments