diff --git a/src/config.rs b/src/config.rs index 706c325..509da9f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,8 +7,10 @@ use url::Url; #[derive(Deserialize, Debug, Clone)] pub struct GlazedConfig { + #[serde(default = "default_bind_address")] pub bind_address: SocketAddr, - pub public_address: Option, + #[serde(default = "default_public_address")] + pub public_address: Url, pub tiled_client: TiledClientConfig, } impl GlazedConfig { @@ -19,8 +21,8 @@ impl GlazedConfig { pub fn default() -> Self { GlazedConfig { - bind_address: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 3000), - public_address: None, + bind_address: default_bind_address(), + public_address: default_public_address(), tiled_client: TiledClientConfig { address: Url::parse("http://localhost:8000").expect("Static URL is valid"), }, @@ -32,3 +34,13 @@ impl GlazedConfig { pub struct TiledClientConfig { pub address: Url, } + +fn default_bind_address() -> SocketAddr { + SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 3000) +} + +fn default_public_address() -> Url { + "http://localhost:3000" + .parse() + .expect("Static str is valid URL") +} diff --git a/src/handlers.rs b/src/handlers.rs index 705b1d6..1c9c00b 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -23,12 +23,8 @@ pub async fn graphql_handler( .into() } -pub async fn graphiql_handler(graphql_endpoint: Option) -> impl IntoResponse { - Html( - GraphiQLSource::build() - .endpoint(graphql_endpoint.as_deref().unwrap_or("/graphql")) - .finish(), - ) +pub async fn graphiql_handler(graphql_endpoint: String) -> impl IntoResponse { + Html(GraphiQLSource::build().endpoint(&graphql_endpoint).finish()) } pub async fn download_handler( diff --git a/src/main.rs b/src/main.rs index 4d8bcfb..8dfaecf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,19 +50,25 @@ async fn main() -> Result<(), Box> { pub struct RootAddress(Url); async fn serve(config: GlazedConfig) -> Result<(), Box> { - let client = TiledClient::new(config.tiled_client.address); - let public_address = config - .public_address - .clone() - .unwrap_or_else(|| Url::parse(&format!("http://{}", config.bind_address)).unwrap()); + let client = TiledClient::new(config.tiled_client.address.clone()); let schema = Schema::build(TiledQuery, EmptyMutation, EmptySubscription) - .data(RootAddress(public_address)) + .data(RootAddress(config.public_address.clone())) .data(client.clone()) .finish(); - let graphql_endpoint = config - .public_address - .map(|u| u.join("graphql").unwrap().to_string()); + let graphql_endpoint = { + let mut addr = config.public_address.clone(); + addr.path_segments_mut() + .expect("Public address is not valid") + .pop_if_empty() + .push("graphql"); + addr.to_string() + }; + info!( + "Redirecting traffic to public address: {}", + config.public_address + ); + info!("Public graphql endpoint available at {}", graphql_endpoint); let app = Router::new() .route("/graphql", post(graphql_handler).get(graphql_get_warning))