2024-05-19 23:52:59 +02:00
|
|
|
use std::sync::OnceLock;
|
|
|
|
|
2021-12-07 16:31:15 +01:00
|
|
|
use actix_http::HttpService;
|
|
|
|
use actix_server::Server;
|
|
|
|
use actix_service::map_config;
|
2024-08-18 15:33:28 +02:00
|
|
|
use actix_web::{dev::AppConfig, get, App, Responder};
|
2021-12-07 16:31:15 +01:00
|
|
|
|
2024-05-20 00:06:05 +02:00
|
|
|
static MEDIUM: OnceLock<String> = OnceLock::new();
|
2024-05-19 23:52:59 +02:00
|
|
|
static LARGE: OnceLock<String> = OnceLock::new();
|
|
|
|
|
2021-12-07 16:31:15 +01:00
|
|
|
#[get("/")]
|
2024-08-18 15:33:28 +02:00
|
|
|
async fn index() -> impl Responder {
|
2021-12-07 16:31:15 +01:00
|
|
|
"Hello, world. From Actix Web!"
|
|
|
|
}
|
|
|
|
|
2024-05-19 23:52:59 +02:00
|
|
|
#[get("/large")]
|
|
|
|
async fn large() -> &'static str {
|
2024-05-20 00:06:05 +02:00
|
|
|
LARGE.get_or_init(|| "123456890".repeat(1024 * 100))
|
2024-05-19 23:52:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/medium")]
|
|
|
|
async fn medium() -> &'static str {
|
2024-05-20 00:06:05 +02:00
|
|
|
MEDIUM.get_or_init(|| "123456890".repeat(1024 * 5))
|
2024-05-19 23:52:59 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 16:31:15 +01:00
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
|
|
|
Server::build()
|
|
|
|
.bind("hello-world", "127.0.0.1:8080", || {
|
|
|
|
// construct actix-web app
|
2024-05-19 23:52:59 +02:00
|
|
|
let app = App::new().service(index).service(large).service(medium);
|
2021-12-07 16:31:15 +01:00
|
|
|
|
|
|
|
HttpService::build()
|
|
|
|
// pass the app to service builder
|
|
|
|
// map_config is used to map App's configuration to ServiceBuilder
|
2022-03-06 00:43:31 +01:00
|
|
|
// h1 will configure server to only use HTTP/1.1
|
|
|
|
.h1(map_config(app, |_| AppConfig::default()))
|
2021-12-07 16:31:15 +01:00
|
|
|
.tcp()
|
|
|
|
})?
|
|
|
|
.run()
|
|
|
|
.await
|
|
|
|
}
|