Skip to main content

haste_health/commands/
server.rs

1use std::sync::Arc;
2
3use clap::{Subcommand, ValueEnum};
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_jwt::claims::SubscriptionTier;
11use haste_server::{config::ServerConfig, server};
12
13#[derive(Clone, Debug, ValueEnum)]
14pub(crate) enum UserSubscriptionChoice {
15    Free,
16    Professional,
17    Team,
18    Unlimited,
19}
20
21impl From<UserSubscriptionChoice> for SubscriptionTier {
22    fn from(choice: UserSubscriptionChoice) -> Self {
23        match choice {
24            UserSubscriptionChoice::Free => SubscriptionTier::Free,
25            UserSubscriptionChoice::Professional => SubscriptionTier::Professional,
26            UserSubscriptionChoice::Team => SubscriptionTier::Team,
27            UserSubscriptionChoice::Unlimited => SubscriptionTier::Unlimited,
28        }
29    }
30}
31
32#[derive(Subcommand, Debug)]
33pub(crate) enum ServerCommands {
34    Start {
35        #[arg(short, long)]
36        port: Option<u16>,
37    },
38}
39
40pub(crate) async fn server(command: &ServerCommands) -> Result<(), OperationOutcomeError> {
41    let config: ServerConfig = Figment::new()
42        .merge(Toml::file("haste.toml"))
43        .merge(Env::prefixed("HASTE_"))
44        .extract()
45        .map_err(|e| OperationOutcomeError::error(IssueType::exception(), e.to_string()))?;
46
47    match &command {
48        ServerCommands::Start { port } => {
49            server::serve(Arc::new(config), port.unwrap_or(3000)).await
50        }
51    }
52}