Use App::configure to set up actix

This avoids the need for the messy cache-control-header macro.
Otherwise, it has no effect.
This commit is contained in:
Dustin J. Mitchell
2021-10-09 17:53:36 -04:00
parent f109056340
commit aaac1c3356
7 changed files with 63 additions and 40 deletions

View File

@@ -1,21 +1,10 @@
#![deny(clippy::all)]
use actix_web::{middleware, middleware::Logger, App, HttpServer};
use actix_web::{middleware::Logger, App, HttpServer};
use clap::Arg;
use taskchampion_sync_server::storage::SqliteStorage;
use taskchampion_sync_server::Server;
// The `.wrap` method returns an opaque type, meaning that we can't easily return it from
// functions. So, we must apply these default headers when the app is created, which occurs both
// in `main` and in the tests. To check that those are both doing precisely the same thing, we use
// a macro. This is ugly, and will go away when actix-web is no longer the framework in use.
macro_rules! cache_control_headers {
($wrapped:expr) => {
$wrapped
.wrap(middleware::DefaultHeaders::new().header("Cache-Control", "no-store, max-age=0"))
};
}
#[actix_web::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();
@@ -51,9 +40,9 @@ async fn main() -> anyhow::Result<()> {
log::warn!("Serving on port {}", port);
HttpServer::new(move || {
cache_control_headers!(App::new())
App::new()
.wrap(Logger::default())
.service(server.service())
.configure(|cfg| server.config(cfg))
})
.bind(format!("0.0.0.0:{}", port))?
.run()
@@ -65,21 +54,16 @@ async fn main() -> anyhow::Result<()> {
mod test {
use super::*;
use actix_web::{test, App};
use pretty_assertions::assert_eq;
use taskchampion_sync_server::storage::InMemoryStorage;
#[actix_rt::test]
async fn test_index_get() {
let server = Server::new(Box::new(InMemoryStorage::new()));
let app = cache_control_headers!(App::new()).service(server.service());
let app = App::new().configure(|sc| server.config(sc));
let mut app = test::init_service(app).await;
let req = test::TestRequest::get().uri("/").to_request();
let resp = test::call_service(&mut app, req).await;
assert!(resp.status().is_success());
assert_eq!(
resp.headers().get("Cache-Control").unwrap(),
&"no-store, max-age=0".to_string()
)
}
}