diff --git a/src/main.rs b/src/main.rs index a3c4ddf..1cea0b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -71,7 +71,7 @@ fn main() { ); let shared_config = Arc::new(config); - let actix_server = actix_web::server::new(server::router::factory(shared_config)) + let actix_server = actix_web::server::new(move || server::router::create(shared_config.clone())) .workers(workers) .bind(bind); diff --git a/src/server/api.rs b/src/server/api.rs index d8ebb7e..532a8f4 100644 --- a/src/server/api.rs +++ b/src/server/api.rs @@ -9,8 +9,9 @@ use bytes::{Buf, Bytes, IntoBuf}; use futures::future::Future; use crate::server::error::APIError; +use crate::server::router::AppState; -pub fn route(scope: Scope<()>) -> Scope<()> { +pub fn route(scope: Scope) -> Scope { scope.resource("address", |r| r.method(Method::GET).f(get_address)) .nested("{root}/{zone}", |zone_scope| { zone_scope @@ -44,7 +45,7 @@ fn parse_remote_info(remote_info: &str) -> Result { } } -fn get_address(req: &HttpRequest) -> HttpResponse { +fn get_address(req: &HttpRequest) -> HttpResponse { match req.connection_info().remote() { Some(remote_info) => { match parse_remote_info(remote_info) { @@ -61,7 +62,7 @@ fn get_address(req: &HttpRequest) -> HttpResponse { } } -fn update_address_automatically(req: &HttpRequest) -> HttpResponse { +fn update_address_automatically(req: &HttpRequest) -> HttpResponse { match req.connection_info().remote() { Some(remote_info) => { match parse_remote_info(remote_info) { @@ -78,7 +79,7 @@ fn update_address_automatically(req: &HttpRequest) -> HttpResponse { } } -fn update_address_manually(req: &HttpRequest) -> FutureResponse { +fn update_address_manually(req: &HttpRequest) -> FutureResponse { req.body() .limit(48) .from_err() diff --git a/src/server/middleware.rs b/src/server/middleware/api_auth.rs similarity index 100% rename from src/server/middleware.rs rename to src/server/middleware/api_auth.rs diff --git a/src/server/middleware/mod.rs b/src/server/middleware/mod.rs new file mode 100644 index 0000000..11ecfd5 --- /dev/null +++ b/src/server/middleware/mod.rs @@ -0,0 +1 @@ +pub mod api_auth; \ No newline at end of file diff --git a/src/server/mod.rs b/src/server/mod.rs index cac1605..ca8303b 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -1,5 +1,6 @@ use actix_web::{HttpRequest, Json, Result}; +use crate::server::router::AppState; use crate::VERSION; pub mod api; @@ -11,10 +12,10 @@ pub struct Health { version: &'static str, } -pub fn index(_req: &HttpRequest) -> &'static str { +pub fn index(_req: &HttpRequest) -> &'static str { "Hello, World!" } -pub fn healthcheck(_req: HttpRequest) -> Result> { +pub fn healthcheck(_req: HttpRequest) -> Result> { Ok(Json(Health { version: VERSION })) } diff --git a/src/server/router.rs b/src/server/router.rs index 3df1b96..ec9cf43 100644 --- a/src/server/router.rs +++ b/src/server/router.rs @@ -8,15 +8,17 @@ use actix_web::middleware::Logger; use crate::config::model::Config; use crate::server; -pub fn factory(config: Arc) -> impl Fn() -> App + Send + Clone { - || { - actix_web::App::new() - .middleware(Logger::default()) - .scope("api/", server::api::route) +pub struct AppState { + config: Arc +} + +pub fn create(config: Arc) -> App { + actix_web::App::with_state(AppState { config }) + .middleware(Logger::default()) + .scope("api/", server::api::route) // .middleware() - .resource("/health", |r| { - r.method(http::Method::GET).with(server::healthcheck) - }) - .resource("/", |r| r.f(server::index)) - } + .resource("/health", |r| { + r.method(http::Method::GET).with(server::healthcheck) + }) + .resource("/", |r| r.f(server::index)) }