Skip to content

Commit

Permalink
minor imp
Browse files Browse the repository at this point in the history
  • Loading branch information
dejankos committed Oct 17, 2020
1 parent 30bf73e commit 908a224
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
5 changes: 2 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use confy::ConfyError;
use rocksdb::{BlockBasedIndexType, BlockBasedOptions, Cache, DBCompactionStyle, Options};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -155,12 +154,12 @@ fn block_based_opts(cfg: &RocksDbConfig) -> BlockBasedOptions {
}
}

pub fn load_db_config(cfg_path: &str) -> Result<DbConfig, ConfyError> {
pub fn load_db_config(cfg_path: &str) -> anyhow::Result<DbConfig> {
let rocks_cfg = confy::load_path(format!("{}/db_config.toml", cfg_path))?;
Ok(DbConfig::new(rocks_cfg))
}

pub fn load_service_config(cfg_path: &str) -> Result<ServiceConfig, ConfyError> {
pub fn load_service_config(cfg_path: &str) -> anyhow::Result<ServiceConfig> {
Ok(confy::load_path(format!(
"{}/service_config.toml",
cfg_path
Expand Down
9 changes: 0 additions & 9 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::fmt;
use std::fmt::{Display, Formatter};
use std::io::ErrorKind;

use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -66,11 +65,3 @@ impl From<anyhow::Error> for ErrWrapper {
ErrWrapper { err }
}
}

pub fn convert_err(any: anyhow::Error) -> std::io::Error {
if let Some(err) = any.downcast::<std::io::Error>().err() {
std::io::Error::new(ErrorKind::Other, err)
} else {
std::io::Error::from(ErrorKind::Other)
}
}
12 changes: 7 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use actix_web::web::Bytes;
use actix_web::{delete, dev, get, http, post, HttpRequest, HttpResponse, ResponseError};
use actix_web::{web, App, HttpServer};
use actix_web_prom::PrometheusMetrics;
use anyhow::anyhow;
use log::LevelFilter;
use serde::Deserialize;
use simplelog::{ConfigBuilder, TermLogger, TerminalMode, ThreadLogMode, WriteLogger};
Expand All @@ -18,7 +19,7 @@ use structopt::StructOpt;
use crate::config::{load_db_config, load_service_config};
use crate::conversion::{convert, current_ms};
use crate::db::DbManager;
use crate::errors::{convert_err, ApiError, ErrWrapper, ErrorCtx};
use crate::errors::{ApiError, ErrWrapper, ErrorCtx};

mod errors;

Expand Down Expand Up @@ -53,11 +54,11 @@ struct PathVal {
}

trait Expiration {
fn calc_expire(&self) -> Response<u128>;
fn calc_expire(&self) -> anyhow::Result<u128>;
}

impl Expiration for HttpRequest {
fn calc_expire(&self) -> Response<u128> {
fn calc_expire(&self) -> anyhow::Result<u128> {
self.headers()
.get(TTL_HEADER)
.map(|h| Ok(current_ms()? + convert(h)?))
Expand Down Expand Up @@ -169,7 +170,7 @@ async fn health() -> HttpResponse {

// main thread will panic! if config can't be initialized
#[actix_web::main]
async fn main() -> std::io::Result<()> {
async fn main() -> anyhow::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=error");
std::env::set_var("RUST_BACKTRACE", "1");

Expand All @@ -183,7 +184,7 @@ async fn main() -> std::io::Result<()> {
let db_cfg = load_db_config(&path_cfg.config_path).expect("Can't load service config");
info!("Loaded db configuration = {:#?}", &db_cfg);

let db_manager = DbManager::new(db_cfg).map_err(convert_err)?;
let db_manager = DbManager::new(db_cfg)?;
let db_manager = web::Data::new(db_manager);

let prometheus = init_prometheus();
Expand All @@ -205,6 +206,7 @@ async fn main() -> std::io::Result<()> {
.shutdown_timeout(60)
.run()
.await
.map_err(|e| anyhow!("Startup failed {}", e))
}

fn init_prometheus() -> PrometheusMetrics {
Expand Down

0 comments on commit 908a224

Please sign in to comment.