This commit is contained in:
Michael Zhang 2018-09-25 21:45:04 -05:00
parent 5d43c75d13
commit 091c571227
No known key found for this signature in database
GPG Key ID: A1B65B603268116B
9 changed files with 98 additions and 12 deletions

22
Cargo.lock generated
View File

@ -124,6 +124,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "cache"
version = "0.1.0"
dependencies = [
"serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_cbor 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cc"
@ -318,6 +322,11 @@ name = "glob"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "half"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "httparse"
version = "1.3.2"
@ -526,6 +535,7 @@ dependencies = [
"serde_derive 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)",
"structopt 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
"tera 0.11.16 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -844,6 +854,16 @@ name = "serde"
version = "1.0.78"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde_cbor"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"byteorder 1.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
"half 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde_derive"
version = "1.0.79"
@ -1290,6 +1310,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)" = "5e33ec290da0d127825013597dbdfc28bee4964690c7ce1166cbc2a7bd08b1bb"
"checksum generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ef25c5683767570c2bbd7deba372926a55eaae9982d7726ee2a1050239d45b9d"
"checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb"
"checksum half 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee6c0438de3ca4d8cac2eec62b228e2f8865cfe9ebefea720406774223fa2d2e"
"checksum httparse 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7b6288d7db100340ca12873fd4d08ad1b8f206a9457798dfb17c018a33fee540"
"checksum humansize 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e"
"checksum humantime 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0484fda3e7007f2a4a0d9c3a703ca38c71c54c55602ce4660c419fd32e188c9e"
@ -1354,6 +1375,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum scheduled-thread-pool 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1a2ff3fc5223829be817806c6441279c676e454cc7da608faf03b0ccc09d3889"
"checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27"
"checksum serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)" = "92ec94e2754699adddbbc4f555791bd3acc2a2f5574cba16c93a4a9cf4a04415"
"checksum serde_cbor 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "45cd6d95391b16cd57e88b68be41d504183b7faae22030c0cc3b3f73dd57b2fd"
"checksum serde_derive 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)" = "31569d901045afbff7a9479f793177fe9259819aff10ab4f89ef69bbc5f567fe"
"checksum serde_json 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "59790990c5115d16027f00913e2e66de23a51f70422e549d2ad68c8c5f268f1c"
"checksum siphasher 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac"

View File

@ -7,15 +7,17 @@ authors = ["Michael <localhost>"]
members = [".", "cache"]
[dependencies]
cache = { path = "cache" }
embed = { git = "https://github.com/iptq/embed" }
bcrypt = "0.2"
cache = { path = "cache" }
diesel = { version = "1.3", features = ["mysql"] }
embed = { git = "https://github.com/iptq/embed" }
env_logger = "0.5"
failure = "0.1.1"
lazy_static = "1.0"
log = "0.4"
mime_guess = "2.0.0-alpha.6"
r2d2 = "0.8"
r2d2-diesel = "1.0"
rocket = "0.3"
rocket_codegen = "0.3"
rocket_contrib = { version = "0.3", default-features = false }
@ -23,7 +25,4 @@ serde = "1.0"
serde_derive = "1.0"
structopt = "0.2"
tera = "0.11"
r2d2 = "0.8"
r2d2-diesel = "1.0"
diesel = { version = "1.3", features = ["mysql"] }
toml = "0.4"

2
cache/Cargo.toml vendored
View File

@ -7,3 +7,5 @@ authors = ["Michael Zhang <failed.down@gmail.com>"]
path = "lib.rs"
[dependencies]
serde = "1.0"
serde_cbor = "0.9"

38
cache/hashmap.rs vendored Normal file
View File

@ -0,0 +1,38 @@
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use serde_cbor::Value;
use serde_cbor::{from_value, to_value};
use Cache;
/// A cache implemented with a HashMap.
pub struct HashMapCache {
inner: HashMap<String, Value>,
}
impl Cache for HashMapCache {
type Error = ();
fn get<K, V>(&self, key: K) -> Result<V, Self::Error>
where
K: AsRef<str>,
for<'a> V: Deserialize<'a>,
{
self.inner
.get(key.as_ref())
.and_then(|value| from_value(value.clone()).ok())
.ok_or(())
}
fn set<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error>
where
K: AsRef<str>,
V: Serialize,
{
self.inner
.insert(key.as_ref().to_owned(), to_value(value).map_err(|_| ())?);
Ok(())
}
}

29
cache/lib.rs vendored
View File

@ -13,5 +13,32 @@
#![deny(missing_docs)]
extern crate serde;
extern crate serde_cbor;
#[cfg(feature = "redis")]
mod redis;
mod hashmap;
use serde::{Deserialize, Serialize};
pub use hashmap::HashMapCache;
/// An abstraction for a key-value cache.
pub trait Cache {}
pub trait Cache {
/// The Error type (should support both `get` and `set`).
type Error;
/// Get the value associated with the given `key` from the datastore.
fn get<K, V>(&self, key: K) -> Result<V, Self::Error>
where
K: AsRef<str>,
for<'a> V: Deserialize<'a>;
/// Set the value associated with `key` to `value`.
fn set<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error>
where
K: AsRef<str>,
V: Serialize;
}

1
cache/redis.rs vendored Normal file
View File

@ -0,0 +1 @@

View File

@ -38,4 +38,3 @@ pub mod web;
pub use challenge::Challenge;
pub use config::Config;
use db::Connection;

View File

@ -2,7 +2,6 @@ use rocket::{
request::{self, FromRequest, Request},
Outcome,
};
use serde::ser::{Serialize, SerializeStruct, Serializer};
use tera::Context;
#[derive(Serialize, Default)]

View File

@ -1,6 +1,5 @@
use std::borrow::Cow;
use env_logger;
use rocket::{
http::{ContentType, Status},
response::{self, Content},
@ -55,7 +54,7 @@ impl Template {
Err(err) => {
error!("Template render error: {}", err);
Err(Status::InternalServerError)
},
}
}
}
}