aboutsummaryrefslogtreecommitdiff
path: root/src/api/mod.rs
diff options
context:
space:
mode:
authorFuwn <[email protected]>2021-05-10 15:50:51 +0000
committerFuwn <[email protected]>2021-05-10 15:55:30 +0000
commit0a97100e9c2c550ae4141142cb06b05804b4c2e7 (patch)
tree073f1eb758b311372d90470fe304f3d923844c98 /src/api/mod.rs
parentMerge pull request #16 from Whirlsplash/renovate/commitizen-4.x (diff)
downloadwhirl-0a97100e9c2c550ae4141142cb06b05804b4c2e7.tar.xz
whirl-0a97100e9c2c550ae4141142cb06b05804b4c2e7.zip
perf(api): use actix_web instead of rocket for api
Refactors API for Actix, also enables CORS. BREAKING CHANGE: `whirlsplash.port.api` configuration key created. closes #20
Diffstat (limited to 'src/api/mod.rs')
-rw-r--r--src/api/mod.rs27
1 files changed, 22 insertions, 5 deletions
diff --git a/src/api/mod.rs b/src/api/mod.rs
index 17ffea4..c744f3d 100644
--- a/src/api/mod.rs
+++ b/src/api/mod.rs
@@ -1,14 +1,31 @@
// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective
// SPDX-License-Identifier: GPL-3.0-only
+use actix_web::web::resource;
+
mod routes;
pub struct Api;
impl Api {
- pub fn listen() {
- let _ = rocket::ignite()
- .mount("/", routes![routes::index])
- .mount("/api/v1", routes![routes::stats::statistics])
- .launch();
+ pub async fn listen(
+ tx: std::sync::mpsc::Sender<actix_web::dev::Server>,
+ address: &str,
+ ) -> std::io::Result<()> {
+ let mut sys = actix_web::rt::System::new("api");
+
+ let server = actix_web::HttpServer::new(|| {
+ actix_web::App::new()
+ .wrap(actix_cors::Cors::default().allow_any_origin())
+ .service(resource("/").to(|| async { "Whirlsplash" }))
+ .service(resource("/api/v1/statistics").to(routes::stats::statistics))
+ })
+ .bind(address)?
+ .run();
+
+ info!("http api now listening at {}", address);
+
+ let _ = tx.send(server.clone());
+
+ sys.block_on(server)
}
}