dgate 2.1.0

DGate API Gateway - High-performance API gateway with JavaScript module support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//! Resource types for DGate
//!
//! This module defines all the core resource types:
//! - Namespace: Organizational unit for resources
//! - Route: Request routing configuration
//! - Service: Upstream service definitions
//! - Module: JavaScript/TypeScript modules for request handling
//! - Domain: Ingress domain configuration
//! - Secret: Sensitive data storage
//! - Collection: Document grouping
//! - Document: KV data storage

#![allow(dead_code)] // Public API items for future use

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use url::Url;

/// Namespace is a way to organize resources in DGate
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Namespace {
    pub name: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<String>,
}

impl Namespace {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            tags: Vec::new(),
        }
    }

    pub fn default_namespace() -> Self {
        Self {
            name: "default".to_string(),
            tags: vec!["default".to_string()],
        }
    }
}

/// Route defines how requests are handled and forwarded
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Route {
    pub name: String,
    pub namespace: String,
    pub paths: Vec<String>,
    pub methods: Vec<String>,
    #[serde(default)]
    pub strip_path: bool,
    #[serde(default)]
    pub preserve_host: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub service: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub modules: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<String>,
}

impl Route {
    pub fn new(name: impl Into<String>, namespace: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            namespace: namespace.into(),
            paths: Vec::new(),
            methods: vec!["*".to_string()],
            strip_path: false,
            preserve_host: false,
            service: None,
            modules: Vec::new(),
            tags: Vec::new(),
        }
    }
}

/// Service represents an upstream service
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Service {
    pub name: String,
    pub namespace: String,
    pub urls: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub retries: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub retry_timeout_ms: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub connect_timeout_ms: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub request_timeout_ms: Option<u64>,
    #[serde(default)]
    pub tls_skip_verify: bool,
    #[serde(default)]
    pub http2_only: bool,
    #[serde(default)]
    pub hide_dgate_headers: bool,
    #[serde(default)]
    pub disable_query_params: bool,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<String>,
}

impl Service {
    pub fn new(name: impl Into<String>, namespace: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            namespace: namespace.into(),
            urls: Vec::new(),
            retries: None,
            retry_timeout_ms: None,
            connect_timeout_ms: None,
            request_timeout_ms: None,
            tls_skip_verify: false,
            http2_only: false,
            hide_dgate_headers: false,
            disable_query_params: false,
            tags: Vec::new(),
        }
    }

    /// Parse URLs into actual Url objects
    pub fn parsed_urls(&self) -> Vec<Url> {
        self.urls
            .iter()
            .filter_map(|u| Url::parse(u).ok())
            .collect()
    }
}

/// Module type for script processing
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "lowercase")]
pub enum ModuleType {
    #[default]
    Javascript,
    Typescript,
}

impl ModuleType {
    pub fn as_str(&self) -> &'static str {
        match self {
            ModuleType::Javascript => "javascript",
            ModuleType::Typescript => "typescript",
        }
    }
}

/// Module contains JavaScript/TypeScript code for request processing
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Module {
    pub name: String,
    pub namespace: String,
    /// Base64 encoded payload
    pub payload: String,
    #[serde(default, rename = "moduleType")]
    pub module_type: ModuleType,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<String>,
}

impl Module {
    pub fn new(name: impl Into<String>, namespace: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            namespace: namespace.into(),
            payload: String::new(),
            module_type: ModuleType::Javascript,
            tags: Vec::new(),
        }
    }

    /// Decode the base64 payload to get the actual script content
    pub fn decode_payload(&self) -> Result<String, base64::DecodeError> {
        use base64::Engine;
        let bytes = base64::engine::general_purpose::STANDARD.decode(&self.payload)?;
        Ok(String::from_utf8_lossy(&bytes).to_string())
    }

    /// Encode a script as base64 payload
    pub fn encode_payload(script: &str) -> String {
        use base64::Engine;
        base64::engine::general_purpose::STANDARD.encode(script)
    }
}

/// Domain controls ingress traffic into namespaces
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Domain {
    pub name: String,
    pub namespace: String,
    pub patterns: Vec<String>,
    #[serde(default)]
    pub priority: i32,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub cert: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub key: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<String>,
}

impl Domain {
    pub fn new(name: impl Into<String>, namespace: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            namespace: namespace.into(),
            patterns: Vec::new(),
            priority: 0,
            cert: String::new(),
            key: String::new(),
            tags: Vec::new(),
        }
    }
}

/// Secret stores sensitive information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Secret {
    pub name: String,
    pub namespace: String,
    #[serde(skip_serializing_if = "String::is_empty")]
    pub data: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<String>,
    #[serde(default = "Utc::now")]
    pub created_at: DateTime<Utc>,
    #[serde(default = "Utc::now")]
    pub updated_at: DateTime<Utc>,
}

impl Secret {
    pub fn new(name: impl Into<String>, namespace: impl Into<String>) -> Self {
        let now = Utc::now();
        Self {
            name: name.into(),
            namespace: namespace.into(),
            data: String::new(),
            tags: Vec::new(),
            created_at: now,
            updated_at: now,
        }
    }
}

/// Collection groups Documents together
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Collection {
    pub name: String,
    pub namespace: String,
    #[serde(default)]
    pub visibility: CollectionVisibility,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<String>,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "lowercase")]
pub enum CollectionVisibility {
    /// Private collections are only accessible by modules in the same namespace
    #[default]
    Private,
    /// Public collections are accessible by modules from any namespace
    Public,
}

impl CollectionVisibility {
    /// Check if this visibility allows access from another namespace
    pub fn is_public(&self) -> bool {
        matches!(self, CollectionVisibility::Public)
    }
}

impl Collection {
    pub fn new(name: impl Into<String>, namespace: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            namespace: namespace.into(),
            visibility: CollectionVisibility::Private,
            tags: Vec::new(),
        }
    }

    /// Check if this collection is accessible from a given namespace.
    ///
    /// - Private collections are only accessible from the same namespace
    /// - Public collections are accessible from any namespace
    pub fn is_accessible_from(&self, requesting_namespace: &str) -> bool {
        match self.visibility {
            CollectionVisibility::Private => self.namespace == requesting_namespace,
            CollectionVisibility::Public => true,
        }
    }
}

/// Document is KV data stored in a collection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Document {
    pub id: String,
    pub namespace: String,
    pub collection: String,
    /// The document data - stored as JSON value
    pub data: serde_json::Value,
    #[serde(default = "Utc::now")]
    pub created_at: DateTime<Utc>,
    #[serde(default = "Utc::now")]
    pub updated_at: DateTime<Utc>,
}

impl Document {
    pub fn new(
        id: impl Into<String>,
        namespace: impl Into<String>,
        collection: impl Into<String>,
    ) -> Self {
        let now = Utc::now();
        Self {
            id: id.into(),
            namespace: namespace.into(),
            collection: collection.into(),
            data: serde_json::Value::Null,
            created_at: now,
            updated_at: now,
        }
    }
}

/// Change log command types
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ChangeCommand {
    AddNamespace,
    DeleteNamespace,
    AddRoute,
    DeleteRoute,
    AddService,
    DeleteService,
    AddModule,
    DeleteModule,
    AddDomain,
    DeleteDomain,
    AddSecret,
    DeleteSecret,
    AddCollection,
    DeleteCollection,
    AddDocument,
    DeleteDocument,
}

impl ChangeCommand {
    pub fn resource_type(&self) -> ResourceType {
        match self {
            ChangeCommand::AddNamespace | ChangeCommand::DeleteNamespace => ResourceType::Namespace,
            ChangeCommand::AddRoute | ChangeCommand::DeleteRoute => ResourceType::Route,
            ChangeCommand::AddService | ChangeCommand::DeleteService => ResourceType::Service,
            ChangeCommand::AddModule | ChangeCommand::DeleteModule => ResourceType::Module,
            ChangeCommand::AddDomain | ChangeCommand::DeleteDomain => ResourceType::Domain,
            ChangeCommand::AddSecret | ChangeCommand::DeleteSecret => ResourceType::Secret,
            ChangeCommand::AddCollection | ChangeCommand::DeleteCollection => {
                ResourceType::Collection
            }
            ChangeCommand::AddDocument | ChangeCommand::DeleteDocument => ResourceType::Document,
        }
    }

