updated the current codebase to actix-web 1.0

This commit is contained in:
Gonçalo Valério 2019-06-30 00:25:33 +01:00
parent 6811ba8715
commit 4173a1a6de
6 changed files with 439 additions and 478 deletions

843
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,8 @@ version = "0.1.0"
authors = ["Gonçalo Valério <gon@ovalerio.net>"]
[dependencies]
actix-web = "0.7.18"
actix = "0.8.3"
actix-web = "1.0.2"
diesel = { version = "1.4.2", features = ["sqlite"] }
toml = "0.5.0"
clap = "2.32.0"

View File

@ -1,8 +1,3 @@
pub fn create_subscription() {
pub fn create_subscription() {}
}
pub fn remove_subscription() {
}
pub fn remove_subscription() {}

View File

@ -1,5 +1,5 @@
use actions::{create_subscription, remove_subscription};
use actix_web::{http, HttpRequest, HttpResponse};
use actix_web::{http, web, HttpRequest, HttpResponse};
use askama::Template;
use url::form_urlencoded;
use utils::{validate_parsed_data, AppState};
@ -8,14 +8,14 @@ use utils::{validate_parsed_data, AppState};
#[template(path = "index.html")]
struct IndexView;
pub fn index(_req: HttpRequest<AppState>) -> HttpResponse {
pub fn index(state: web::Data<AppState>, _req: HttpRequest) -> HttpResponse {
HttpResponse::Ok()
.content_type("text/html")
.body(IndexView.render().unwrap())
}
pub fn hub(_req: HttpRequest<AppState>, params: String) -> HttpResponse {
let log = &_req.state().log;
pub fn hub(state: web::Data<AppState>, _req: HttpRequest, params: String) -> HttpResponse {
let log = &state.log;
info!(log, "Received Request");
debug!(log, "Content: {}", params);
let parsed_data = form_urlencoded::parse(params.as_bytes());
@ -32,8 +32,8 @@ pub fn hub(_req: HttpRequest<AppState>, params: String) -> HttpResponse {
#[cfg(test)]
mod tests {
use super::*;
use actix_web::actix::{SyncArbiter, System};
use actix_web::{http, test};
use actix::{SyncArbiter, System};
use actix_web::{http, test, web};
use diesel::prelude::*;
use utils::{setup_logging, DbExecutor};
@ -44,13 +44,12 @@ mod tests {
DbExecutor(SqliteConnection::establish("test.db").unwrap())
});
let resp = index(
test::TestRequest::with_state(AppState {
log: setup_logging(),
db: addr.clone(),
})
.finish(),
);
let data = web::Data::new(AppState {
log: setup_logging(),
db: addr.clone(),
});
let resp = index(data, test::TestRequest::get().to_http_request());
assert_eq!(resp.status(), http::StatusCode::OK);
}
@ -61,11 +60,16 @@ mod tests {
DbExecutor(SqliteConnection::establish("test.db").unwrap())
});
let resp = hub(test::TestRequest::with_state(AppState {
let data = web::Data::new(AppState {
log: setup_logging(),
db: addr.clone(),
})
.finish());
assert_eq!(resp.status(), http::StatusCode::OK);
});
let resp = hub(
data,
test::TestRequest::post().to_http_request(),
"key=value".to_string(),
);
assert_eq!(resp.status(), http::StatusCode::BAD_REQUEST);
}
}

View File

@ -1,3 +1,4 @@
extern crate actix;
extern crate actix_web;
extern crate askama;
extern crate clap;
@ -8,8 +9,8 @@ extern crate slog;
extern crate slog_async;
extern crate slog_term;
extern crate url;
use actix_web::actix::{SyncArbiter, System};
use actix_web::{http, server, App};
use actix::{SyncArbiter, System};
use actix_web::{web, App, HttpServer};
use clap::Arg;
use controllers::{hub, index};
use diesel::prelude::*;
@ -53,14 +54,17 @@ fn main() {
DbExecutor(SqliteConnection::establish("local.db").unwrap())
});
let app_data = web::Data::new(AppState {
log: setup_logging(),
db: addr.clone(),
});
info!(log, "Starting server");
server::new(move || {
App::with_state(AppState {
log: setup_logging(),
db: addr.clone(),
})
.route("/", http::Method::GET, index)
.route("/", http::Method::POST, hub)
HttpServer::new(move || {
App::new()
.register_data(app_data.clone())
.route("/", web::get().to(index))
.route("/", web::post().to(hub))
})
.bind(format!("{}:{}", address, port))
.unwrap()

View File

@ -1,4 +1,4 @@
use actix_web::actix::{Actor, Addr, SyncContext};
use actix::{Actor, Addr, SyncContext};
use diesel::prelude::*;
use slog::Drain;
use url::form_urlencoded::Parse;