Skip to content

Commit

Permalink
fix(server): give path to config in config errors (#23)
Browse files Browse the repository at this point in the history
* fix(nix): Fixed missing libiconv on darwin

* feat(server): add note giving path to config
  • Loading branch information
TheButlah authored Nov 22, 2024
1 parent 59c5cef commit 90c37bd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
47 changes: 26 additions & 21 deletions identity-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{io::IsTerminal as _, path::PathBuf};
use std::{
io::IsTerminal as _,
path::{Path, PathBuf},
};

use clap::Parser as _;
use color_eyre::{
Expand All @@ -21,6 +24,27 @@ use identity_server::{

const GOOGLE_CLIENT_ID_DOCS_URL: &str = "https://developers.google.com/identity/gsi/web/guides/get-google-api-clientid#get_your_google_api_client_id";

async fn load_config(cfg_path: &Path) -> Result<Config> {
tokio::fs::read_to_string(cfg_path)
.await
.wrap_err("failed to read config file")
.inspect(|cfg| debug!(contents = cfg, "config file contents"))
.and_then(|cfg| cfg.parse().wrap_err("failed to parse config file"))
.and_then(|cfg: Config| {
cfg.validate().map(|()| cfg).or_else(|err| {
let suggestion = match err {
ValidationError::UnspecifiedDomain => {
"try adding your domain in the `http.tls.domains` list"
}
};
Err(err)
.wrap_err("config file was invalid")
.suggestion(suggestion)
})
})
.with_note(|| format!("Config file path: {}", cfg_path.display()))
}

#[derive(clap::Parser, Debug)]
#[clap(version)]
struct Cli {
Expand All @@ -44,26 +68,7 @@ struct ServeArgs {
impl ServeArgs {
async fn run(self) -> Result<()> {
let cli = self;

let config_file = tokio::fs::read_to_string(&cli.config)
.await
.wrap_err("failed to read config file")
.with_note(|| format!("Config file path: {}", cli.config.display()))?;
debug!(contents = config_file, "config file contents");
let config_file: Config =
config_file.parse().wrap_err("config file was invalid")?;

let validation_result = config_file.validate();
if let Err(ref err) = validation_result {
let suggestion = match err {
ValidationError::UnspecifiedDomain => {
"try adding your domain in the `http.tls.domains` list"
}
};
validation_result
.wrap_err("config file was invalid")
.suggestion(suggestion)?;
}
let config_file = load_config(&cli.config).await?;

let db_pool = {
let DatabaseConfig::Sqlite { ref db_file } = config_file.database;
Expand Down
5 changes: 3 additions & 2 deletions nix/devShells.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ in
# These programs be available to the dev shell
buildInputs = (with pkgs; [
nixpkgs-fmt
]) ++ [
]) ++ pkgs.lib.optional pkgs.stdenv.isDarwin [
pkgs.libiconv
] ++ [
rustToolchain
rustPlatform.bindgenHook
# fenix.packages.${system}.rust-analyzer
];

# Hook the shell to set custom env vars
shellHook = ''
# FOOBAR=1
'';
};
}

0 comments on commit 90c37bd

Please sign in to comment.