@@ -22,7 +22,7 @@ use crate::value_notifier::{Computed, ValueNotifier};
22
22
struct InternalState {
23
23
data_dir : DataDir ,
24
24
public_dir : Option < PathBuf > ,
25
- address : String ,
25
+ site_url : Computed < url :: Url , Config > ,
26
26
dev : bool ,
27
27
demo : bool ,
28
28
@@ -74,6 +74,8 @@ impl AppState {
74
74
pub ( crate ) fn new ( args : AppStateArgs ) -> Self {
75
75
let config = ValueNotifier :: new ( args. config ) ;
76
76
77
+ let site_url = Computed :: new ( & config, move |c| build_site_url ( c, & args. address ) ) ;
78
+
77
79
let record_apis = {
78
80
let schema_metadata_clone = args. schema_metadata . clone ( ) ;
79
81
let conn_clone = args. conn . clone ( ) ;
@@ -109,7 +111,7 @@ impl AppState {
109
111
state : Arc :: new ( InternalState {
110
112
data_dir : args. data_dir ,
111
113
public_dir : args. public_dir ,
112
- address : args . address ,
114
+ site_url ,
113
115
dev : args. dev ,
114
116
demo : args. demo ,
115
117
oauth : Computed :: new ( & config, |c| {
@@ -233,11 +235,8 @@ impl AppState {
233
235
. collect ( ) ;
234
236
}
235
237
236
- // TODO: Turn this into a parsed url::Url.
237
- pub fn site_url ( & self ) -> String {
238
- self
239
- . access_config ( |c| c. server . site_url . clone ( ) )
240
- . unwrap_or_else ( || format ! ( "http://{}" , self . state. address) )
238
+ pub fn site_url ( & self ) -> Arc < url:: Url > {
239
+ return self . state . site_url . load ( ) . clone ( ) ;
241
240
}
242
241
243
242
pub ( crate ) fn mailer ( & self ) -> Arc < Mailer > {
@@ -344,6 +343,7 @@ pub async fn test_state(options: Option<TestStateOptions>) -> anyhow::Result<App
344
343
// Construct a fabricated config for tests and make sure it's valid.
345
344
let mut config = Config :: new_with_custom_defaults ( ) ;
346
345
346
+ config. server . site_url = Some ( "https://test.org" . to_string ( ) ) ;
347
347
config. email . smtp_host = Some ( "smtp.test.org" . to_string ( ) ) ;
348
348
config. email . smtp_port = Some ( 587 ) ;
349
349
config. email . smtp_username = Some ( "user" . to_string ( ) ) ;
@@ -429,11 +429,12 @@ pub async fn test_state(options: Option<TestStateOptions>) -> anyhow::Result<App
429
429
} ) ;
430
430
}
431
431
432
+ let address = "localhost:1234" ;
432
433
return Ok ( AppState {
433
434
state : Arc :: new ( InternalState {
434
435
data_dir,
435
436
public_dir : None ,
436
- address : "localhost:1234" . to_string ( ) ,
437
+ site_url : Computed :: new ( & config , move |c| build_site_url ( c , address ) ) ,
437
438
dev : true ,
438
439
demo : false ,
439
440
oauth : Computed :: new ( & config, |c| {
@@ -537,3 +538,20 @@ pub(crate) fn build_objectstore(
537
538
object_store:: local:: LocalFileSystem :: new_with_prefix ( data_dir. uploads_path ( ) ) ?,
538
539
) ) ;
539
540
}
541
+
542
+ fn build_site_url ( c : & Config , address : & str ) -> url:: Url {
543
+ let fallback = url:: Url :: parse ( & format ! ( "http://{address}" ) ) . expect ( "startup" ) ;
544
+
545
+ if let Some ( ref site_url) = c. server . site_url {
546
+ match url:: Url :: parse ( site_url) {
547
+ Ok ( url) => {
548
+ return url;
549
+ }
550
+ Err ( err) => {
551
+ error ! ( "Failed to parse site_url '{site_url}' from config: {err}" ) ;
552
+ }
553
+ } ;
554
+ }
555
+
556
+ return fallback;
557
+ }
0 commit comments