You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.9 KiB
56 lines
1.9 KiB
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()
|
|
}
|