/ src / main.rs
main.rs
 1  #![forbid(unsafe_code)]
 2  
 3  use std::str::FromStr;
 4  
 5  use config::Config;
 6  use sqlx::{
 7      postgres::{PgConnectOptions, PgPoolOptions},
 8      ConnectOptions,
 9  };
10  
11  #[macro_use]
12  extern crate rocket;
13  
14  pub mod api;
15  pub mod config;
16  pub mod db;
17  pub mod debug_headers;
18  pub mod models;
19  pub mod registry_error;
20  pub mod services;
21  pub mod types;
22  
23  #[launch]
24  async fn rocket() -> _ {
25      let config = Config::new().expect("Failed to load config");
26  
27      // Setup DB
28      let mut pg_options =
29          PgConnectOptions::from_str(&config.database_url).expect("Invalid database url provided");
30  
31      if !config.log_db_statements {
32          pg_options.disable_statement_logging();
33      }
34  
35      let db_pool = PgPoolOptions::new()
36          .max_connections(5)
37          .connect_with(pg_options)
38          .await
39          .expect("Failed to connect to DB");
40  
41      sqlx::migrate!("./migrations")
42          .run(&db_pool)
43          .await
44          .expect("Failed to run migrations");
45  
46      // TODO: avoid hardcoded URL
47      let docker = docker_api::Docker::new(config.docker_socket_url.clone())
48          .expect("Failed to connect to docker");
49  
50      rocket::build()
51          .mount(
52              "/",
53              routes![
54                  api::container_spec::blobs::get_blob,
55                  api::container_spec::get_spec_compliance,
56                  api::container_spec::blobs::post_create_session,
57                  api::container_spec::blobs::patch_upload_blob,
58                  api::container_spec::blobs::put_upload_blob,
59                  api::container_spec::manifests::put_manifest,
60                  api::container_spec::manifests::get_manifest,
61              ],
62          )
63          .mount(
64              "/api",
65              routes![
66                  api::images::get_images,
67                  api::images::run_image,
68                  api::images::get_container_status
69              ],
70          )
71          .manage(db_pool)
72          .manage(config)
73          .manage(docker)
74  }