Skip to main content

haste_health/commands/
server.rs

1use std::sync::Arc;
2
3use clap::Subcommand;
4use figment::{
5    Figment,
6    providers::{Env, Format as _, Toml},
7};
8use haste_fhir_model::r4::generated::terminology::IssueType;
9use haste_fhir_operation_error::OperationOutcomeError;
10use haste_server::{config::ServerConfig, server};
11
12/// Run the FHIR server.
13#[derive(Subcommand, Debug)]
14pub(crate) enum ServerCommands {
15    /// Start the HTTP server. Configuration is read from `haste.toml` and `HASTE_*` env vars.
16    Start {
17        /// Port to listen on. Defaults to 3000.
18        #[arg(short, long)]
19        port: Option<u16>,
20    },
21}
22
23/// Runs the `server` command group.
24pub(crate) async fn run(command: &ServerCommands) -> Result<(), OperationOutcomeError> {
25    let config: ServerConfig = Figment::new()
26        .merge(Toml::file("haste.toml"))
27        .merge(Env::prefixed("HASTE_"))
28        .extract()
29        .map_err(|e| OperationOutcomeError::error(IssueType::exception(), e.to_string()))?;
30
31    match &command {
32        ServerCommands::Start { port } => {
33            server::serve(Arc::new(config), port.unwrap_or(3000)).await
34        }
35    }
36}