Skip to main content

lfsx_server/
lib.rs

1pub mod auth;
2pub mod config;
3pub mod dashboard;
4pub mod error;
5pub mod locks;
6pub mod metrics;
7pub mod model;
8pub mod namespace;
9pub mod page;
10pub mod range;
11pub mod routes;
12pub mod state;
13pub mod storage;
14pub mod tls;
15
16use std::sync::Arc;
17
18use axum::Router;
19
20use crate::auth::Authorizer;
21use crate::config::Config;
22use crate::locks::LockStore;
23use crate::metrics::Metrics;
24use crate::state::AppState;
25use crate::storage::s3::{S3Config, S3Store};
26use crate::storage::{LocalStore, Store};
27
28pub fn app(config: Config) -> Router {
29    // Refusing to start beats starting without it. A server that silently wrote
30    // plaintext because a Secret failed to mount is the one failure this feature
31    // must never have: nothing downstream would notice, and the objects written
32    // in the meantime are the ones the operator believed were covered.
33    let keys = config.encryption_key_file.as_deref().map(|path| {
34        std::sync::Arc::new(
35            crate::storage::crypt::Keyring::load(path)
36                .expect("the encryption key file is not usable"),
37        )
38    });
39
40    let local = LocalStore::new(config.storage_root.clone())
41        .with_max_object_size(config.max_object_size)
42        .with_compression(config.compression)
43        .with_encryption(keys);
44
45    let (store, locks) = match &config.storage {
46        crate::config::Storage::Local => (
47            Store::local(local),
48            LockStore::local(config.storage_root.clone()),
49        ),
50        crate::config::Storage::Bucket {
51            endpoint,
52            bucket,
53            region,
54            access_key,
55            secret_key,
56            path_style,
57            presign,
58        } => {
59            let bucket = S3Store::new(&S3Config {
60                endpoint: endpoint.clone(),
61                bucket: bucket.clone(),
62                region: region.clone(),
63                access_key: access_key.clone(),
64                secret_key: secret_key.clone(),
65                path_style: *path_style,
66                redirect: *presign,
67                lifetime: std::time::Duration::from_secs(config.action_lifetime.into()),
68            })
69            .expect("the bucket configuration is not usable");
70
71            tracing::warn!(
72                "objects and locks are stored in a bucket: collection, deduplication, compression                  and verification answer 501, and the lfsx_objects_stored and lfsx_store_bytes                  gauges are not measured — read capacity from the bucket itself"
73            );
74
75            if *presign {
76                tracing::warn!(
77                    "LFSX_S3_PRESIGN=true — downloads are redirected to the bucket, so                      lfsx_downloaded_bytes stops counting them and the bucket serves the ranges"
78                );
79            }
80
81            if config.encryption_key_file.is_some() {
82                tracing::warn!(
83                    "LFSX_ENCRYPTION_KEY_FILE is set and objects are stored in a bucket — the                      bucket holds them as they arrive, because an encrypted object is only                      readable through the local file the codec opens"
84                );
85            }
86
87            if config.compression.is_some() {
88                tracing::warn!(
89                    "LFSX_COMPRESSION is set and objects are stored in a bucket — the bucket                      holds them uncompressed, because a compressed object is only readable                      through the local file the codec opens"
90                );
91            }
92
93            // The locks go with the objects. Left on the volume they would make
94            // the bucket a half measure: capacity would be shared and the one
95            // piece of state a second replica must agree on would not be.
96            (
97                Store::bucket(bucket.clone(), local),
98                LockStore::bucket(bucket),
99            )
100        }
101    };
102    let authorizer = Authorizer::new(&config.auth);
103
104    routes::router(Arc::new(AppState {
105        store,
106        locks,
107        config,
108        authorizer,
109        metrics: Metrics::new(),
110    }))
111}