Section III - executable
Since the SDKs contain all the modules we will need for our web service, we can go right to writing our executable: src/bin/reporting.rs
.
We start by declaring our dependent external crates
extern crate actix_web;
We then declare the modules we will be using.
use actix_web::{web, App, HttpRequest, HttpServer, HttpResponse};
use actix_web::http::{StatusCode};
use actix_web::middleware::Logger;
We will be referencing a global variable, so we will include that next.
static ALL_PRODUCTS: &str = "all";
We can now add our supportive functions.
Just like we did when building the Provisioning Microservice, we will first build the
wrapper
for the service and confirm it works before including our business logic.
async fn index(req: HttpRequest) -> HttpResponse {
let product = req.match_info().get("product").unwrap_or(ALL_PRODUCTS);
let content = match &product {
&"all" => {
ALL_PRODUCTS.to_string()
},
_ => {
product.to_string()
},
};
HttpResponse::build(StatusCode::OK)
.body(&content)
}
Lastly, we write the main
function that will be executed.
#[actix_web::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
HttpServer::new(|| {
App::new()
.wrap(Logger::default())
.wrap(Logger::new("%a %{User-Agent}i"))
.route("/{product}", web::get().to(index))
})
.bind("localhost:8001")?
.run()
.await
}
When we are finished, our reporting
file should look like the following:
extern crate actix_web;
use actix_web::{web, App, HttpRequest, HttpServer, HttpResponse};
use actix_web::http::{StatusCode};
use actix_web::middleware::Logger;
static ALL_PRODUCTS: &str = "all";
async fn index(req: HttpRequest) -> HttpResponse {
let product = req.match_info().get("product").unwrap_or(ALL_PRODUCTS);
let content = match &product {
&"all" => {
ALL_PRODUCTS.to_string()
},
_ => {
product.to_string()
},
};
HttpResponse::build(StatusCode::OK)
.body(&content)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
HttpServer::new(|| {
App::new()
.wrap(Logger::default())
.wrap(Logger::new("%a %{User-Agent}i"))
.route("/{product}", web::get().to(index))
})
.bind("localhost:8001")?
.run()
.await
}
Last updated
Was this helpful?