Skip to content

Commit

Permalink
Drunken refactoring removing color_eyre and using anyhow + tracing to…
Browse files Browse the repository at this point in the history
… consistently and concisely handle errors
  • Loading branch information
SpookyBoy99 committed Jul 29, 2024
1 parent ef0863b commit bb549da
Show file tree
Hide file tree
Showing 17 changed files with 154 additions and 136 deletions.
2 changes: 1 addition & 1 deletion server/chroma/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ actix-cors = "0.6.4"
actix-multiresponse = { version = "0.4.2", features = ["xml"] }
actix-web = "4.3.0"
actix-governor = "0.4.0"
color-eyre = "0.6.2"
proto = { version = "0.1.0", path = "../proto" }
serde = { version = "1.0.152", features = ["derive"] }
thiserror = "1.0.38"
Expand All @@ -32,6 +31,7 @@ cabbage = "0.1.2"
moka = { version = "0.12.5", features = ["future"] }
dotenv = "0.15.0"
governor = "0.6.3"
anyhow = "1.0.86"

[dev-dependencies]
serde_json = "1.0.93"
4 changes: 2 additions & 2 deletions server/chroma/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use color_eyre::eyre::Error;
use anyhow::{Error, Result};
use serde::Deserialize;
use tracing::{info, warn};

Expand Down Expand Up @@ -105,7 +105,7 @@ impl Config {
}

