lynx_core/entities/
rule_content.rs

1//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.4
2use anyhow::anyhow;
3use async_trait::async_trait;
4use schemars::{schema_for, JsonSchema};
5use sea_orm::entity::prelude::*;
6use serde::{Deserialize, Serialize};
7
8#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10#[sea_orm(table_name = "rule_content")]
11pub struct Model {
12    #[sea_orm(primary_key)]
13    pub id: i32,
14    pub content: Json,
15    pub rule_id: i32,
16}
17
18#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
19pub enum Relation {
20    #[sea_orm(
21        belongs_to = "super::rule::Entity",
22        from = "Column::RuleId",
23        to = "super::rule::Column::Id"
24    )]
25    Rule,
26}
27
28impl Related<super::rule::Entity> for Entity {
29    fn to() -> RelationDef {
30        Relation::Rule.def()
31    }
32}
33
34#[async_trait]
35impl ActiveModelBehavior for ActiveModel {}
36
37#[derive(Debug, Deserialize, Serialize, JsonSchema)]
38#[serde(rename_all = "camelCase")]
39pub struct Capture {
40    /// use glob pattern to match the uri
41    /// syntax: https://crates.io/crates/glob-match
42    pub uri: String,
43}
44
45#[derive(Debug, Deserialize, Serialize, JsonSchema)]
46#[serde(rename_all = "camelCase")]
47pub struct RuleContent {
48    pub capture: Capture,
49    pub handler: Handler,
50}
51
52#[derive(Debug, Deserialize, Serialize, JsonSchema)]
53#[serde(rename_all = "camelCase")]
54pub struct Handler {
55    /// proxy pass to the target
56    /// example: http://localhost:8080
57    pub proxy_pass: String,
58}
59
60pub fn parse_rule_content(instance: serde_json::Value) -> anyhow::Result<RuleContent> {
61    let schema = serde_json::to_value(schema_for!(RuleContent)).unwrap();
62    jsonschema::validate(&schema, &instance).map_err(|e| anyhow!(format!("{}", e)))?;
63    let rule_content: RuleContent = serde_json::from_value(instance)?;
64    Ok(rule_content)
65}
66