    pub fn is_add(&self) -> bool {
        matches!(
            self,
            ChangeCommand::AddNamespace
                | ChangeCommand::AddRoute
                | ChangeCommand::AddService
                | ChangeCommand::AddModule
                | ChangeCommand::AddDomain
                | ChangeCommand::AddSecret
                | ChangeCommand::AddCollection
                | ChangeCommand::AddDocument
        )
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ResourceType {
    Namespace,
    Route,
    Service,
    Module,
    Domain,
    Secret,
    Collection,
    Document,
}

/// Change log entry for tracking state changes
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChangeLog {
    pub id: String,
    pub cmd: ChangeCommand,
    pub namespace: String,
    pub name: String,
    pub item: serde_json::Value,
    #[serde(default = "Utc::now")]
    pub timestamp: DateTime<Utc>,
}

impl ChangeLog {
    pub fn new<T: Serialize>(
        cmd: ChangeCommand,
        namespace: impl Into<String>,
        name: impl Into<String>,
        item: &T,
    ) -> Self {
        Self {
            id: uuid::Uuid::new_v4().to_string(),
            cmd,
            namespace: namespace.into(),
            name: name.into(),
            item: serde_json::to_value(item).unwrap_or(serde_json::Value::Null),
            timestamp: Utc::now(),
        }
    }
}

/// Internal representation of a route with resolved references
#[derive(Debug, Clone)]
pub struct ResolvedRoute {
    pub route: Route,
    pub namespace: Namespace,
    pub service: Option<Service>,
    pub modules: Vec<Module>,
}

/// Internal representation of a domain with resolved namespace
#[derive(Debug, Clone)]
pub struct ResolvedDomain {
    pub domain: Domain,
    pub namespace: Namespace,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_namespace_creation() {
        let ns = Namespace::new("test");
        assert_eq!(ns.name, "test");
        assert!(ns.tags.is_empty());
    }

    #[test]
    fn test_module_payload_encoding() {
        let script = "export function requestHandler(ctx) { return ctx; }";
        let encoded = Module::encode_payload(script);

        let mut module = Module::new("test", "default");
        module.payload = encoded;

        let decoded = module.decode_payload().unwrap();
        assert_eq!(decoded, script);
    }

    #[test]
    fn test_route_creation() {
        let route = Route::new("test-route", "default");
        assert_eq!(route.name, "test-route");
        assert_eq!(route.namespace, "default");
        assert_eq!(route.methods, vec!["*"]);
    }

    #[test]
    fn test_collection_visibility_private() {
        let col = Collection::new("users", "namespace-a");

        // Private collection should only be accessible from same namespace
        assert!(col.is_accessible_from("namespace-a"));
        assert!(!col.is_accessible_from("namespace-b"));
        assert!(!col.is_accessible_from("other"));
    }

    #[test]
    fn test_collection_visibility_public() {
        let mut col = Collection::new("shared-data", "namespace-a");
        col.visibility = CollectionVisibility::Public;

        // Public collection should be accessible from any namespace
        assert!(col.is_accessible_from("namespace-a"));
        assert!(col.is_accessible_from("namespace-b"));
        assert!(col.is_accessible_from("any-namespace"));
    }

    #[test]
    fn test_collection_visibility_default_is_private() {
        let col = Collection::new("test", "default");
        assert_eq!(col.visibility, CollectionVisibility::Private);
        assert!(!col.visibility.is_public());
    }

    #[test]
    fn test_collection_visibility_is_public() {
        assert!(!CollectionVisibility::Private.is_public());
        assert!(CollectionVisibility::Public.is_public());
    }
}