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. 22
      src/server/router.rs

2
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);

9
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<AppState>) -> Scope<AppState> {
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<IpAddr, APIError> {
}
}
fn get_address(req: &HttpRequest) -> HttpResponse {
fn get_address(req: &HttpRequest<AppState>) -> 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<AppState>) -> 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<HttpResponse> {
fn update_address_manually(req: &HttpRequest<AppState>) -> FutureResponse<HttpResponse> {
req.body()
.limit(48)
.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 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<AppState>) -> &'static str {
"Hello, World!"
}
pub fn healthcheck(_req: HttpRequest) -> Result<Json<Health>> {
pub fn healthcheck(_req: HttpRequest<AppState>) -> Result<Json<Health>> {
Ok(Json(Health { version: VERSION }))
}

22
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<Config>) -> impl Fn() -> App + Send + Clone {
|| {
actix_web::App::new()
.middleware(Logger::default())
.scope("api/", server::api::route)
pub struct AppState {
config: Arc<Config>
}
pub fn create(config: Arc<Config>) -> App<AppState> {
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))
}
Loading…
Cancel
Save