Import std.net.http.routes
HttpRouter
extend
Extend HttpRouter
Routes from a Class's Methods, by their attributes.
std/net/http/routes.fb, line 46
-
Adds a route for each Async Method of
instance's type with a @Get, @Post, @Put, @Patch, or @Delete, or several, which gives an HttpResponse and takes nothing, the request, one Struct or Class, or that and the request:@Get("/users/:id") Async Method Show(args: ShowUser): HttpResponse. Each call's Struct or Class is made by Load from an Object of its Fields' values: a path parameter or, failing that, a query parameter, whose name is the Field's, or its @Rename's, or else its @Renamed's old one, whatever the letters' case, as text, which Load reads as a number or a Bool where the Field is one; a @Flatten Field's own Fields, as Load reads them; and for a @JsonBody Field, the body's JSON. A Field the request doesn't give keeps its default, or zero, and one with neither, or a value that doesn't fit, or a body that isn't JSON, is a 400 that says why. Methods without one of those attributes are left alone, and so are Static Methods and those with a ByRef parameter, which no For Method loop goes over. Panics as Get does for a path, or a route with its paths already, and for a Method of up to four arguments with one that isn't a route's shape: one that isn't Async, throws, gives something else, takes the request first, or takes what Load doesn't make from an Object, like a number or an HttpRequest?.Examples
Struct ShowUser Field id: Int End Struct Class Users @Get("/users/:id") Public Async Method Show(request: ShowUser): HttpResponse Return HttpResponse.Of(Body.Text("user {request.id}")) End Method End Class Local server = HttpServer() server.Routes(Users()) Try server.Listen("127.0.0.1", 8080) // Serve answers requests, GET /users/7 with "user 7", until Stop. server.Serve()
Report an issue with this page or with FutureBlitz on GitHub.