mirror of
https://codeberg.org/Mo8it/git-webhook-client
synced 2024-11-23 11:11:36 +00:00
Don't panic!
This commit is contained in:
parent
fb23798d78
commit
5eb910fcb5
5 changed files with 41 additions and 28 deletions
|
@ -1,3 +1,4 @@
|
|||
use anyhow::{Context, Result};
|
||||
use serde::Deserialize;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
|
@ -20,18 +21,17 @@ pub struct Config {
|
|||
}
|
||||
|
||||
impl Config {
|
||||
pub fn new() -> Self {
|
||||
pub fn new() -> Result<Self> {
|
||||
let config_file_var = "GWC_CONFIG_FILE";
|
||||
let config_path = env::var(config_file_var)
|
||||
.unwrap_or_else(|_| panic!("Environment variable {config_file_var} missing!"));
|
||||
.with_context(|| format!("Environment variable {config_file_var} missing!"))?;
|
||||
|
||||
let config_file = File::open(&config_path).unwrap_or_else(|e| {
|
||||
panic!("Can not open the config file at the path {config_path}: {e}")
|
||||
});
|
||||
let config_file = File::open(&config_path)
|
||||
.with_context(|| format!("Can not open the config file at the path {config_path}"))?;
|
||||
let config_reader = BufReader::new(config_file);
|
||||
let config: Self =
|
||||
serde_json::from_reader(config_reader).expect("Can not parse the config file as JSON!");
|
||||
let config: Self = serde_json::from_reader(config_reader)
|
||||
.context("Can not parse the config file as JSON!")?;
|
||||
|
||||
config
|
||||
Ok(config)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,16 +11,16 @@ use crate::schema::hooklog;
|
|||
|
||||
pub type DBPool = Pool<ConnectionManager<SqliteConnection>>;
|
||||
|
||||
pub fn establish_connection_pool() -> DBPool {
|
||||
pub fn establish_connection_pool() -> Result<DBPool> {
|
||||
let database_url_var = "DATABASE_URL";
|
||||
let database_url = env::var(database_url_var)
|
||||
.unwrap_or_else(|_| panic!("Environment variable {database_url_var} missing!"));
|
||||
.with_context(|| format!("Environment variable {database_url_var} missing!"))?;
|
||||
|
||||
let manager = ConnectionManager::<SqliteConnection>::new(&database_url);
|
||||
let manager = ConnectionManager::<SqliteConnection>::new(database_url);
|
||||
|
||||
Pool::builder()
|
||||
.build(manager)
|
||||
.expect("Could not build database connection pool!")
|
||||
.context("Could not build database connection pool!")
|
||||
}
|
||||
|
||||
fn get_conn(pool: &DBPool) -> Result<PooledConnection<ConnectionManager<SqliteConnection>>> {
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
use anyhow::{Context, Result};
|
||||
use simplelog::{ColorChoice, LevelFilter, TermLogger, TerminalMode, WriteLogger};
|
||||
use std::fs::OpenOptions;
|
||||
|
||||
use crate::config;
|
||||
|
||||
pub fn init_logger(config: &config::Config) {
|
||||
pub fn init_logger(config: &config::Config) -> Result<()> {
|
||||
let logger = if cfg!(debug_assertions) {
|
||||
TermLogger::init(
|
||||
LevelFilter::Debug,
|
||||
|
@ -19,11 +20,11 @@ pub fn init_logger(config: &config::Config) {
|
|||
.create(true)
|
||||
.append(true)
|
||||
.open(&config.log_file)
|
||||
.unwrap_or_else(|e| {
|
||||
panic!("Could not open the log file {}: {e}", &config.log_file)
|
||||
}),
|
||||
.with_context(|| format!("Could not open the log file {}", &config.log_file))?,
|
||||
)
|
||||
};
|
||||
|
||||
logger.expect("Could not initialize the logger!");
|
||||
logger.context("Could not initialize the logger!")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
24
src/main.rs
24
src/main.rs
|
@ -7,21 +7,31 @@ mod routes;
|
|||
mod schema;
|
||||
mod states;
|
||||
|
||||
use anyhow::Result;
|
||||
use log::info;
|
||||
use rocket::{Build, Rocket};
|
||||
use rocket_dyn_templates::Template;
|
||||
use std::process;
|
||||
|
||||
#[rocket::launch]
|
||||
fn rocket() -> _ {
|
||||
let config = config::Config::new();
|
||||
fn init() -> Result<Rocket<Build>> {
|
||||
let config = config::Config::new()?;
|
||||
|
||||
logging::init_logger(&config);
|
||||
logging::init_logger(&config)?;
|
||||
|
||||
info!("Starting client");
|
||||
|
||||
rocket::build()
|
||||
Ok(rocket::build()
|
||||
.mount("/", rocket::routes![routes::index])
|
||||
.mount("/api", rocket::routes![routes::trigger])
|
||||
.manage(states::DB::new())
|
||||
.manage(states::DB::new()?)
|
||||
.manage(states::Config::new(config))
|
||||
.attach(Template::fairing())
|
||||
.attach(Template::fairing()))
|
||||
}
|
||||
|
||||
#[rocket::launch]
|
||||
fn rocket() -> _ {
|
||||
init().unwrap_or_else(|e| {
|
||||
eprintln!("{e}");
|
||||
process::exit(1);
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,15 +1,17 @@
|
|||
use crate::config;
|
||||
use crate::db;
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
pub struct DB {
|
||||
pub pool: db::DBPool,
|
||||
}
|
||||
|
||||
impl DB {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
pool: db::establish_connection_pool(),
|
||||
}
|
||||
pub fn new() -> Result<Self> {
|
||||
Ok(Self {
|
||||
pool: db::establish_connection_pool()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue