Skip to content

Commit 7c6bda1

Browse files
committed
Fix S3 test, cleanup
1 parent d9403f9 commit 7c6bda1

File tree

4 files changed

+23
-29
lines changed

4 files changed

+23
-29
lines changed

docs/src/atomicserver/installation.md

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ ATOMIC_SERVER_URL=https://example.com
107107
```
108108

109109
### Configuring S3 for File Storage
110+
110111
You can configure atomic-server to use S3 (and compatible services) for file storage via environment variables or command line arguments when starting atomic-server.
111112

112113
Credentials can either be found in the standard location for AWS credentials on your OS (e.g. `~/.aws/credentials` on UNIX systems) or by using the environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`.

server/src/files.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl FileStore {
6262
if let FileStore::FS(config) = self {
6363
let fs_file_id = file_id.strip_prefix(Self::FS_PREFIX).unwrap_or(file_id);
6464
let mut file_path = config.path.clone();
65-
file_path.push(fs_file_id.to_string());
65+
file_path.push(fs_file_id);
6666
Ok(file_path)
6767
} else {
6868
Err("Wrong FileStore passed to get_fs_file_path".into())
@@ -82,8 +82,8 @@ impl FileStore {
8282

8383
pub async fn upload_file(&self, file_id: &str, field: Field) -> AtomicServerResult<i64> {
8484
match self {
85-
FileStore::S3(_) => s3_upload(self, &file_id, field).await,
86-
FileStore::FS(config) => fs_upload(self, &config, &file_id, field).await,
85+
FileStore::S3(_) => s3_upload(self, file_id, field).await,
86+
FileStore::FS(config) => fs_upload(self, config, file_id, field).await,
8787
}
8888
}
8989
}
@@ -130,8 +130,8 @@ async fn s3_upload(
130130
if let FileStore::S3(config) = file_store {
131131
builder.bucket(&config.bucket);
132132
builder.root(&config.path);
133-
config.region.as_ref().map(|r| builder.region(&r));
134-
config.endpoint.as_ref().map(|e| builder.endpoint(&e));
133+
config.region.as_ref().map(|r| builder.region(r));
134+
config.endpoint.as_ref().map(|e| builder.endpoint(e));
135135
} else {
136136
return Err("Uploading to S3 but no S3 config provided".into());
137137
}
@@ -141,7 +141,7 @@ async fn s3_upload(
141141
let mut len = 0;
142142
while let Some(chunk) = field.next().await {
143143
let data = chunk.map_err(|e| format!("Error while reading multipart data. {}", e))?;
144-
len = len + data.len();
144+
len += data.len();
145145
w.write(data).await?;
146146
}
147147

@@ -160,8 +160,8 @@ pub async fn get_s3_signed_url(
160160
if let FileStore::S3(config) = file_store {
161161
builder.bucket(&config.bucket);
162162
builder.root(&config.path);
163-
config.region.as_ref().map(|r| builder.region(&r));
164-
config.endpoint.as_ref().map(|e| builder.endpoint(&e));
163+
config.region.as_ref().map(|r| builder.region(r));
164+
config.endpoint.as_ref().map(|e| builder.endpoint(e));
165165
} else {
166166
return Err("Downloading from S3 but no S3 config provided".into());
167167
}

server/src/serve.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub async fn serve(config: crate::config::Config) -> AtomicServerResult<()> {
9898
tracing::info!("Binding HTTP server to endpoint {}", endpoint);
9999
println!("{}", message);
100100
server
101-
.bind(&format!("{}:{}", config.opts.ip, config.opts.port))
101+
.bind(&endpoint)
102102
.map_err(|e| format!("Cannot bind to endpoint {}: {}", &endpoint, e))?
103103
.shutdown_timeout(TIMEOUT)
104104
.run()

server/src/tests.rs

+13-20
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ fn build_request_authenticated(path: &str, appstate: &AppState) -> TestRequest {
3030
prereq.insert_header(("Accept", "application/ad+json"))
3131
}
3232

33-
#[actix_rt::test]
34-
async fn server_tests() {
35-
let unique_string = atomic_lib::utils::random_string(10);
33+
fn build_test_config() -> crate::config::Config {
3634
use clap::Parser;
35+
let unique_string = atomic_lib::utils::random_string(10);
3736
let opts = Opts::parse_from([
3837
"atomic-server",
3938
"--initialize",
@@ -46,8 +45,16 @@ async fn server_tests() {
4645
let mut config = config::build_config(opts)
4746
.map_err(|e| format!("Initialization failed: {}", e))
4847
.expect("failed init config");
48+
4949
// This prevents folder access issues when running concurrent tests
5050
config.search_index_path = format!("./.temp/{}/search_index", unique_string).into();
51+
config
52+
}
53+
54+
#[actix_rt::test]
55+
async fn server_tests() {
56+
let unique_string = atomic_lib::utils::random_string(10);
57+
let config = build_test_config();
5158

5259
let appstate = crate::appstate::init(config.clone()).expect("failed init appstate");
5360
let data = Data::new(appstate.clone());
@@ -166,20 +173,7 @@ fn get_body(resp: ServiceResponse) -> String {
166173

167174
#[actix_rt::test]
168175
async fn file_store_tests() {
169-
let unique_string = atomic_lib::utils::random_string(10);
170-
use clap::Parser;
171-
let mut opts = Opts::parse_from([
172-
"atomic-server",
173-
"--initialize",
174-
"--data-dir",
175-
&format!("./.temp/{}/db", unique_string),
176-
"--config-dir",
177-
&format!("./.temp/{}/config", unique_string),
178-
]);
179-
180-
let mut config = config::build_config(opts.clone())
181-
.map_err(|e| format!("Initialization failed: {}", e))
182-
.expect("failed init config");
176+
let mut config = build_test_config();
183177

184178
let fs_store = FileStore::init_fs_from_config(&config);
185179
if let FileStore::FS(fs_config) = &fs_store {
@@ -206,9 +200,8 @@ async fn file_store_tests() {
206200
.contains("uploads/my-great-file"));
207201

208202
// FileStore::S3 init
209-
opts.s3_bucket = Some("test-bucket".to_string());
210-
opts.s3_path = Some("uploads".to_string());
211-
config.opts = opts;
203+
config.opts.s3_bucket = Some("test-bucket".to_string());
204+
config.opts.s3_path = Some("uploads".to_string());
212205
let appstate = crate::appstate::init(config.clone()).expect("failed init appstate");
213206

214207
let s3_store = FileStore::init_from_config(&config, fs_store.clone());

0 commit comments

Comments
 (0)