Browse Source

add db to AppState

master
Gonçalo Valério 3 years ago
parent
commit
ed18c442f2
  1. 1
      .gitignore
  2. 23
      Cargo.lock
  3. 2
      Cargo.toml
  4. 16
      src/controllers.rs
  5. 17
      src/main.rs
  6. 8
      src/models.rs
  7. 9
      src/utils.rs

1
.gitignore vendored

@ -4,3 +4,4 @@
.vscode
*.iml
local.db
test.db

23
Cargo.lock generated

@ -430,6 +430,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"diesel_derives 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libsqlite3-sys 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -718,6 +719,15 @@ name = "libc"
version = "0.2.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libsqlite3-sys"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"vcpkg 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "linked-hash-map"
version = "0.5.2"
@ -963,6 +973,11 @@ dependencies = [
"unicase 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "pkg-config"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "proc-macro2"
version = "0.4.27"
@ -1802,6 +1817,11 @@ dependencies = [
"version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "vcpkg"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "vec_map"
version = "0.8.1"
@ -1950,6 +1970,7 @@ dependencies = [
"checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14"
"checksum lazycell 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f"
"checksum libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)" = "aab692d7759f5cd8c859e169db98ae5b52c924add2af5fbbca11d12fefb567c1"
"checksum libsqlite3-sys 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fd6457c70bbff456d9fe49deaba35ec47c3e598bf8d7950ff0575ceb7a8a6ad1"
"checksum linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ae91b68aebc4ddb91978b11a1b02ddd8602a05ec19002801c5666000e05e0f83"
"checksum lock_api 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "62ebf1391f6acad60e5c8b43706dde4582df75c06698ab44511d15016bc2442c"
"checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6"
@ -1979,6 +2000,7 @@ dependencies = [
"checksum phf_codegen 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)" = "b03e85129e324ad4166b06b2c7491ae27fe3ec353af72e72cd1654c7225d517e"
"checksum phf_generator 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)" = "09364cc93c159b8b06b1f4dd8a4398984503483891b0c26b867cf431fb132662"
"checksum phf_shared 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)" = "234f71a15de2288bcb7e3b6515828d22af7ec8598ee6d24c3b526fa0a80b67a0"
"checksum pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c"
"checksum proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)" = "4d317f9caece796be1980837fd5cb3dfec5613ebdb04ad0956deea83ce168915"
"checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0"
"checksum quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "cdd8e04bd9c52e0342b406469d494fcb033be4bdbe5c606016defbb1681411e1"
@ -2068,6 +2090,7 @@ dependencies = [
"checksum v_escape 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c8b50688edb86f4c092a1a9fe8bda004b0faa3197100897653809e97e09a2814"
"checksum v_escape_derive 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7cd994c63b487fef7aad31e5394ec04b9e24de7b32ea5251c9fb499cd2cbf44c"
"checksum v_htmlescape 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "020cae817dc82693aa523f01087b291b1c7a9ac8cea5c12297963f21769fb27f"
"checksum vcpkg 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "def296d3eb3b12371b2c7d0e83bfe1403e4db2d7a0bba324a12b21c4ee13143d"
"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a"
"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
"checksum widestring 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7157704c2e12e3d2189c507b7482c52820a16dfa4465ba91add92f266667cadb"

2
Cargo.toml

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

16
src/controllers.rs

@ -19,14 +19,22 @@ pub fn hub(_req: HttpRequest<AppState>) -> HttpResponse {
#[cfg(test)]
mod tests {
use super::*;
use actix_web::actix::{SyncArbiter, System};
use actix_web::{http, test};
use utils::setup_logging;
use diesel::prelude::*;
use utils::{setup_logging, DbExecutor};
#[test]
fn test_index() {
let _sys = System::new("rusty-hub-test");
let addr = SyncArbiter::start(1, || {
DbExecutor(SqliteConnection::establish("test.db").unwrap())
});
let resp = index(
test::TestRequest::with_state(AppState {
log: setup_logging(),
db: addr.clone(),
})
.finish(),
);
@ -35,8 +43,14 @@ mod tests {
#[test]
fn test_hub() {
let _sys = System::new("rusty-hub-test");
let addr = SyncArbiter::start(1, || {
DbExecutor(SqliteConnection::establish("test.db").unwrap())
});
let resp = hub(test::TestRequest::with_state(AppState {
log: setup_logging(),
db: addr.clone(),
})
.finish());
assert_eq!(resp.status(), http::StatusCode::OK);

17
src/main.rs

@ -2,15 +2,21 @@ extern crate actix_web;
extern crate askama;
extern crate clap;
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate slog;
extern crate slog_async;
extern crate slog_term;
use actix_web::actix::{SyncArbiter, System};
use actix_web::{http, server, App};
use clap::Arg;
use controllers::{hub, index};
use utils::{setup_logging, AppState};
use diesel::prelude::*;
use utils::{setup_logging, AppState, DbExecutor};
mod controllers;
mod models;
mod schema;
mod utils;
fn main() {
@ -40,16 +46,23 @@ fn main() {
return;
}
let sys = System::new("rusty-hub");
let addr = SyncArbiter::start(3, || {
DbExecutor(SqliteConnection::establish("local.db").unwrap())
});
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)
})
.bind(format!("{}:{}", address, port))
.unwrap()
.run();
.start();
let _ = sys.run();
info!(log, "Shutting down server");
}

8
src/models.rs

@ -1,4 +1,4 @@
use super::schema::posts;
use super::schema::subscriptions;
#[derive(Queryable)]
pub struct Subscription {
@ -7,15 +7,15 @@ pub struct Subscription {
pub topic: String,
pub sec: String,
pub created_at: i32,
pub expires_at: i32
pub expires_at: i32,
}
#[derive(Insertable)]
#[table_name="subscriptions"]
#[table_name = "subscriptions"]
pub struct NewSubscription<'a> {
pub callback: &'a str,
pub topic: &'a str,
pub sec: &'a str,
pub created_at: &'a i32,
pub expires_at: &'a i32
pub expires_at: &'a i32,
}

9
src/utils.rs

@ -1,7 +1,16 @@
use actix_web::actix::{Actor, Addr, SyncContext};
use diesel::prelude::*;
use slog::Drain;
pub struct DbExecutor(pub SqliteConnection);
impl Actor for DbExecutor {
type Context = SyncContext<Self>;
}
pub struct AppState {
pub log: slog::Logger,
pub db: Addr<DbExecutor>,
}
pub fn setup_logging() -> slog::Logger {

Loading…
Cancel
Save