serverino-router ~main
Router and params handling
To use this package, run the following command in your project's root directory:
Manual usage
Put the following dependency into your project's dependences section:
Serverino Routing and Params helpers
Add serverino-router to your project by running,
dub add serverino-router
Routing
Helper functions available to map the URL pattern to the handler function. Make sure these route details are available for workers.
@onWorkerStart void handleWorkerStart()
{
postRoute!"/api/v1/folders"(&createFolderHandler);
putRoute!"/api/v1/folders/:id:long"(&editFolderHandler);
getRoute!"/api/v1/folders"(&listFoldersHandler);
getRoute!"/api/v1/folders/:id:long"(&getFolderHandler);
deleteRoute!"/api/v1/folders/:id:long"(&deleteFolderHandler);
// ...
}
URL params are accessible as pathParams. For example, pathParams["id"] (string type but validated for the specified type). URL params are also available for parseParams function (See next section).
Params
Helper functions to parse all types of params and convert into D struct/class. Similar to Ruby on Rails, all the params will be available in one place.
Example:
struct LoginRequest
{
string username;
string password;
string authenticityToken;
}
void loginHandler(Request request, Output output)
{
auto params = parseParams!LoginRequest(request);
// Use params.username, params.password and
// params.authenticityToken as needed.
}
This function checks the params in the following order. If the Content type is set to application/json then parses the body JSON.
- Path params - Path params are only available if the routing helper from this library.
- Query Params - Get params from Query.
- JSON Params - Try to get params from JSON body.
- Post params
- Multipart formdata
TODO: Support for parsing files is still not available. Use request.form to read the files data.
Use @paramName UDA if the param name is different from the struct field name. For example,
struct ArticleRequest
{
string title;
string content;
@paramName("author_id") long authorId;
}
Use @ignoreParam to not try parsing that param. It may be a derived column or complex type and parsing may fail for that type. For example,
struct DocumentRequest
{
string name;
string content;
@ignoreParam ubyte[] contentBinary;
}
auto params = parseParams!DocumentRequest(request);
// ..Process the params
params.contentBinary = content.representation;
// ...
Why?
Handling all params from one place has its own advantageous. A few of them are listed below.
- Easily support multiple ways to send data(JSON or Formdata).
- Easy to switch web framework without changing all the handlers.
- Discards the extra/unwanted fields and parses only whatever requested by the struct.
- Simplifies the validations and params handling in Controllers(or Handlers).
Other utilities
Start Time.
When the router is used, it sets the startTime and uses it to print duration in the debug build. Use nginx logging to log the request and response details. If Auth or any other endpoint defined with higher priority, make sure to set the Start time by calling setStartTime function.
@endpoint @priority(1000)
void sessionStartAndAuthHandler(Request request, Output output)
{
setStartTime;
// ... other logic
}
Set Content Type
A utility function to set the content type.
output.setContentType("application/json");
Write JSON body
@endpoint @route!"/ping"
void pingHandler(Request request, Output output)
{
output.writeJsonBody(["ok": true]);
}
Or with Status code,
auto note = Note.create(...);
output.writeJsonBody(note, HttpStatus.created);
- ~main released 4 months ago
- aravindavk/serverino-router
- MIT
- Copyright © 2024, Aravinda VK <[email protected]>
- Authors:
- Dependencies:
- serverino, http-status-codes, json-serialization
- Versions:
-
Show all 2 versions0.1.0 2025-Jul-27 ~main 2025-Jul-27 - Download Stats:
-
-
0 downloads today
-
0 downloads this week
-
0 downloads this month
-
0 downloads total
-
- Score:
- 0.5
- Short URL:
- serverino-router.dub.pm