Browse Source
Stabalizing api code
Stabalizing api code
* Added logging * Moved api routing into the api module * Added method stubs for getting address, updating address, and updating address manually.master
Drew Short
6 years ago
6 changed files with 138 additions and 8 deletions
-
54Cargo.lock
-
4Cargo.toml
-
25src/main.rs
-
5src/router.rs
-
56src/server/api.rs
-
2src/server/mod.rs
@ -1,13 +1,16 @@ |
|||
extern crate actix_web;
|
|||
|
|||
use actix_web::middleware::Logger;
|
|||
use actix_web::{http, App};
|
|||
|
|||
use server;
|
|||
|
|||
pub fn create() -> App {
|
|||
actix_web::App::new()
|
|||
.resource("/", |r| r.f(server::index))
|
|||
.middleware(Logger::default())
|
|||
.scope("api/", |api_scope| server::api::route(api_scope))
|
|||
.resource("/health", |r| {
|
|||
r.method(http::Method::GET).with(server::healthcheck)
|
|||
})
|
|||
.resource("/", |r| r.f(server::index))
|
|||
}
|
@ -0,0 +1,56 @@ |
|||
use actix_web::http::Method;
|
|||
use actix_web::http::StatusCode;
|
|||
use actix_web::{AsyncResponder, FutureResponse, HttpMessage, HttpRequest, HttpResponse, Scope};
|
|||
use bytes::{Buf, Bytes, IntoBuf};
|
|||
use futures::future::Future;
|
|||
use std::io::Read;
|
|||
|
|||
pub fn route(scope: Scope<()>) -> Scope<()> {
|
|||
scope.nested("{root}/{zone}", |zone_scope| {
|
|||
zone_scope
|
|||
.resource("", |r| r.method(Method::GET).f(get_address))
|
|||
.resource("update", |r| {
|
|||
r.method(Method::GET).f(update_address_automatically);
|
|||
r.method(Method::POST).f(update_address_manually)
|
|||
})
|
|||
})
|
|||
}
|
|||
|
|||
fn update_address(address: String) -> String {
|
|||
info!("Updating Address {}", address);
|
|||
address
|
|||
}
|
|||
|
|||
fn get_address(req: &HttpRequest) -> HttpResponse {
|
|||
match req.connection_info().remote() {
|
|||
Some(addr) => HttpResponse::build(StatusCode::OK)
|
|||
.content_type("text/plain")
|
|||
.body(format!("{}", addr)),
|
|||
None => HttpResponse::build(StatusCode::BAD_REQUEST).finish(),
|
|||
}
|
|||
}
|
|||
|
|||
fn update_address_automatically(req: &HttpRequest) -> HttpResponse {
|
|||
match req.connection_info().remote() {
|
|||
Some(addr) => HttpResponse::build(StatusCode::OK)
|
|||
.content_type("text/plain")
|
|||
.body(format!("{}", addr)),
|
|||
None => HttpResponse::build(StatusCode::BAD_REQUEST).finish(),
|
|||
}
|
|||
}
|
|||
|
|||
fn update_address_manually(req: &HttpRequest) -> FutureResponse<HttpResponse> {
|
|||
req.body()
|
|||
.limit(48)
|
|||
.from_err()
|
|||
.and_then(|bytes: Bytes| {
|
|||
let mut buffer = String::new();
|
|||
match bytes.into_buf().reader().read_to_string(&mut buffer) {
|
|||
Ok(_) => Ok(HttpResponse::Ok()
|
|||
.content_type("text/plain")
|
|||
.body(update_address(buffer))),
|
|||
Err(_) => Ok(HttpResponse::build(StatusCode::BAD_REQUEST).finish()),
|
|||
}
|
|||
})
|
|||
.responder()
|
|||
}
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue