Store entire Config struct on collab AppState

This commit is contained in:
Max Brunsfeld 2022-10-18 13:02:30 -07:00
parent 38bdf7ad92
commit 5e57a33df7
4 changed files with 13 additions and 13 deletions

View file

@ -76,7 +76,7 @@ pub async fn validate_api_token<B>(req: Request<B>, next: Next<B>) -> impl IntoR
let state = req.extensions().get::<Arc<AppState>>().unwrap(); let state = req.extensions().get::<Arc<AppState>>().unwrap();
if token != state.api_token { if token != state.config.api_token {
Err(Error::Http( Err(Error::Http(
StatusCode::UNAUTHORIZED, StatusCode::UNAUTHORIZED,
"invalid authorization token".to_string(), "invalid authorization token".to_string(),

View file

@ -6357,8 +6357,7 @@ impl TestServer {
async fn build_app_state(test_db: &TestDb) -> Arc<AppState> { async fn build_app_state(test_db: &TestDb) -> Arc<AppState> {
Arc::new(AppState { Arc::new(AppState {
db: test_db.db().clone(), db: test_db.db().clone(),
api_token: Default::default(), config: Default::default(),
invite_link_prefix: Default::default(),
}) })
} }

View file

@ -34,17 +34,15 @@ pub struct Config {
pub struct AppState { pub struct AppState {
db: Arc<dyn Db>, db: Arc<dyn Db>,
api_token: String, config: Config,
invite_link_prefix: String,
} }
impl AppState { impl AppState {
async fn new(config: &Config) -> Result<Arc<Self>> { async fn new(config: Config) -> Result<Arc<Self>> {
let db = PostgresDb::new(&config.database_url, 5).await?; let db = PostgresDb::new(&config.database_url, 5).await?;
let this = Self { let this = Self {
db: Arc::new(db), db: Arc::new(db),
api_token: config.api_token.clone(), config,
invite_link_prefix: config.invite_link_prefix.clone(),
}; };
Ok(Arc::new(this)) Ok(Arc::new(this))
} }
@ -61,9 +59,9 @@ async fn main() -> Result<()> {
let config = envy::from_env::<Config>().expect("error loading config"); let config = envy::from_env::<Config>().expect("error loading config");
init_tracing(&config); init_tracing(&config);
let state = AppState::new(&config).await?; let state = AppState::new(config).await?;
let listener = TcpListener::bind(&format!("0.0.0.0:{}", config.http_port)) let listener = TcpListener::bind(&format!("0.0.0.0:{}", state.config.http_port))
.expect("failed to bind TCP listener"); .expect("failed to bind TCP listener");
let rpc_server = rpc::Server::new(state.clone(), None); let rpc_server = rpc::Server::new(state.clone(), None);

View file

@ -397,7 +397,7 @@ impl Server {
if let Some((code, count)) = invite_code { if let Some((code, count)) = invite_code {
this.peer.send(connection_id, proto::UpdateInviteInfo { this.peer.send(connection_id, proto::UpdateInviteInfo {
url: format!("{}{}", this.app_state.invite_link_prefix, code), url: format!("{}{}", this.app_state.config.invite_link_prefix, code),
count, count,
})?; })?;
} }
@ -561,7 +561,7 @@ impl Server {
self.peer.send( self.peer.send(
connection_id, connection_id,
proto::UpdateInviteInfo { proto::UpdateInviteInfo {
url: format!("{}{}", self.app_state.invite_link_prefix, &code), url: format!("{}{}", self.app_state.config.invite_link_prefix, &code),
count: user.invite_count as u32, count: user.invite_count as u32,
}, },
)?; )?;
@ -579,7 +579,10 @@ impl Server {
self.peer.send( self.peer.send(
connection_id, connection_id,
proto::UpdateInviteInfo { proto::UpdateInviteInfo {
url: format!("{}{}", self.app_state.invite_link_prefix, invite_code), url: format!(
"{}{}",
self.app_state.config.invite_link_prefix, invite_code
),
count: user.invite_count as u32, count: user.invite_count as u32,
}, },
)?; )?;