lynx_core/entities/
app_config.rs

1//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.4
2
3use anyhow::{anyhow, Result};
4use schemars::JsonSchema;
5use sea_orm::entity::prelude::*;
6use serde::{Deserialize, Serialize};
7
8use crate::server_context::DB;
9
10#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
11#[serde(rename_all = "camelCase")]
12#[sea_orm(table_name = "app_config")]
13pub struct Model {
14    #[sea_orm(primary_key)]
15    pub id: i32,
16    pub recording_status: RecordingStatus,
17    #[serde(rename = "captureSSL")]
18    pub capture_ssl: bool,
19    pub ssl_config: Option<Json>,
20}
21
22#[derive(
23    EnumIter, DeriveActiveEnum, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema,
24)]
25#[sea_orm(
26    rs_type = "String",
27    db_type = "String(StringLen::None)",
28    rename_all = "camelCase"
29)]
30pub enum RecordingStatus {
31    StartRecording,
32    PauseRecording,
33}
34
35#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
36pub enum Relation {}
37
38impl ActiveModelBehavior for ActiveModel {}
39
40pub async fn get_app_config() -> Model {
41    Entity::find()
42        .one(DB.get().unwrap())
43        .await
44        .expect("app config not found")
45        .expect("app config not found")
46}
47
48#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
49#[serde(rename_all = "camelCase")]
50pub struct SSLConfigRule {
51    pub switch: bool,
52    pub host: String,
53    pub port: Option<u16>,
54}
55
56#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
57#[serde(rename_all = "camelCase")]
58pub struct SSLConfig {
59    pub include_domains: Vec<SSLConfigRule>,
60    pub exclude_domains: Vec<SSLConfigRule>,
61}
62
63pub async fn get_enabled_ssl_config() -> Result<(Vec<SSLConfigRule>, Vec<SSLConfigRule>)> {
64    let app_config = get_app_config().await;
65    match app_config.ssl_config {
66        None => Ok((vec![], vec![])),
67        Some(ssl_config) => {
68            let ssl_config: SSLConfig = serde_json::from_value(ssl_config)
69                .map_err(|e| anyhow!(e).context("parse ssl config error"))?;
70            let SSLConfig {
71                include_domains,
72                exclude_domains,
73            } = ssl_config;
74            let include = include_domains.into_iter().filter(|x| x.switch).collect();
75            let exclude = exclude_domains.into_iter().filter(|x| x.switch).collect();
76            Ok((include, exclude))
77        }
78    }
79}