mirror of
https://codeberg.org/Mo8it/git-webhook-client
synced 2024-11-21 11:06:32 +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 serde::Deserialize;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
@ -20,18 +21,17 @@ pub struct Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Result<Self> {
|
||||||
let config_file_var = "GWC_CONFIG_FILE";
|
let config_file_var = "GWC_CONFIG_FILE";
|
||||||
let config_path = env::var(config_file_var)
|
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| {
|
let config_file = File::open(&config_path)
|
||||||
panic!("Can not open the config file at the path {config_path}: {e}")
|
.with_context(|| format!("Can not open the config file at the path {config_path}"))?;
|
||||||
});
|
|
||||||
let config_reader = BufReader::new(config_file);
|
let config_reader = BufReader::new(config_file);
|
||||||
let config: Self =
|
let config: Self = serde_json::from_reader(config_reader)
|
||||||
serde_json::from_reader(config_reader).expect("Can not parse the config file as JSON!");
|
.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 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_var = "DATABASE_URL";
|
||||||
let database_url = env::var(database_url_var)
|
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()
|
Pool::builder()
|
||||||
.build(manager)
|
.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>>> {
|
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 simplelog::{ColorChoice, LevelFilter, TermLogger, TerminalMode, WriteLogger};
|
||||||
use std::fs::OpenOptions;
|
use std::fs::OpenOptions;
|
||||||
|
|
||||||
use crate::config;
|
use crate::config;
|
||||||
|
|
||||||
pub fn init_logger(config: &config::Config) {
|
pub fn init_logger(config: &config::Config) -> Result<()> {
|
||||||
let logger = if cfg!(debug_assertions) {
|
let logger = if cfg!(debug_assertions) {
|
||||||
TermLogger::init(
|
TermLogger::init(
|
||||||
LevelFilter::Debug,
|
LevelFilter::Debug,
|
||||||
|
@ -19,11 +20,11 @@ pub fn init_logger(config: &config::Config) {
|
||||||
.create(true)
|
.create(true)
|
||||||
.append(true)
|
.append(true)
|
||||||
.open(&config.log_file)
|
.open(&config.log_file)
|
||||||
.unwrap_or_else(|e| {
|
.with_context(|| format!("Could not open the log file {}", &config.log_file))?,
|
||||||
panic!("Could not open the log file {}: {e}", &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 schema;
|
||||||
mod states;
|
mod states;
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
use log::info;
|
use log::info;
|
||||||
|
use rocket::{Build, Rocket};
|
||||||
use rocket_dyn_templates::Template;
|
use rocket_dyn_templates::Template;
|
||||||
|
use std::process;
|
||||||
|
|
||||||
#[rocket::launch]
|
fn init() -> Result<Rocket<Build>> {
|
||||||
fn rocket() -> _ {
|
let config = config::Config::new()?;
|
||||||
let config = config::Config::new();
|
|
||||||
|
|
||||||
logging::init_logger(&config);
|
logging::init_logger(&config)?;
|
||||||
|
|
||||||
info!("Starting client");
|
info!("Starting client");
|
||||||
|
|
||||||
rocket::build()
|
Ok(rocket::build()
|
||||||
.mount("/", rocket::routes![routes::index])
|
.mount("/", rocket::routes![routes::index])
|
||||||
.mount("/api", rocket::routes![routes::trigger])
|
.mount("/api", rocket::routes![routes::trigger])
|
||||||
.manage(states::DB::new())
|
.manage(states::DB::new()?)
|
||||||
.manage(states::Config::new(config))
|
.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::config;
|
||||||
use crate::db;
|
use crate::db;
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
|
|
||||||
pub struct DB {
|
pub struct DB {
|
||||||
pub pool: db::DBPool,
|
pub pool: db::DBPool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DB {
|
impl DB {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Result<Self> {
|
||||||
Self {
|
Ok(Self {
|
||||||
pool: db::establish_connection_pool(),
|
pool: db::establish_connection_pool()?,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue