clean up .env settings
This commit is contained in:
parent
aed6ba6495
commit
cf46675971
4 changed files with 50 additions and 33 deletions
12
.env.example
Normal file
12
.env.example
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
RUST_LOG=debug
|
||||||
|
ROOT_PATH=/app
|
||||||
|
FRONTEND_HOST=http://127.0.0.1:8080
|
||||||
|
DATABASE_URL=postgresql://postgres:guest@127.0.0.1/postgres
|
||||||
|
HOST=0.0.0.0
|
||||||
|
SERVER_SECRET=guest
|
||||||
|
AUTH__CLIENT_ID=
|
||||||
|
AUTH__CLIENT_SECRET=
|
||||||
|
AUTH__AUTH_URL=
|
||||||
|
AUTH__TOKEN_URL=
|
||||||
|
AUTH__USERINFO_URL=
|
||||||
|
AUTH__LOGOUT_URL=
|
||||||
|
|
@ -16,14 +16,13 @@
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use clap::Parser as _;
|
use clap::Parser as _;
|
||||||
use dotenvy::dotenv;
|
|
||||||
use phono_models::MIGRATOR;
|
use phono_models::MIGRATOR;
|
||||||
use tracing_subscriber::EnvFilter;
|
use tracing_subscriber::EnvFilter;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
app::App,
|
app::App,
|
||||||
cli::{Cli, Commands, serve_command, worker_command},
|
cli::{Cli, Commands, serve_command, worker_command},
|
||||||
settings::Settings,
|
settings::{Settings, load_dotenv},
|
||||||
};
|
};
|
||||||
|
|
||||||
mod app;
|
mod app;
|
||||||
|
|
@ -48,8 +47,8 @@ mod workspace_utils;
|
||||||
/// Run CLI
|
/// Run CLI
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
// Attempt to pre-load .env in case it contains a RUST_LOG variable
|
load_dotenv()?;
|
||||||
dotenv().ok();
|
|
||||||
tracing_subscriber::fmt()
|
tracing_subscriber::fmt()
|
||||||
.with_env_filter(EnvFilter::from_default_env())
|
.with_env_filter(EnvFilter::from_default_env())
|
||||||
.init();
|
.init();
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,15 @@ use axum::extract::FromRef;
|
||||||
use config::{Config, Environment};
|
use config::{Config, Environment};
|
||||||
use dotenvy::dotenv;
|
use dotenvy::dotenv;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use tracing::info;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
use crate::app::App;
|
use crate::app::App;
|
||||||
|
|
||||||
|
/// Runtime application configuration values, typically read from environment
|
||||||
|
/// variables.
|
||||||
|
///
|
||||||
|
/// This may be used with the [`axum::extract::State`] extractor.
|
||||||
#[derive(Clone, Debug, Deserialize)]
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
pub(crate) struct Settings {
|
pub(crate) struct Settings {
|
||||||
/// Prefix under which to nest all routes. If specified, include leading
|
/// Prefix under which to nest all routes. If specified, include leading
|
||||||
|
|
@ -17,11 +22,6 @@ pub(crate) struct Settings {
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub(crate) root_path: String,
|
pub(crate) root_path: String,
|
||||||
|
|
||||||
/// When set to 1, dev features such as the frontend reloader will be
|
|
||||||
/// enabled.
|
|
||||||
#[serde(default)]
|
|
||||||
pub(crate) dev: u8,
|
|
||||||
|
|
||||||
/// postgresql:// URL for Phonograph's application database.
|
/// postgresql:// URL for Phonograph's application database.
|
||||||
pub(crate) database_url: Url,
|
pub(crate) database_url: Url,
|
||||||
|
|
||||||
|
|
@ -58,13 +58,19 @@ fn default_host() -> String {
|
||||||
"127.0.0.1".to_owned()
|
"127.0.0.1".to_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// OAuth2 and session cookie settings.
|
||||||
#[derive(Clone, Debug, Deserialize)]
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
pub(crate) struct AuthSettings {
|
pub(crate) struct AuthSettings {
|
||||||
pub(crate) client_id: String,
|
pub(crate) client_id: String,
|
||||||
|
|
||||||
pub(crate) client_secret: String,
|
pub(crate) client_secret: String,
|
||||||
|
|
||||||
pub(crate) auth_url: String,
|
pub(crate) auth_url: String,
|
||||||
|
|
||||||
pub(crate) token_url: String,
|
pub(crate) token_url: String,
|
||||||
|
|
||||||
pub(crate) userinfo_url: String,
|
pub(crate) userinfo_url: String,
|
||||||
|
|
||||||
pub(crate) logout_url: Option<String>,
|
pub(crate) logout_url: Option<String>,
|
||||||
|
|
||||||
#[serde(default = "default_cookie_name")]
|
#[serde(default = "default_cookie_name")]
|
||||||
|
|
@ -72,28 +78,12 @@ pub(crate) struct AuthSettings {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn default_cookie_name() -> String {
|
fn default_cookie_name() -> String {
|
||||||
"ITM_SESSION".to_string()
|
"PHONO_SESSION".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Settings {
|
impl Settings {
|
||||||
|
/// Load environment variables into a [`Settings`] struct.
|
||||||
pub(crate) fn load() -> Result<Self> {
|
pub(crate) fn load() -> Result<Self> {
|
||||||
match dotenv() {
|
|
||||||
Err(err) => {
|
|
||||||
if err.not_found() {
|
|
||||||
tracing::info!("no .env file found");
|
|
||||||
} else {
|
|
||||||
return Err(err).context("dotenvy error");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(pathbuf) => {
|
|
||||||
tracing::info!(
|
|
||||||
"using env file {}",
|
|
||||||
pathbuf
|
|
||||||
.to_str()
|
|
||||||
.ok_or(anyhow::anyhow!("pathbuf is not valid unicode"))?
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let s = Config::builder()
|
let s = Config::builder()
|
||||||
.add_source(Environment::default().separator("__"))
|
.add_source(Environment::default().separator("__"))
|
||||||
.build()
|
.build()
|
||||||
|
|
@ -107,3 +97,25 @@ impl FromRef<App> for Settings {
|
||||||
state.settings.clone()
|
state.settings.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Attempt to load environment variables from .env file.
|
||||||
|
pub(crate) fn load_dotenv() -> Result<()> {
|
||||||
|
dotenv()
|
||||||
|
.map(|pathbuf| {
|
||||||
|
info!(
|
||||||
|
"using env file {0}",
|
||||||
|
pathbuf
|
||||||
|
.to_str()
|
||||||
|
.ok_or(anyhow::anyhow!("pathbuf is not valid unicode"))?
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
.or_else(|err| {
|
||||||
|
if err.not_found() {
|
||||||
|
info!("no env file loaded");
|
||||||
|
Ok(Ok(()))
|
||||||
|
} else {
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
})?
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,11 +9,5 @@
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
{% block main %}{% endblock %}
|
{% block main %}{% endblock %}
|
||||||
{% if settings.dev != 0 %}
|
|
||||||
<script type="module">
|
|
||||||
import { initDevReloader } from "{{ settings.root_path }}/dev_reloader.mjs";
|
|
||||||
initDevReloader("{{ settings.frontend_host }}{{ settings.root_path }}/__dev-healthz");
|
|
||||||
</script>
|
|
||||||
{% endif %}
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue