Crate afire[−][src]
Expand description
π₯ afire
A blazing fast web framework for Rust
π Install
Just add the following to your Cargo.toml
:
[dependencies]
afire = "0.1.3"
π Info
This is kinda like express.js for rust. It is not that complicated but it still makes development of apis / servers much easier. It supports Middleware and comes with some built in for Logging and Rate limiting.
For more information on this lib check the docs here
π₯ Examples
Make a simple server:
// Import Lib
use afire::*;
// Create Server
let mut server: Server = Server::new("localhost", 8080);
// Add a route
server.route(Method::GET, "/", |_req| {
Response::new(
200,
"Hello World",
vec![Header::new("Content-Type", "text/plain")],
)
});
// Start the server
// This is blocking
server.start();
You can add as many routes as you want. The most recently defined route will always take priority. So you can make a 404 page like this:
// Import Library
use afire::{Server, Response, Header, Method};
// Create Server
let mut server: Server = Server::new("localhost", 8080);
// Define 404 page
// Because this is defined first, it will take a low priority
server.all(|req| {
Response::new(
404,
"The page you are looking for does not exist :/",
vec![Header::new("Content-Type", "text/plain")],
)
});
// Define a route
// As this is defined last, it will take a high priority
server.route(Method::GET, "/hello", |req| {
Response::new(
200,
"Hello World!",
vec![Header::new("Content-Type", "text/plain")],
)
});
// Starts the server
// This is blocking
server.start();
π¦ Middleware
afire comes with some builtin extensions in the form of middleware.
β’ βοΈ Rate-limit
This will use the client ip to limit the amount of requests that will be processed. You can configure the request limit and the reset period.
// Import Stuff
use afire::{Server, RateLimiter};
// Make server
let mut server: Server = Server::new("localhost", 8080);
// Enable Rate Limiting
// This will limit the requests per ip to 5 every 10 sec
RateLimiter::attach(&mut server, 5, 10);
β’ π Logger
This will log all requests to a file or stdout or bolth. You can pick a log level that will determine if headers and body will be logged.
// Import Stuff
use afire::{Server, Logger, Level};
// Make server again
let mut server: Server = Server::new("localhost", 8080);
// Enable Logger
// Level::Debug has headers and body
// Level::Info does not
Logger::attach(
&mut server,
Logger::new(Level::Debug, Some("log.log"), true),
);
Structs
Http header
Logger
Struct for holding query data
Limit the amount of requests handled by the server.
Http Request
Http Response
Defines a route.
Defines a server.