Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Url>,
#[serde(default = "default_public_address")]
pub public_address: Url,
pub tiled_client: TiledClientConfig,
}
impl GlazedConfig {
Expand All @@ -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"),
},
Expand All @@ -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")
}
8 changes: 2 additions & 6 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@ pub async fn graphql_handler(
.into()
}

pub async fn graphiql_handler(graphql_endpoint: Option<String>) -> 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(
Expand Down
24 changes: 15 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,25 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
pub struct RootAddress(Url);

async fn serve(config: GlazedConfig) -> Result<(), Box<dyn std::error::Error>> {
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))
Expand Down
Loading