/// Get the database configuration specified by the configuration
pub fn database_config(&self) -> color_eyre::Result<DbConfig> {
pub fn database_config(&self) -> Result<DbConfig> {
if let Some(url) = &self.db_url {
Ok(DbConfig::Url { url })
} else {
Expand Down
140 changes: 23 additions & 117 deletions server/chroma/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,141 +1,47 @@
extern crate core;

use std::process;
use std::time::Duration;

use actix_cors::Cors;
use actix_web::{web, App, HttpServer};
use cabbage::KoalaApi;
use color_eyre::Result;
use dotenv::dotenv;
use noiseless_tracing_actix_web::NoiselessRootSpanBuilder;
use tracing::{error, info, warn};
use tracing_actix_web::TracingLogger;
use tracing::{error, warn};
use tracing::level_filters::LevelFilter;
use tracing_subscriber::EnvFilter;
use tracing_subscriber::fmt::layer;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::EnvFilter;

use dal::database::Database;
use dal::storage_engine::{S3Config, Storage};

use crate::config::Config;
use crate::routes::appdata::{AlbumIdCache, AppData, Ratelimits, SessionIdCache, WebData};
use crate::routes::routable::Routable;

mod config;
mod routes;
mod server;

#[tokio::main]
async fn main() -> Result<()> {
async fn main() {
// Try to load the environment variables from the .env file
if let Err(err) = dotenv() {
panic!("failed to load .env file: {:#}", err);
}
let dotenv_err = dotenv().err();

// Initialize the tracing logger
init_tracing();

// Initialize chroma's core components
let config = init_config();
let db = init_database(&config).await;
let storage = init_storage(&config).await;

// Package the core components up into the AppData struct
let app_data = AppData {
koala: KoalaApi::new(config.koala_base_redirect_uri().clone())?,
db,
storage,
config,
ratelimits: Ratelimits::new(),
};
// Check if the dot env was loaded correctly, otherwise send a warning
if let Some(err) = dotenv_err {
warn!("failed to load .env file: {:#}", err);
}

// Run the webserver using the AppData until stopped or crash
start_webserver(app_data).await
// Run the server
if let Err(err) = server::run().await {
error!("{}", err);
err.chain()
.skip(1)
.for_each(|cause| error!("because: {}", cause));
std::process::exit(1);
}
}

fn init_tracing() {
tracing_subscriber::registry()
.with(layer().compact())
.with(EnvFilter::from_default_env())
.with(
EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy(),
)
.init();
}

fn init_config() -> Config {
info!("parsing config");
let config = Config::parse().unwrap_or_else(|err| {
error!("failed to parse config: {:#}", err);
process::exit(1);
});

if !config.validate() {
error!("config is not valid");
process::exit(1);
}

if !config.service_tokens.is_empty() {
warn!("there are service tokens configured, Make sure these are, and stay, confidential!");
}

config
}

async fn init_database(config: &Config) -> Database {
info!("initializing database connection");
Database::new(config.database_config().unwrap())
.await
.unwrap_or_else(|err| {
error!("failed to initialize database connection: {:#}", err);
process::exit(1);
})
}

async fn init_storage(config: &Config) -> Storage {
info!("initializing S3 storage engine");
Storage::new(S3Config {
bucket_name: config.s3_bucket_name.clone().unwrap(),
endpoint_url: config.s3_endpoint_url.clone().unwrap(),
region: config.s3_region.clone().unwrap(),
access_key_id: config.s3_access_key_id.clone().unwrap(),
secret_access_key: config.s3_secret_access_key.clone().unwrap(),
use_path_style: config.s3_force_path_style(),
create_bucket: config.s3_create_bucket_on_startup(),
})
.await
.unwrap_or_else(|err| {
error!("failed to initialize S3 storage engine: {:#}", err);
process::exit(1);
})
}

async fn start_webserver(app_data: AppData) -> Result<()> {
info!("starting web server");
HttpServer::new(move || {
App::new()
.wrap(Cors::permissive())
.wrap(TracingLogger::<NoiselessRootSpanBuilder>::new())
.app_data(WebData::new(app_data.clone()))
.app_data(web::Data::new(
SessionIdCache::builder()
.max_capacity(10000)
.time_to_live(Duration::from_secs(30))
.build(),
))
.app_data(web::Data::new(
AlbumIdCache::builder().max_capacity(10000).build(),
))
.configure(routes::Router::configure)
})
.bind(&format!(
"0.0.0.0:{}",
std::env::var("HTTP_PORT").unwrap_or("8000".into())
))
.unwrap_or_else(|err| {
error!("failed to bind web server to port 8000: {:#}", err);
process::exit(1);
})
.run()
.await?;

Ok(())
}
2 changes: 1 addition & 1 deletion server/chroma/src/routes/appdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::sync::Arc;

use actix_web::web;
use cabbage::KoalaApi;
use governor::{Quota, RateLimiter};
use governor::clock::DefaultClock;
use governor::state::{InMemoryState, NotKeyed};
use governor::{Quota, RateLimiter};
use moka::future::Cache;

use dal::database::{Album, Database};
Expand Down
2 changes: 1 addition & 1 deletion server/chroma/src/routes/authorization.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::future::Future;
use std::pin::Pin;

use actix_web::{FromRequest, HttpRequest, HttpResponse, ResponseError, web};
use actix_web::body::BoxBody;
use actix_web::dev::Payload;
use actix_web::http::StatusCode;
use actix_web::{web, FromRequest, HttpRequest, HttpResponse, ResponseError};
use tap::TapFallible;
use thiserror::Error;
use tracing::{info, trace};
Expand Down
2 changes: 1 addition & 1 deletion server/chroma/src/routes/empty.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use actix_web::body::BoxBody;
use actix_web::{HttpRequest, HttpResponse, Responder};
use actix_web::body::BoxBody;

pub struct Empty;

Expand Down
2 changes: 1 addition & 1 deletion server/chroma/src/routes/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use actix_web::{HttpResponse, ResponseError};
use actix_web::body::BoxBody;
use actix_web::http::StatusCode;
use actix_web::{HttpResponse, ResponseError};
use thiserror::Error;

pub type WebResult<T> = Result<T, Error>;
Expand Down
2 changes: 1 addition & 1 deletion server/chroma/src/routes/redirect.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use actix_web::{HttpRequest, HttpResponse, Responder};
use actix_web::body::BoxBody;
use actix_web::http::StatusCode;
use actix_web::{HttpRequest, HttpResponse, Responder};

pub struct Redirect(String);

Expand Down
2 changes: 1 addition & 1 deletion server/chroma/src/routes/v1/album/delete.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use actix_multiresponse::Payload;

use dal::database::PhotoQuality;
use dal::database::{Album, Photo};
use dal::database::PhotoQuality;
use proto::DeleteAlbumRequest;

use crate::routes::appdata::WebData;
Expand Down
4 changes: 2 additions & 2 deletions server/chroma/src/routes/v1/album/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use actix_web::web;
use futures::future::join_all;
use serde::Deserialize;

use dal::database::PhotoQuality;
use dal::database::{Album, Photo};
use dal::DalError;
use dal::database::{Album, Photo};
use dal::database::PhotoQuality;
use proto::{AlbumWithCoverPhoto, GetAlbumResponse};

use crate::routes::appdata::{AlbumIdCache, WebData};
Expand Down
2 changes: 1 addition & 1 deletion server/chroma/src/routes/v1/album/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use actix_web::web;
use futures::future::{join_all, try_join_all};
use serde::Deserialize;

use dal::DalError;
use dal::database::{Album, Photo};
use dal::storage_engine::aws_error::GetObjectErrorKind;
use dal::storage_engine::error::{SdkError, StorageError};
use dal::DalError;
use proto::{AlbumWithCoverPhoto, ListAlbumsResponse};

use crate::routes::appdata::{AlbumIdCache, WebData};
Expand Down
2 changes: 1 addition & 1 deletion server/chroma/src/routes/v1/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::time::Duration;

use actix_governor::governor::middleware::StateInformationMiddleware;
use actix_governor::{Governor, GovernorConfig, GovernorConfigBuilder, PeerIpKeyExtractor};
use actix_governor::governor::middleware::StateInformationMiddleware;
use actix_web::web;
use actix_web::web::ServiceConfig;
use serde::Deserialize;
Expand Down
4 changes: 2 additions & 2 deletions server/chroma/src/routes/v1/photo/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::io::Cursor;
use actix_multiresponse::Payload;
use exif::{In, Tag};
use governor::clock::Clock;
use image::{DynamicImage, GenericImageView};
use image::imageops::FilterType;
use image::io::Reader;
use image::{DynamicImage, GenericImageView};
use img_parts::{Bytes, DynImage, ImageEXIF};
use tap::TapFallible;
use time::OffsetDateTime;
Expand All @@ -15,8 +15,8 @@ use webp::Encoder;

use dal::database::{Album, Database, Photo, PhotoQuality};
use dal::storage_engine::Storage;
use proto::photo_respone::Response;
use proto::{CreatePhotoRequest, CreatePhotoResponse};
use proto::photo_respone::Response;

use crate::routes::appdata::WebData;
use crate::routes::authorization::Authorization;
Expand Down
2 changes: 1 addition & 1 deletion server/chroma/src/routes/v1/photo/delete.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use actix_multiresponse::Payload;
use reqwest::StatusCode;

use dal::database::PhotoQuality;
use dal::database::{Album, Photo};
use dal::database::PhotoQuality;
use proto::DeletePhotoRequest;

use crate::routes::appdata::WebData;
Expand Down
4 changes: 2 additions & 2 deletions server/chroma/src/routes/v1/photo/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use serde::Deserialize;
use tap::TapFallible;
use tracing::warn;

use dal::database::Photo;
use dal::DalError;
use proto::photo_respone::Response;
use dal::database::Photo;
use proto::{GetPhotoResponse, PhotoRespone};
use proto::photo_respone::Response;

use crate::routes::appdata::WebData;
use crate::routes::authorization::Authorization;
Expand Down
2 changes: 1 addition & 1 deletion server/chroma/src/routes/v1/photo/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use actix_web::web;
use futures::future::join_all;
use serde::Deserialize;

use dal::database::Photo;
use dal::DalError;
use dal::database::Photo;
use proto::ListPhotoResponse;

use crate::routes::appdata::WebData;
Expand Down
Loading

0 comments on commit bb549da

Please sign in to comment.