Removed anyhow dependency
This commit is contained in:
parent
98d00d261d
commit
c57583d1d4
4 changed files with 45 additions and 24 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -327,12 +327,6 @@ dependencies = [
|
||||||
"alloc-no-stdlib",
|
"alloc-no-stdlib",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "anyhow"
|
|
||||||
version = "1.0.66"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ape"
|
name = "ape"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
|
@ -1457,7 +1451,6 @@ dependencies = [
|
||||||
"actix-test",
|
"actix-test",
|
||||||
"actix-web",
|
"actix-web",
|
||||||
"actix-web-httpauth",
|
"actix-web-httpauth",
|
||||||
"anyhow",
|
|
||||||
"ape",
|
"ape",
|
||||||
"base64",
|
"base64",
|
||||||
"branca",
|
"branca",
|
||||||
|
|
|
@ -14,7 +14,6 @@ ui = ["native-windows-gui", "native-windows-derive"]
|
||||||
actix-files = { version = "0.6" }
|
actix-files = { version = "0.6" }
|
||||||
actix-web = { version = "4" }
|
actix-web = { version = "4" }
|
||||||
actix-web-httpauth = { version = "0.8" }
|
actix-web-httpauth = { version = "0.8" }
|
||||||
anyhow = "1.0.66"
|
|
||||||
ape = "0.4.0"
|
ape = "0.4.0"
|
||||||
base64 = "0.13"
|
base64 = "0.13"
|
||||||
branca = "0.10.1"
|
branca = "0.10.1"
|
||||||
|
|
56
src/main.rs
56
src/main.rs
|
@ -6,13 +6,12 @@ extern crate diesel;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate diesel_migrations;
|
extern crate diesel_migrations;
|
||||||
|
|
||||||
use anyhow::Result;
|
|
||||||
use log::info;
|
use log::info;
|
||||||
use simplelog::{
|
use simplelog::{
|
||||||
ColorChoice, CombinedLogger, LevelFilter, SharedLogger, TermLogger, TerminalMode, WriteLogger,
|
ColorChoice, CombinedLogger, LevelFilter, SharedLogger, TermLogger, TerminalMode, WriteLogger,
|
||||||
};
|
};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::Path;
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
mod app;
|
mod app;
|
||||||
mod db;
|
mod db;
|
||||||
|
@ -24,30 +23,57 @@ mod test;
|
||||||
mod ui;
|
mod ui;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
|
#[derive(thiserror::Error, Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
#[error(transparent)]
|
||||||
|
App(#[from] app::Error),
|
||||||
|
#[error("Could not parse command line arguments:\n\n{0}")]
|
||||||
|
CliArgsParsing(getopts::Fail),
|
||||||
|
#[cfg(unix)]
|
||||||
|
#[error("Failed to turn polaris process into a daemon:\n\n{0}")]
|
||||||
|
Daemonize(daemonize::DaemonizeError),
|
||||||
|
#[error("Could not create log directory `{0}`:\n\n{1}")]
|
||||||
|
LogDirectoryCreationError(PathBuf, std::io::Error),
|
||||||
|
#[error("Could not create log file `{0}`:\n\n{1}")]
|
||||||
|
LogFileCreationError(PathBuf, std::io::Error),
|
||||||
|
#[error("Could not initialize log system:\n\n{0}")]
|
||||||
|
LogInitialization(log::SetLoggerError),
|
||||||
|
#[cfg(unix)]
|
||||||
|
#[error("Could not create pid directory `{0}`:\n\n{1}")]
|
||||||
|
PidDirectoryCreationError(PathBuf, std::io::Error),
|
||||||
|
#[cfg(unix)]
|
||||||
|
#[error("Could not notify systemd of initialization success:\n\n{0}")]
|
||||||
|
SystemDNotify(std::io::Error),
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn daemonize<T: AsRef<Path>>(foreground: bool, pid_file_path: T) -> Result<()> {
|
fn daemonize<T: AsRef<Path>>(foreground: bool, pid_file_path: T) -> Result<(), Error> {
|
||||||
if foreground {
|
if foreground {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
if let Some(parent) = pid_file_path.as_ref().parent() {
|
if let Some(parent) = pid_file_path.as_ref().parent() {
|
||||||
fs::create_dir_all(parent)?;
|
fs::create_dir_all(parent)
|
||||||
|
.map_err(|e| Error::PidDirectoryCreationError(parent.to_owned(), e))?;
|
||||||
}
|
}
|
||||||
let daemonize = daemonize::Daemonize::new()
|
let daemonize = daemonize::Daemonize::new()
|
||||||
.pid_file(pid_file_path.as_ref())
|
.pid_file(pid_file_path.as_ref())
|
||||||
.working_directory(".");
|
.working_directory(".");
|
||||||
daemonize.start()?;
|
daemonize.start().map_err(Error::Daemonize)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn notify_ready() -> Result<()> {
|
fn notify_ready() -> Result<(), Error> {
|
||||||
if let Ok(true) = sd_notify::booted() {
|
if let Ok(true) = sd_notify::booted() {
|
||||||
sd_notify::notify(true, &[sd_notify::NotifyState::Ready])?;
|
sd_notify::notify(true, &[sd_notify::NotifyState::Ready]).map_err(Error::SystemDNotify);
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_logging<T: AsRef<Path>>(log_level: LevelFilter, log_file_path: &Option<T>) -> Result<()> {
|
fn init_logging<T: AsRef<Path>>(
|
||||||
|
log_level: LevelFilter,
|
||||||
|
log_file_path: &Option<T>,
|
||||||
|
) -> Result<(), Error> {
|
||||||
let log_config = simplelog::ConfigBuilder::new()
|
let log_config = simplelog::ConfigBuilder::new()
|
||||||
.set_location_level(LevelFilter::Error)
|
.set_location_level(LevelFilter::Error)
|
||||||
.build();
|
.build();
|
||||||
|
@ -61,25 +87,29 @@ fn init_logging<T: AsRef<Path>>(log_level: LevelFilter, log_file_path: &Option<T
|
||||||
|
|
||||||
if let Some(path) = log_file_path {
|
if let Some(path) = log_file_path {
|
||||||
if let Some(parent) = path.as_ref().parent() {
|
if let Some(parent) = path.as_ref().parent() {
|
||||||
fs::create_dir_all(parent)?;
|
fs::create_dir_all(parent)
|
||||||
|
.map_err(|e| Error::LogDirectoryCreationError(parent.to_owned(), e))?;
|
||||||
}
|
}
|
||||||
loggers.push(WriteLogger::new(
|
loggers.push(WriteLogger::new(
|
||||||
log_level,
|
log_level,
|
||||||
log_config,
|
log_config,
|
||||||
fs::File::create(path)?,
|
fs::File::create(path)
|
||||||
|
.map_err(|e| Error::LogFileCreationError(path.as_ref().to_owned(), e))?,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
CombinedLogger::init(loggers)?;
|
CombinedLogger::init(loggers).map_err(Error::LogInitialization)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<(), Error> {
|
||||||
// Parse CLI options
|
// Parse CLI options
|
||||||
let args: Vec<String> = std::env::args().collect();
|
let args: Vec<String> = std::env::args().collect();
|
||||||
let options_manager = options::Manager::new();
|
let options_manager = options::Manager::new();
|
||||||
let cli_options = options_manager.parse(&args[1..])?;
|
let cli_options = options_manager
|
||||||
|
.parse(&args[1..])
|
||||||
|
.map_err(Error::CliArgsParsing)?;
|
||||||
|
|
||||||
if cli_options.show_help {
|
if cli_options.show_help {
|
||||||
let program = args[0].clone();
|
let program = args[0].clone();
|
||||||
|
|
|
@ -42,7 +42,7 @@ pub fn make_config(app: App) -> impl FnOnce(&mut ServiceConfig) + Clone {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(app: App) -> anyhow::Result<()> {
|
pub fn run(app: App) -> Result<(), std::io::Error> {
|
||||||
let address = ("0.0.0.0", app.port);
|
let address = ("0.0.0.0", app.port);
|
||||||
System::new().block_on(
|
System::new().block_on(
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
|
@ -58,6 +58,5 @@ pub fn run(app: App) -> anyhow::Result<()> {
|
||||||
e
|
e
|
||||||
})?
|
})?
|
||||||
.run(),
|
.run(),
|
||||||
)?;
|
)
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue