Skip to content

Commit

Permalink
rename ApiConfig to Config
Browse files Browse the repository at this point in the history
  • Loading branch information
jbr committed Jul 15, 2023
1 parent 68ff772 commit e03a8ba
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/bin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use divviup_api::{ApiConfig, DivviupApi, Queue};
use divviup_api::{Config, DivviupApi, Queue};

use trillium_http::Stopper;
use trillium_tokio::CloneCounterObserver;
Expand All @@ -7,7 +7,7 @@ use trillium_tokio::CloneCounterObserver;
async fn main() {
env_logger::init();

let config = match ApiConfig::from_env() {
let config = match Config::from_env() {
Ok(config) => config,
Err(e) => panic!("{e}"),
};
Expand Down
4 changes: 2 additions & 2 deletions src/clients/auth0_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use url::Url;

use crate::{
clients::{ClientConnExt, ClientError, PostmarkClient},
ApiConfig,
Config,
};

#[derive(Debug, Clone)]
Expand All @@ -42,7 +42,7 @@ fn generate_password() -> String {
}

impl Auth0Client {
pub fn new(config: &ApiConfig) -> Self {
pub fn new(config: &Config) -> Self {
Self {
token: Arc::new(RwLock::new(None)),
client: config.client.clone(),
Expand Down
4 changes: 2 additions & 2 deletions src/clients/postmark_client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
clients::{ClientConnExt, ClientError},
ApiConfig,
Config,
};
use email_address::EmailAddress;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
Expand All @@ -26,7 +26,7 @@ impl FromConn for PostmarkClient {
}

impl PostmarkClient {
pub fn new(config: &ApiConfig) -> Self {
pub fn new(config: &Config) -> Self {
Self {
token: config.postmark_token.clone(),
client: config.client.clone(),
Expand Down
22 changes: 11 additions & 11 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use url::Url;
const POSTMARK_URL: &str = "https://api.postmarkapp.com";

#[derive(Debug, Clone)]
pub struct ApiConfig {
pub struct Config {
pub session_secret: String,
pub api_url: Url,
pub app_url: Url,
Expand All @@ -28,7 +28,7 @@ pub struct ApiConfig {
}

#[derive(Debug, Error, Clone, Copy)]
pub enum ApiConfigError {
pub enum ConfigError {
#[error("environment variable `{0}` was not found.")]
MissingEnvVar(&'static str),

Expand All @@ -39,23 +39,23 @@ pub enum ApiConfigError {
InvalidUrl(#[from] url::ParseError),
}

fn var<T: FromStr>(name: &'static str) -> Result<T, ApiConfigError> {
fn var<T: FromStr>(name: &'static str) -> Result<T, ConfigError> {
let format = std::any::type_name::<T>();
std::env::var(name)
.map_err(|error| match error {
VarError::NotPresent => ApiConfigError::MissingEnvVar(name),
VarError::NotUnicode(_) => ApiConfigError::InvalidEnvVarFormat(name, format),
VarError::NotPresent => ConfigError::MissingEnvVar(name),
VarError::NotUnicode(_) => ConfigError::InvalidEnvVarFormat(name, format),
})
.and_then(|input| {
input
.parse()
.map_err(|_| ApiConfigError::InvalidEnvVarFormat(name, format))
.map_err(|_| ConfigError::InvalidEnvVarFormat(name, format))
})
}

fn var_optional<T: FromStr>(name: &'static str, default: T) -> Result<T, ApiConfigError> {
fn var_optional<T: FromStr>(name: &'static str, default: T) -> Result<T, ConfigError> {
match var(name) {
Err(ApiConfigError::MissingEnvVar(_)) => Ok(default),
Err(ConfigError::MissingEnvVar(_)) => Ok(default),
other => other,
}
}
Expand All @@ -73,8 +73,8 @@ fn build_client() -> trillium_client::Client {
))
}

impl ApiConfig {
pub fn from_env() -> Result<Self, ApiConfigError> {
impl Config {
pub fn from_env() -> Result<Self, ConfigError> {
Ok(Self {
database_url: var("DATABASE_URL")?,
session_secret: var("SESSION_SECRET")?,
Expand Down Expand Up @@ -107,7 +107,7 @@ impl ApiConfig {
}
}

impl AsRef<Client> for ApiConfig {
impl AsRef<Client> for Config {
fn as_ref(&self) -> &Client {
&self.client
}
Expand Down
10 changes: 5 additions & 5 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub(crate) mod oauth2;
pub(crate) mod origin_router;
pub(crate) mod session_store;

use crate::{routes, ApiConfig, Db};
use crate::{routes, Config, Db};

use cors::cors_headers;
use error::ErrorHandler;
Expand Down Expand Up @@ -40,7 +40,7 @@ pub struct DivviupApi {
#[handler(except = init)]
handler: Box<dyn Handler>,
db: Db,
config: Arc<ApiConfig>,
config: Arc<Config>,
}

impl DivviupApi {
Expand All @@ -53,7 +53,7 @@ impl DivviupApi {
self.handler.init(info).await
}

pub async fn new(config: ApiConfig) -> Self {
pub async fn new(config: Config) -> Self {
let config = Arc::new(config);
let db = Db::connect(config.database_url.as_ref()).await;
Self {
Expand All @@ -77,7 +77,7 @@ impl DivviupApi {
&self.db
}

pub fn config(&self) -> &ApiConfig {
pub fn config(&self) -> &Config {
&self.config
}
}
Expand All @@ -88,7 +88,7 @@ impl AsRef<Db> for DivviupApi {
}
}

fn api(db: &Db, config: &ApiConfig) -> impl Handler {
fn api(db: &Db, config: &Config) -> impl Handler {
(
compression(),
#[cfg(feature = "integration-testing")]
Expand Down
4 changes: 2 additions & 2 deletions src/handler/assets.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{handler::origin_router, ApiConfig};
use crate::{handler::origin_router, Config};
use std::time::Duration;
use trillium::{
Conn, Handler,
Expand All @@ -11,7 +11,7 @@ use url::Url;

const ONE_YEAR: Duration = Duration::from_secs(60 * 60 * 24 * 365);

pub fn static_assets(config: &ApiConfig) -> impl Handler {
pub fn static_assets(config: &Config) -> impl Handler {
origin_router().with_handler(
config.app_url.as_ref(),
ReactApp {
Expand Down
6 changes: 3 additions & 3 deletions src/handler/cors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::ApiConfig;
use crate::Config;
use trillium::{
Conn, Handler,
KnownHeaderName::{
Expand Down Expand Up @@ -43,13 +43,13 @@ impl Handler for CorsHeaders {
}

impl CorsHeaders {
pub fn new(config: &ApiConfig) -> Self {
pub fn new(config: &Config) -> Self {
let mut origin = config.app_url.to_string();
origin.pop();
Self { origin }
}
}

pub fn cors_headers(config: &ApiConfig) -> impl Handler {
pub fn cors_headers(config: &Config) -> impl Handler {
CorsHeaders::new(config)
}
6 changes: 3 additions & 3 deletions src/handler/misc.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{ApiConfig, PermissionsActor, User};
use crate::{Config, PermissionsActor, User};
use trillium::{Conn, Handler, Status};
use trillium_api::{api, Halt};
use trillium_redirect::Redirect;
use trillium_sessions::SessionConnExt;

/// note(jbr): most of these need to find better places to live
pub fn redirect_if_logged_in(config: &ApiConfig) -> impl Handler {
pub fn redirect_if_logged_in(config: &Config) -> impl Handler {
let app_url = config.app_url.to_string();
api(move |_: &mut Conn, user: Option<User>| {
let app_url = app_url.clone();
Expand All @@ -19,7 +19,7 @@ pub fn redirect_if_logged_in(config: &ApiConfig) -> impl Handler {
})
}

pub fn logout_from_auth0(config: &ApiConfig) -> impl Handler {
pub fn logout_from_auth0(config: &Config) -> impl Handler {
let mut logout_url = config.auth_url.join("/v2/logout").unwrap();

logout_url.query_pairs_mut().extend_pairs([
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod routes;
pub mod telemetry;
mod user;

pub use config::{ApiConfig, ApiConfigError};
pub use config::{Config, ConfigError};
pub use db::Db;
pub use handler::{DivviupApi, Error};
pub use permissions::{Permissions, PermissionsActor};
Expand Down
6 changes: 3 additions & 3 deletions src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub use job::*;

use crate::{
entity::queue::{ActiveModel, Column, Entity, Model},
ApiConfig, Db, DivviupApi,
Config, Db, DivviupApi,
};
use sea_orm::{
sea_query::{self, all, Expr},
Expand All @@ -27,7 +27,7 @@ pub struct Queue {
job_state: Arc<SharedJobState>,
}
/*
These configuration variables may eventually be useful to put on ApiConfig
These configuration variables may eventually be useful to put on Config
*/
const MAX_RETRY: i32 = 5;
const QUEUE_CHECK_INTERVAL: Range<u64> = 60_000..120_000;
Expand All @@ -53,7 +53,7 @@ impl From<&DivviupApi> for Queue {
}

impl Queue {
pub fn new(db: &Db, config: &ApiConfig) -> Self {
pub fn new(db: &Db, config: &Config) -> Self {
Self {
observer: Default::default(),
db: db.clone(),
Expand Down
6 changes: 3 additions & 3 deletions src/queue/job.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
clients::{Auth0Client, ClientError, PostmarkClient},
entity::Membership,
ApiConfig,
Config,
};
use sea_orm::{ActiveModelTrait, ConnectionTrait, DbErr};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -78,8 +78,8 @@ pub struct SharedJobState {
pub auth0_client: Auth0Client,
pub postmark_client: PostmarkClient,
}
impl From<&ApiConfig> for SharedJobState {
fn from(config: &ApiConfig) -> Self {
impl From<&Config> for SharedJobState {
fn from(config: &Config) -> Self {
Self {
auth0_client: Auth0Client::new(config),
postmark_client: PostmarkClient::new(config),
Expand Down
8 changes: 4 additions & 4 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
oauth2::{self, OauthClient},
redirect_if_logged_in, ReplaceMimeTypes,
},
ApiConfig,
Config,
};
pub use health_check::health_check;
use trillium::{
Expand All @@ -25,7 +25,7 @@ use trillium_api::api;
use trillium_redirect::redirect;
use trillium_router::router;

pub fn routes(config: &ApiConfig) -> impl Handler {
pub fn routes(config: &Config) -> impl Handler {
let oauth2_client = OauthClient::new(&config.oauth_config());
let auth0_client = Auth0Client::new(config);

Expand Down Expand Up @@ -54,7 +54,7 @@ pub fn routes(config: &ApiConfig) -> impl Handler {
)
}

fn api_routes(config: &ApiConfig) -> impl Handler {
fn api_routes(config: &Config) -> impl Handler {
(
ReplaceMimeTypes,
api(actor_required),
Expand Down Expand Up @@ -88,7 +88,7 @@ fn api_routes(config: &ApiConfig) -> impl Handler {
)
}

fn accounts_routes(config: &ApiConfig) -> impl Handler {
fn accounts_routes(config: &Config) -> impl Handler {
router()
.patch("/", api(accounts::update))
.get("/", api(accounts::show))
Expand Down
6 changes: 3 additions & 3 deletions tests/harness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use base64::{engine::general_purpose::STANDARD, Engine};
use divviup_api::{
clients::aggregator_client::api_types::{Encode, HpkeConfig},
entity::queue,
ApiConfig, Db,
Config, Db,
};
use serde::{de::DeserializeOwned, Serialize};
use std::{error::Error, future::Future};
Expand Down Expand Up @@ -68,8 +68,8 @@ async fn set_up_schema(db: &Db) {
set_up_schema_for(&schema, db, ApiTokens).await;
}

pub fn config(api_mocks: impl Handler) -> ApiConfig {
ApiConfig {
pub fn config(api_mocks: impl Handler) -> Config {
Config {
session_secret: "x".repeat(32),
api_url: "https://api.example".parse().unwrap(),
app_url: "https://app.example".parse().unwrap(),
Expand Down

0 comments on commit e03a8ba

Please sign in to comment.