Skip to content
Merged
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
15 changes: 8 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ futures = "0.3.31"
mime = "0.3.17"
tower-http = { version = "0.6.6", features = ["trace", "cors", "timeout"] }
tower = { version = "0.5.2", features = ["limit"] }
url = "2.5.7"
async-trait = "0.1.89"
async-stream = "0.3.6"
uuid = { version = "1.18.1", features = ["v4"] }
Expand Down
4 changes: 4 additions & 0 deletions docs/topics/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ server:
max-upload-size: 50000000
request-timeout: 60000
graceful-shutdown: true
base-path: /
```

<deflist>
Expand All @@ -145,6 +146,9 @@ server:
Shutdowns will take no longer than the request timeout configured by <code>server.http.request.timeout</code>.
If disabled, the process is stopped immediately, which will potentially lead to incomplete responses for outstanding requests.
</def>
<def title="server.http.base-path" id="server.http.base-path">
Sets the base path for all HTTP endpoints.
</def>
</deflist>

## DIMSE Server Config
Expand Down
2 changes: 1 addition & 1 deletion src/api/aets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use axum::response::IntoResponse;
use axum::routing::get;
use axum::{Json, Router};

pub fn api() -> Router<AppState> {
pub fn routes() -> Router<AppState> {
Router::new()
.route("/aets", get(all_aets))
.route("/aets/{aet}", get(aet_health))
Expand Down
16 changes: 16 additions & 0 deletions src/api/home.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::AppState;
use axum::response::IntoResponse;
use axum::routing::get;
use axum::Router;

pub fn routes() -> Router<AppState> {
Router::new().route("/", get(index))
}

// TODO: Return HTML page for a quick user-friendly overview
async fn index() -> impl IntoResponse {
format!(
"This server is running DICOM-RST (v{})",
env!("CARGO_PKG_VERSION")
)
}
26 changes: 18 additions & 8 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@ use crate::AppState;
use axum::Router;

mod aets;
mod home;
pub mod qido;
pub mod stow;
pub mod wado;

pub fn routes() -> Router<AppState> {
Router::new().merge(aets::api()).nest(
"/aets/{aet}",
Router::new()
.merge(qido::routes())
.merge(wado::routes())
.merge(stow::routes()),
)
pub fn routes(base_path: &str) -> Router<AppState> {
let router = Router::new()
.merge(home::routes())
.merge(aets::routes())
.nest(
"/aets/{aet}",
Router::new()
.merge(qido::routes())
.merge(wado::routes())
.merge(stow::routes()),
);

// axum no longer supports nesting at the root
match base_path {
"/" | "" => router,
base_path => Router::new().nest(base_path, router),
}
}
1 change: 1 addition & 0 deletions src/config/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ server:
max-upload-size: 50000000
request-timeout: 60000
graceful-shutdown: true
base-path: /
dimse:
- aet: DICOM-RST
interface: 0.0.0.0
Expand Down
26 changes: 26 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ impl AppConfig {
))
.add_source(File::with_name("config.yaml").required(false))
.add_source(Environment::with_prefix("DICOM_RST").separator("_"))
.set_override_option(
"server.http.base-path",
std::env::var("DICOM_RST_SERVER_HTTP_BASE_PATH").ok(),
)?
.build()?
.try_deserialize()
}
Expand Down Expand Up @@ -232,6 +236,27 @@ pub struct HttpServerConfig {
pub max_upload_size: usize,
pub request_timeout: u64,
pub graceful_shutdown: bool,
pub base_path: String,
}

impl HttpServerConfig {
const WILDCARD_ADDRESSES: [&'static str; 3] =
["0.0.0.0", "::", "0000:0000:0000:0000:0000:0000:0000:0000"];

pub fn base_url(&self) -> Result<url::Url, url::ParseError> {
let origin = format!("http://{}:{}", self.interface, self.port);
let mut url = url::Url::parse(&origin)?;

if url
.host()
.is_some_and(|host| Self::WILDCARD_ADDRESSES.contains(&host.to_string().as_str()))
{
url.set_host(Some("127.0.0.1"))?;
}
let url = url.join(&self.base_path)?;

Ok(url)
}
}

impl Default for HttpServerConfig {
Expand All @@ -242,6 +267,7 @@ impl Default for HttpServerConfig {
graceful_shutdown: true,
max_upload_size: 50_000_000, // 50 MB
request_timeout: 60_000, // 1 min
base_path: String::from("/"),
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ async fn run(config: AppConfig) -> anyhow::Result<()> {
});
}

let app = api::routes()
let app = api::routes(&config.server.http.base_path)
.layer(CorsLayer::permissive())
.layer(axum::middleware::from_fn(add_common_headers))
.layer(
Expand All @@ -152,7 +152,12 @@ async fn run(config: AppConfig) -> anyhow::Result<()> {
let addr = SocketAddr::from((host, port));
let listener = TcpListener::bind(addr).await?;

info!("Started DICOMweb server on http://{addr}");
info!(
server.address = addr.ip().to_string(),
server.port = addr.port(),
url.full = config.server.http.base_url()?.as_str(),
"Started DICOMweb server"
);
if config.server.http.graceful_shutdown {
axum::serve(listener, app)
.with_graceful_shutdown(shutdown_signal())
Expand Down
Loading