updated the current codebase to actix-web 1.0
This commit is contained in:
parent
6811ba8715
commit
4173a1a6de
File diff suppressed because it is too large
Load Diff
|
@ -4,7 +4,8 @@ version = "0.1.0"
|
||||||
authors = ["Gonçalo Valério <gon@ovalerio.net>"]
|
authors = ["Gonçalo Valério <gon@ovalerio.net>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "0.7.18"
|
actix = "0.8.3"
|
||||||
|
actix-web = "1.0.2"
|
||||||
diesel = { version = "1.4.2", features = ["sqlite"] }
|
diesel = { version = "1.4.2", features = ["sqlite"] }
|
||||||
toml = "0.5.0"
|
toml = "0.5.0"
|
||||||
clap = "2.32.0"
|
clap = "2.32.0"
|
||||||
|
|
|
@ -1,8 +1,3 @@
|
||||||
pub fn create_subscription() {
|
pub fn create_subscription() {}
|
||||||
|
|
||||||
}
|
pub fn remove_subscription() {}
|
||||||
|
|
||||||
|
|
||||||
pub fn remove_subscription() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use actions::{create_subscription, remove_subscription};
|
use actions::{create_subscription, remove_subscription};
|
||||||
use actix_web::{http, HttpRequest, HttpResponse};
|
use actix_web::{http, web, HttpRequest, HttpResponse};
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
use url::form_urlencoded;
|
use url::form_urlencoded;
|
||||||
use utils::{validate_parsed_data, AppState};
|
use utils::{validate_parsed_data, AppState};
|
||||||
|
@ -8,14 +8,14 @@ use utils::{validate_parsed_data, AppState};
|
||||||
#[template(path = "index.html")]
|
#[template(path = "index.html")]
|
||||||
struct IndexView;
|
struct IndexView;
|
||||||
|
|
||||||
pub fn index(_req: HttpRequest<AppState>) -> HttpResponse {
|
pub fn index(state: web::Data<AppState>, _req: HttpRequest) -> HttpResponse {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.content_type("text/html")
|
.content_type("text/html")
|
||||||
.body(IndexView.render().unwrap())
|
.body(IndexView.render().unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hub(_req: HttpRequest<AppState>, params: String) -> HttpResponse {
|
pub fn hub(state: web::Data<AppState>, _req: HttpRequest, params: String) -> HttpResponse {
|
||||||
let log = &_req.state().log;
|
let log = &state.log;
|
||||||
info!(log, "Received Request");
|
info!(log, "Received Request");
|
||||||
debug!(log, "Content: {}", params);
|
debug!(log, "Content: {}", params);
|
||||||
let parsed_data = form_urlencoded::parse(params.as_bytes());
|
let parsed_data = form_urlencoded::parse(params.as_bytes());
|
||||||
|
@ -32,8 +32,8 @@ pub fn hub(_req: HttpRequest<AppState>, params: String) -> HttpResponse {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use actix_web::actix::{SyncArbiter, System};
|
use actix::{SyncArbiter, System};
|
||||||
use actix_web::{http, test};
|
use actix_web::{http, test, web};
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use utils::{setup_logging, DbExecutor};
|
use utils::{setup_logging, DbExecutor};
|
||||||
|
|
||||||
|
@ -44,13 +44,12 @@ mod tests {
|
||||||
DbExecutor(SqliteConnection::establish("test.db").unwrap())
|
DbExecutor(SqliteConnection::establish("test.db").unwrap())
|
||||||
});
|
});
|
||||||
|
|
||||||
let resp = index(
|
let data = web::Data::new(AppState {
|
||||||
test::TestRequest::with_state(AppState {
|
|
||||||
log: setup_logging(),
|
log: setup_logging(),
|
||||||
db: addr.clone(),
|
db: addr.clone(),
|
||||||
})
|
});
|
||||||
.finish(),
|
|
||||||
);
|
let resp = index(data, test::TestRequest::get().to_http_request());
|
||||||
assert_eq!(resp.status(), http::StatusCode::OK);
|
assert_eq!(resp.status(), http::StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,11 +60,16 @@ mod tests {
|
||||||
DbExecutor(SqliteConnection::establish("test.db").unwrap())
|
DbExecutor(SqliteConnection::establish("test.db").unwrap())
|
||||||
});
|
});
|
||||||
|
|
||||||
let resp = hub(test::TestRequest::with_state(AppState {
|
let data = web::Data::new(AppState {
|
||||||
log: setup_logging(),
|
log: setup_logging(),
|
||||||
db: addr.clone(),
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
20
src/main.rs
20
src/main.rs
|
@ -1,3 +1,4 @@
|
||||||
|
extern crate actix;
|
||||||
extern crate actix_web;
|
extern crate actix_web;
|
||||||
extern crate askama;
|
extern crate askama;
|
||||||
extern crate clap;
|
extern crate clap;
|
||||||
|
@ -8,8 +9,8 @@ extern crate slog;
|
||||||
extern crate slog_async;
|
extern crate slog_async;
|
||||||
extern crate slog_term;
|
extern crate slog_term;
|
||||||
extern crate url;
|
extern crate url;
|
||||||
use actix_web::actix::{SyncArbiter, System};
|
use actix::{SyncArbiter, System};
|
||||||
use actix_web::{http, server, App};
|
use actix_web::{web, App, HttpServer};
|
||||||
use clap::Arg;
|
use clap::Arg;
|
||||||
use controllers::{hub, index};
|
use controllers::{hub, index};
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
|
@ -53,14 +54,17 @@ fn main() {
|
||||||
DbExecutor(SqliteConnection::establish("local.db").unwrap())
|
DbExecutor(SqliteConnection::establish("local.db").unwrap())
|
||||||
});
|
});
|
||||||
|
|
||||||
info!(log, "Starting server");
|
let app_data = web::Data::new(AppState {
|
||||||
server::new(move || {
|
|
||||||
App::with_state(AppState {
|
|
||||||
log: setup_logging(),
|
log: setup_logging(),
|
||||||
db: addr.clone(),
|
db: addr.clone(),
|
||||||
})
|
});
|
||||||
.route("/", http::Method::GET, index)
|
|
||||||
.route("/", http::Method::POST, hub)
|
info!(log, "Starting server");
|
||||||
|
HttpServer::new(move || {
|
||||||
|
App::new()
|
||||||
|
.register_data(app_data.clone())
|
||||||
|
.route("/", web::get().to(index))
|
||||||
|
.route("/", web::post().to(hub))
|
||||||
})
|
})
|
||||||
.bind(format!("{}:{}", address, port))
|
.bind(format!("{}:{}", address, port))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use actix_web::actix::{Actor, Addr, SyncContext};
|
use actix::{Actor, Addr, SyncContext};
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use slog::Drain;
|
use slog::Drain;
|
||||||
use url::form_urlencoded::Parse;
|
use url::form_urlencoded::Parse;
|
||||||
|
|
Loading…
Reference in New Issue