Drew Short 6 years ago
parent
commit
076c11535c
  1. 2
      src/main.rs
  2. 9
      src/server/api.rs
  3. 0
      src/server/middleware/api_auth.rs
  4. 1
      src/server/middleware/mod.rs
  5. 5
      src/server/mod.rs
  6. 10
      src/server/router.rs

2
src/main.rs

@ -71,7 +71,7 @@ fn main() {
); );
let shared_config = Arc::new(config); 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) .workers(workers)
.bind(bind); .bind(bind);

9
src/server/api.rs

@ -9,8 +9,9 @@ use bytes::{Buf, Bytes, IntoBuf};
use futures::future::Future; use futures::future::Future;
use crate::server::error::APIError; use crate::server::error::APIError;
use crate::server::router::AppState;
pub fn route(scope: Scope<()>) -> Scope<()> {
pub fn route(scope: Scope<AppState>) -> Scope<AppState> {
scope.resource("address", |r| r.method(Method::GET).f(get_address)) scope.resource("address", |r| r.method(Method::GET).f(get_address))
.nested("{root}/{zone}", |zone_scope| { .nested("{root}/{zone}", |zone_scope| {
zone_scope zone_scope
@ -44,7 +45,7 @@ fn parse_remote_info(remote_info: &str) -> Result<IpAddr, APIError> {
} }
} }
fn get_address(req: &HttpRequest) -> HttpResponse {
fn get_address(req: &HttpRequest<AppState>) -> HttpResponse {
match req.connection_info().remote() { match req.connection_info().remote() {
Some(remote_info) => { Some(remote_info) => {
match parse_remote_info(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<AppState>) -> HttpResponse {
match req.connection_info().remote() { match req.connection_info().remote() {
Some(remote_info) => { Some(remote_info) => {
match parse_remote_info(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<HttpResponse> {
fn update_address_manually(req: &HttpRequest<AppState>) -> FutureResponse<HttpResponse> {
req.body() req.body()
.limit(48) .limit(48)
.from_err() .from_err()

0
src/server/middleware.rs → src/server/middleware/api_auth.rs

1
src/server/middleware/mod.rs

@ -0,0 +1 @@
pub mod api_auth;

5
src/server/mod.rs

@ -1,5 +1,6 @@
use actix_web::{HttpRequest, Json, Result}; use actix_web::{HttpRequest, Json, Result};
use crate::server::router::AppState;
use crate::VERSION; use crate::VERSION;
pub mod api; pub mod api;
@ -11,10 +12,10 @@ pub struct Health {
version: &'static str, version: &'static str,
} }
pub fn index(_req: &HttpRequest) -> &'static str {
pub fn index(_req: &HttpRequest<AppState>) -> &'static str {
"Hello, World!" "Hello, World!"
} }
pub fn healthcheck(_req: HttpRequest) -> Result<Json<Health>> {
pub fn healthcheck(_req: HttpRequest<AppState>) -> Result<Json<Health>> {
Ok(Json(Health { version: VERSION })) Ok(Json(Health { version: VERSION }))
} }

10
src/server/router.rs

@ -8,9 +8,12 @@ use actix_web::middleware::Logger;
use crate::config::model::Config; use crate::config::model::Config;
use crate::server; use crate::server;
pub fn factory(config: Arc<Config>) -> impl Fn() -> App + Send + Clone {
|| {
actix_web::App::new()
pub struct AppState {
config: Arc<Config>
}
pub fn create(config: Arc<Config>) -> App<AppState> {
actix_web::App::with_state(AppState { config })
.middleware(Logger::default()) .middleware(Logger::default())
.scope("api/", server::api::route) .scope("api/", server::api::route)
// .middleware() // .middleware()
@ -19,4 +22,3 @@ pub fn factory(config: Arc<Config>) -> impl Fn() -> App + Send + Clone {
}) })
.resource("/", |r| r.f(server::index)) .resource("/", |r| r.f(server::index))
} }
}
Loading…
Cancel
Save