Skip to content

Commit db52521

Browse files
metamejoepio
authored andcommitted
move file stores to appstate
1 parent edaeae4 commit db52521

File tree

5 files changed

+29
-32
lines changed

5 files changed

+29
-32
lines changed

server/src/appstate.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! App state, which is accessible from handlers
22
use crate::{
3-
commit_monitor::CommitMonitor, config::Config, errors::AtomicServerResult, search::SearchState,
3+
commit_monitor::CommitMonitor, config::Config, errors::AtomicServerResult, files::FileStore,
4+
search::SearchState,
45
};
56
use atomic_lib::{
67
agents::{generate_public_key, Agent},
@@ -23,6 +24,10 @@ pub struct AppState {
2324
/// The Actix Address of the CommitMonitor, which should receive updates when a commit is applied
2425
pub commit_monitor: actix::Addr<CommitMonitor>,
2526
pub search_state: SearchState,
27+
/// stores config values and the active FileStore type, e.g. FS or S3
28+
pub file_store: FileStore,
29+
/// stores config values for FS filestore regardless of active file store as fallback
30+
pub fs_file_store: FileStore,
2631
}
2732

2833
/// Creates the AppState (the server's context available in Handlers).
@@ -102,11 +107,18 @@ pub fn init(config: Config) -> AtomicServerResult<AppState> {
102107
crate::search::add_all_resources(&search_state, &store)?
103108
}
104109

110+
tracing::info!("Initializing file stores");
111+
// Initialize file stores
112+
let fs_file_store = FileStore::init_fs_from_config(&config);
113+
let file_store = FileStore::init_from_config(&config, fs_file_store.clone());
114+
105115
Ok(AppState {
106116
store,
107117
config,
108118
commit_monitor,
109119
search_state,
120+
file_store,
121+
fs_file_store,
110122
})
111123
}
112124

