Line data Source code
1 : //! `object_storage` is a service which provides API for uploading and downloading
2 : //! files. It is used by compute and control plane for accessing LFC prewarm data.
3 : //! This service is deployed either as a separate component or as part of compute image
4 : //! for large computes.
5 : mod app;
6 : use anyhow::Context;
7 : use tracing::info;
8 : use utils::logging;
9 :
10 : //see set()
11 0 : const fn max_upload_file_limit() -> usize {
12 0 : 100 * 1024 * 1024
13 0 : }
14 :
15 0 : #[derive(serde::Deserialize)]
16 : #[serde(tag = "type")]
17 : struct Config {
18 : listen: std::net::SocketAddr,
19 : pemfile: camino::Utf8PathBuf,
20 : #[serde(flatten)]
21 : storage_config: remote_storage::RemoteStorageConfig,
22 : #[serde(default = "max_upload_file_limit")]
23 : max_upload_file_limit: usize,
24 : }
25 :
26 : #[tokio::main]
27 0 : async fn main() -> anyhow::Result<()> {
28 0 : logging::init(
29 0 : logging::LogFormat::Plain,
30 0 : logging::TracingErrorLayerEnablement::EnableWithRustLogFilter,
31 0 : logging::Output::Stdout,
32 0 : )?;
33 0 :
34 0 : let config: String = std::env::args().skip(1).take(1).collect();
35 0 : if config.is_empty() {
36 0 : anyhow::bail!("Usage: object_storage config.json")
37 0 : }
38 0 : info!("Reading config from {config}");
39 0 : let config = std::fs::read_to_string(config.clone())?;
40 0 : let config: Config = serde_json::from_str(&config).context("parsing config")?;
41 0 : info!("Reading pemfile from {}", config.pemfile.clone());
42 0 : let pemfile = std::fs::read(config.pemfile.clone())?;
43 0 : info!("Loading public key from {}", config.pemfile.clone());
44 0 : let auth = object_storage::JwtAuth::new(&pemfile)?;
45 0 :
46 0 : let listener = tokio::net::TcpListener::bind(config.listen).await.unwrap();
47 0 : info!("listening on {}", listener.local_addr().unwrap());
48 0 :
49 0 : let storage = remote_storage::GenericRemoteStorage::from_config(&config.storage_config).await?;
50 0 : let cancel = tokio_util::sync::CancellationToken::new();
51 0 : app::check_storage_permissions(&storage, cancel.clone()).await?;
52 0 :
53 0 : let proxy = std::sync::Arc::new(object_storage::Storage {
54 0 : auth,
55 0 : storage,
56 0 : cancel: cancel.clone(),
57 0 : max_upload_file_limit: config.max_upload_file_limit,
58 0 : });
59 0 :
60 0 : tokio::spawn(utils::signals::signal_handler(cancel.clone()));
61 0 : axum::serve(listener, app::app(proxy))
62 0 : .with_graceful_shutdown(async move { cancel.cancelled().await })
63 0 : .await?;
64 0 : Ok(())
65 0 : }
|