Warning: Currently in the experimental phase, everything can change.
The goal of YAHF
is both to provide a good developer experience and to be easy to extend.
Following the author's vision, those are the project rules: It runs on stable rust; Serialization and Deserialization internally dealt with; No new macros;
YAHF
follows theSemVer
.
The objective for v1.0.0 is to have a stable project that can deal with real-world problems with good developer experience and the possibility to extend the project to suit any need.
The goals for this version are:
The default code structure will look something like this
Any function that follows some of these signatures will be considered an handler:
rust
async fn handler_name(req: Request<ReqBodyType>) -> Response<ResBodyType> {/*Some code*/}
async fn handler_name(req: ReqBodyType) -> Response<ResBodyType> {/*Some code*/}
async fn handler_name() -> Response<ResBodyType> {/*Some code*/}
async fn handler_name(req: Request<ReqBodyType>) -> ResBodyType {/*Some code*/}
async fn handler_name(req: ReqBodyType) -> ResBodyType {/*Some code*/}
async fn handler_name() -> ResBodyType {/*Some code*/}
There will be two structures that will be used to setup routes, one is the Server
and the other is the Router
.
Both will follow the same pattern to register a new route
in them, but only Server
will be able to start listening for requests.
Adding routes will be:
rust
// Registering an handler for a route and a method
router.<method>("/path/goes/here", handler, RequestBodyDeserializer, ResponseBodySerializer);
Here both Deserializer
and Serializer
are structs with zero data. For more details look into the #2.
Router
is the way to compose the structure, as it will allow to merge with other routers, this includes the Server
. This is an example of what would be the usage of a Router
:
```rust let somerouter: Router = Router::new(); somerouter.all('/user', /Some handler/, &(), &Json);
let anotherrouter: Router = Router::new(); anotherrouter.all('/function', /Some handler/, &(), &String);
let updatedserver: Server = server.router(somerouter)?.router(another_router)?; ```
There is one more thing, Router
as we'll see it later, will be the way to apply a middleware to an set of routes.
Server
is the core structure of an application as everything around is needed to set the Server
up. Mainly, after setting up, running the application it's simple as:
rust
server.listen('/*The IpAddress to bind and start listen*/')
It'll be supported two types of middleware functions: PreMiddleware
and AfterMiddleware
Apply transformations to Request
:
```rust
async fn some_middleware(request: Result
// When building a Router
or a Server
let router = router.pre(some_middleware);
```
Apply transformations to Response
:
```rust
async fn someaftermiddleware(response: Result
// When building a Router
or a Server
let router = router.after(some_middleware);
```