Commit
+55 -41 +/-2 browse
1 | diff --git a/src/app.rs b/src/app.rs |
2 | index d5f604a..596256a 100644 |
3 | --- a/src/app.rs |
4 | +++ b/src/app.rs |
5 | @@ -69,16 +69,18 @@ pub struct App<S> { |
6 | storage: S, |
7 | } |
8 | |
9 | - impl<S> App<S> |
10 | - where |
11 | - S: Storage + Send + Sync + 'static, |
12 | - S::Error: Into<Error> + 'static, |
13 | - Error: From<S::Error>, |
14 | - { |
15 | + impl<S> App<S> { |
16 | pub fn new(storage: S) -> Self { |
17 | App { storage } |
18 | } |
19 | + } |
20 | |
21 | + impl<S> App<S> |
22 | + where |
23 | + S: Storage + Send + Sync, |
24 | + S::Error: Into<Error>, |
25 | + Error: From<S::Error>, |
26 | + { |
27 | /// Handles the index route. |
28 | fn index(req: Request<Body>) -> Result<Response<Body>, Error> { |
29 | let template = IndexTemplate { |
30 | diff --git a/src/main.rs b/src/main.rs |
31 | index e7abbdb..921e83f 100644 |
32 | --- a/src/main.rs |
33 | +++ b/src/main.rs |
34 | @@ -40,7 +40,7 @@ use structopt::StructOpt; |
35 | use crate::app::App; |
36 | use crate::error::Error; |
37 | use crate::logger::Logger; |
38 | - use crate::storage::{Cached, Disk, Encrypted, Retrying, Verify, S3}; |
39 | + use crate::storage::{Cached, Disk, Encrypted, Retrying, Storage, Verify, S3}; |
40 | |
41 | #[cfg(feature = "faulty")] |
42 | use crate::storage::Faulty; |
43 | @@ -53,7 +53,7 @@ struct Args { |
44 | |
45 | /// Root directory of the object cache. |
46 | #[structopt(long = "cache-dir")] |
47 | - cache_dir: PathBuf, |
48 | + cache_dir: Option<PathBuf>, |
49 | |
50 | /// Logging level to use. |
51 | #[structopt(long = "log-level", default_value = "info")] |
52 | @@ -98,17 +98,10 @@ impl Args { |
53 | .next() |
54 | .unwrap_or_else(|| SocketAddr::from(([0, 0, 0, 0], 8080))); |
55 | |
56 | - // Convert cache size to bytes. |
57 | - let max_cache_size = |
58 | - self.max_cache_size.into::<human_size::Byte>().value() as u64; |
59 | - let key = self.key; |
60 | - |
61 | - // Initialize our storage backends. |
62 | - let disk = Disk::new(self.cache_dir).map_err(Error::from); |
63 | - let s3 = S3::new(self.s3_bucket, self.s3_prefix).map_err(Error::from); |
64 | - |
65 | log::info!("Initializing storage..."); |
66 | - let (disk, s3) = future::try_join(disk, s3).await?; |
67 | + let s3 = S3::new(self.s3_bucket, self.s3_prefix) |
68 | + .map_err(Error::from) |
69 | + .await?; |
70 | |
71 | // Retry certain operations to S3 to make it more reliable. |
72 | let s3 = Retrying::new(s3); |
73 | @@ -116,37 +109,56 @@ impl Args { |
74 | // Add a little instability for testing purposes. |
75 | #[cfg(feature = "faulty")] |
76 | let s3 = Faulty::new(s3); |
77 | - #[cfg(feature = "faulty")] |
78 | - let disk = Faulty::new(disk); |
79 | |
80 | - // Use the disk as a cache. |
81 | - let storage = Cached::new(max_cache_size, disk, s3).await?; |
82 | + match self.cache_dir { |
83 | + Some(cache_dir) => { |
84 | + // Convert cache size to bytes. |
85 | + let max_cache_size = |
86 | + self.max_cache_size.into::<human_size::Byte>().value() |
87 | + as u64; |
88 | + |
89 | + // Use disk storage as a cache. |
90 | + let disk = Disk::new(cache_dir).map_err(Error::from).await?; |
91 | + |
92 | + #[cfg(feature = "faulty")] |
93 | + let disk = Faulty::new(disk); |
94 | + |
95 | + let cache = Cached::new(max_cache_size, disk, s3).await?; |
96 | + let storage = Verify::new(Encrypted::new(self.key, cache)); |
97 | + run_server(storage, &addr).await?; |
98 | + } |
99 | + None => { |
100 | + let storage = Verify::new(Encrypted::new(self.key, s3)); |
101 | + run_server(storage, &addr).await?; |
102 | + } |
103 | + }; |
104 | |
105 | - // Encrypt/decrypt and verify all incoming and outgoing data. |
106 | - let storage = Arc::new(Verify::new(Encrypted::new(key, storage))); |
107 | + Ok(()) |
108 | + } |
109 | + } |
110 | |
111 | - // Create our service factory. |
112 | - let new_service = make_service_fn(move |socket: &AddrStream| { |
113 | - // Create our app. |
114 | - let service = App::new(storage.clone()); |
115 | + async fn run_server<S>(storage: S, addr: &SocketAddr) -> hyper::Result<()> |
116 | + where |
117 | + S: Storage + Send + Sync + 'static, |
118 | + S::Error: Into<Error>, |
119 | + Error: From<S::Error>, |
120 | + { |
121 | + let storage = Arc::new(storage); |
122 | |
123 | - // Add logging middleware |
124 | - future::ok::<_, Infallible>(Logger::new( |
125 | - socket.remote_addr(), |
126 | - service, |
127 | - )) |
128 | - }); |
129 | + let new_service = make_service_fn(move |socket: &AddrStream| { |
130 | + // Create our app. |
131 | + let service = App::new(storage.clone()); |
132 | |
133 | - // Create the server. |
134 | - let server = Server::bind(&addr).serve(new_service); |
135 | + // Add logging middleware |
136 | + future::ok::<_, Infallible>(Logger::new(socket.remote_addr(), service)) |
137 | + }); |
138 | |
139 | - log::info!("Listening on {}", server.local_addr()); |
140 | + let server = Server::bind(&addr).serve(new_service); |
141 | |
142 | - // Run the server. |
143 | - server.await?; |
144 | + log::info!("Listening on {}", server.local_addr()); |
145 | |
146 | - Ok(()) |
147 | - } |
148 | + server.await?; |
149 | + Ok(()) |
150 | } |
151 | |
152 | #[tokio::main] |