server/src/config.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Parse CLI options, setup on boot, read .env values
22
33
use crate::errors::AtomicServerResult;
4-
use crate::files::FileStore;
54
use clap::Parser;
65
use dotenv::dotenv;
76
use std::net::IpAddr;
@@ -207,10 +206,6 @@ pub struct Config {
207206
pub uploads_path: PathBuf,
208207
/// Path to where the search index for tantivy full text search is located
209208
pub search_index_path: PathBuf,
210-
/// stores config values and the active FileStore type, e.g. FS or S3
211-
pub file_store: FileStore,
212-
/// stores config values for FS filestore regardless of active file store as fallback
213-
pub fs_file_store: FileStore,
214209
/// If true, the initialization scripts will be ran (create first Drive, Agent, indexing, etc)
215210
pub initialize: bool,
216211
}
@@ -270,10 +265,6 @@ pub fn build_config(opts: Opts) -> AtomicServerResult<Config> {
270265

271266
let initialize = !std::path::Path::exists(&store_path) || opts.initialize;
272267

273-
// Initialize FileStore config
274-
let fs_file_store = FileStore::init_fs_from_config(&opts, uploads_path.clone());
275-
let file_store = FileStore::init_from_config(&opts, fs_file_store.clone());
276-
277268
if opts.https & opts.email.is_none() {
278269
return Err(
279270
"The `--email` flag (or ATOMIC_EMAIL env) is required for getting an HTTPS certificate from letsencrypt.org."
@@ -305,7 +296,5 @@ pub fn build_config(opts: Opts) -> AtomicServerResult<Config> {
305296
store_path,
306297
search_index_path,
307298
uploads_path,
308-
file_store,
309-
fs_file_store,
310299
})
311300
}

server/src/files.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ use std::{fmt, path::PathBuf, time::Duration};
33
use opendal::{services::S3, Operator};
44
use tokio::fs::File;
55

6-
use crate::{
7-
config::{Config, Opts},
8-
errors::AtomicServerResult,
9-
};
6+
use crate::{appstate::AppState, config::Config, errors::AtomicServerResult};
107

118
#[derive(Clone, Debug)]
129
pub enum FileStore {
@@ -24,18 +21,21 @@ pub struct S3Config {
2421

2522
#[derive(Clone, Debug)]
2623
pub struct FSConfig {
27-
path: PathBuf
24+
path: PathBuf,
2825
}
2926

3027
impl FileStore {
3128
const S3_PREFIX: &'static str = "s3:";
3229
const FS_PREFIX: &'static str = "fs:";
3330

34-
pub fn init_fs_from_config(_opts: &Opts, path: PathBuf) -> FileStore {
35-
FileStore::FS(FSConfig {path})
31+
pub fn init_fs_from_config(config: &Config) -> FileStore {
32+
FileStore::FS(FSConfig {
33+
path: config.uploads_path.clone(),
34+
})
3635
}
3736

38-
pub fn init_from_config(opts: &Opts, fs_file_store: FileStore) -> FileStore {
37+
pub fn init_from_config(config: &Config, fs_file_store: FileStore) -> FileStore {
38+
let opts = &config.opts;
3939
if opts.s3_bucket.is_some() {
4040
let config = S3Config {
4141
bucket: opts.s3_bucket.clone().unwrap(),
@@ -49,11 +49,11 @@ impl FileStore {
4949
}
5050
}
5151

52-
pub fn get_subject_file_store<'a>(config: &'a Config, subject: &str) -> &'a FileStore {
52+
pub fn get_subject_file_store<'a>(appstate: &'a AppState, subject: &str) -> &'a FileStore {
5353
if subject.contains(Self::S3_PREFIX) {
54-
&config.file_store
54+
&appstate.file_store
5555
} else {
56-
&config.fs_file_store
56+
&appstate.fs_file_store
5757
}
5858
}
5959

server/src/handlers/download.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub async fn handle_download(
3333

3434
let for_agent = get_client_agent(headers, &appstate, subject.clone())?;
3535
tracing::info!("handle_download: {}", subject);
36-
let file_store = FileStore::get_subject_file_store(&appstate.config, &subject);
36+
let file_store = FileStore::get_subject_file_store(&appstate, &subject);
3737
let encoded = subject.replace(file_store.prefix(), &file_store.encoded());
3838
let resource = store.get_resource_extended(&encoded, false, &for_agent)?;
3939
let file_id = resource
@@ -53,7 +53,7 @@ pub fn download_file_handler_partial(
5353
req: &HttpRequest,
5454
appstate: &AppState,
5555
) -> AtomicServerResult<HttpResponse> {
56-
let file_path = appstate.config.fs_file_store.get_fs_file_path(file_id)?;
56+
let file_path = appstate.fs_file_store.get_fs_file_path(file_id)?;
5757
let file = NamedFile::open(file_path)?;
5858
Ok(file.into_response(req))
5959
}
@@ -63,12 +63,8 @@ async fn signed_url_redirect_handler(
6363
req: &HttpRequest,
6464
appstate: &AppState,
6565
) -> AtomicServerResult<HttpResponse> {
66-
let signed_url = files::get_s3_signed_url(
67-
&appstate.config.file_store,
68-
Duration::from_secs(3600),
69-
file_id,
70-
)
71-
.await?;
66+
let signed_url =
67+
files::get_s3_signed_url(&appstate.file_store, Duration::from_secs(3600), file_id).await?;
7268
Ok(web::Redirect::to(signed_url)
7369
.respond_to(req)
7470
.map_into_boxed_body())

server/src/handlers/upload.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub async fn upload_handler(
8585
.try_into()
8686
.map_err(|_e| "Too large")?;
8787

88-
let file_store = &appstate.config.file_store;
88+
let file_store = &appstate.file_store;
8989
let file_id = format!("{}{}", file_store.prefix(), &fs_file_id);
9090
if let FileStore::S3(_) = file_store {
9191
files::s3_upload_object(&file_store, &file_id, &file_path).await?;

0 commit comments

Comments
 (0)