annis_web/
config.rs

1use crate::Result;
2use clap::Parser;
3use oauth2::{basic::BasicClient, AuthUrl, ClientId, RedirectUrl, TokenUrl};
4use std::{ffi::OsString, path::PathBuf};
5
6#[derive(Parser, Debug)]
7#[command(author, version, about, long_about = None)]
8pub struct CliConfig {
9    /// Port to listen to.
10    #[arg(long, short, default_value_t = 3000)]
11    pub port: u16,
12    /// Externally used URL for the start page of the frontend.
13    #[arg(long, default_value = "http://127.0.0.1:3000/")]
14    pub frontend_prefix: String,
15    /// URL for the graphANNIS service used by the frontend.
16    #[arg(long, default_value = "http://127.0.0.1:5711/v1/")]
17    pub service_url: String,
18
19    /// If set, the SQLite database file to store sessions in.
20    #[arg(long)]
21    pub session_file: Option<PathBuf>,
22    /// Client name of this service when connecting to an OAuth 2.0 authorization server.
23    #[arg(long, env = "ANNIS_OAUTH2_CLIENT_ID", default_value = "annis")]
24    pub oauth2_client_id: String,
25    /// URL of the OAuth 2.0 authorization server's authorization endpoint.
26    #[arg(long, env = "ANNIS_OAUTH2_AUTH_URL")]
27    pub oauth2_auth_url: Option<String>,
28    /// URL of the OAuth 2.0 authorization server's token endpoint.
29    #[arg(long, env = "ANNIS_OAUTH2_TOKEN_URL")]
30    pub oauth2_token_url: Option<String>,
31}
32
33impl Default for CliConfig {
34    fn default() -> Self {
35        let empty_arguments: Vec<OsString> = Vec::default();
36        Parser::parse_from(empty_arguments)
37    }
38}
39
40impl CliConfig {
41    pub fn create_oauth2_basic_client(&self) -> Result<Option<BasicClient>> {
42        if let (Some(auth_url), Some(token_url)) = (&self.oauth2_auth_url, &self.oauth2_token_url) {
43            let redirect_url = format!("{}/oauth/callback", self.frontend_prefix);
44            let client = BasicClient::new(
45                ClientId::new("annis".to_string()),
46                None,
47                AuthUrl::new(auth_url.clone())?,
48                Some(TokenUrl::new(token_url.clone())?),
49            )
50            .set_redirect_uri(RedirectUrl::new(redirect_url)?);
51            Ok(Some(client))
52        } else {
53            Ok(None)
54        }
55    }
56}