1use kotoba_core::types::{Result, Value, ContentHash};
7use kotoba_errors::KotobaError;
8use serde::{Deserialize, Serialize};
9use std::collections::HashMap;
10use chrono::{DateTime, Utc};
11
12#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
14pub enum RuntimeType {
15 Deno,
17 NodeJs,
19 Python,
21 Rust,
23 Go,
25}
26
27#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
29pub enum DeploymentStatus {
30 Creating,
32 Preparing,
34 Running,
36 Stopping,
38 Stopped,
40 Error(String),
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
46pub enum DeploymentPriority {
47 Low,
49 Normal,
51 High,
53 Critical,
55}
56
57#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct ResourceUsage {
60 pub cpu_usage: f64,
62 pub memory_usage: f64,
64 pub network_usage: f64,
66 pub disk_usage: f64,
68 pub request_rate: f64,
70}
71
72#[derive(Debug, Clone, Serialize, Deserialize)]
74pub struct DeployMetadata {
75 pub name: String,
77 pub version: String,
79 pub description: Option<String>,
81 pub author: Option<String>,
83 pub created_at: DateTime<Utc>,
85 pub updated_at: Option<DateTime<Utc>>,
87 pub config_hash: Option<ContentHash>,
89}
90
91#[derive(Debug, Clone, Serialize, Deserialize)]
93pub struct ApplicationConfig {
94 pub entry_point: String,
96 pub runtime: RuntimeType,
98 pub environment: HashMap<String, String>,
100 pub build_command: Option<String>,
102 pub start_command: Option<String>,
104}
105
106#[derive(Debug, Clone, Serialize, Deserialize)]
108pub struct ScalingConfig {
109 pub min_instances: u32,
111 pub max_instances: u32,
113 pub cpu_threshold: f64,
115 pub memory_threshold: f64,
117 pub auto_scaling_enabled: bool,
119}
120
121#[derive(Debug, Clone, Serialize, Deserialize)]
123pub struct NetworkConfig {
124 pub domains: Vec<String>,
126 pub ssl: Option<SslConfig>,
128 pub cors: Option<CorsConfig>,
130 pub cdn: Option<CdnConfig>,
132}
133
134#[derive(Debug, Clone, Serialize, Deserialize)]
136pub struct SslConfig {
137 pub cert_type: CertType,
139 pub cert_path: Option<String>,
141 pub key_path: Option<String>,
143}
144
145#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
147pub enum CertType {
148 LetsEncrypt,
150 Custom,
152 SelfSigned,
154}
155
156#[derive(Debug, Clone, Serialize, Deserialize)]
158pub struct CorsConfig {
159 pub allowed_origins: Vec<String>,
161 pub allowed_methods: Vec<String>,
163 pub allowed_headers: Vec<String>,
165 pub allow_credentials: bool,
167}
168
169#[derive(Debug, Clone, Serialize, Deserialize)]
171pub struct CdnConfig {
172 pub provider: CdnProvider,
174 pub edge_locations: Vec<String>,
176 pub cache_config: CacheConfig,
178}
179
180#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
182pub enum CdnProvider {
183 Cloudflare,
185 Fastly,
187 Akamai,
189 CloudFront,
191}
192
193#[derive(Debug, Clone, Serialize, Deserialize)]
195pub struct CacheConfig {
196 pub ttl: u32,
198 pub invalidation_patterns: Vec<String>,
200}
201
202#[derive(Debug, Clone, Serialize, Deserialize)]
204pub struct DeployConfig {
205 pub metadata: DeployMetadata,
207 pub application: ApplicationConfig,
209 pub scaling: ScalingConfig,
211 pub network: NetworkConfig,
213 pub custom: Value,
215}
216
217impl Default for DeployConfig {
218 fn default() -> Self {
219 Self {
220 metadata: DeployMetadata {
221 name: "default".to_string(),
222 version: "1.0.0".to_string(),
223 description: None,
224 author: None,
225 created_at: Utc::now(),
226 updated_at: None,
227 config_hash: None,
228 },
229 application: ApplicationConfig {
230 entry_point: "index.js".to_string(),
231 runtime: RuntimeType::Deno,
232 environment: HashMap::new(),
233 build_command: None,
234 start_command: None,
235 },
236 scaling: ScalingConfig {
237 min_instances: 1,
238 max_instances: 10,
239 cpu_threshold: 0.8,
240 memory_threshold: 0.8,
241 auto_scaling_enabled: true,
242 },
243 network: NetworkConfig {
244 domains: vec!["localhost".to_string()],
245 ssl: None,
246 cors: None,
247 cdn: None,
248 },
249 custom: Value::Null,
250 }
251 }
252}
253
254#[derive(Debug, Clone)]
256pub struct DeployConfigBuilder {
257 config: DeployConfig,
258}
259
260impl DeployConfigBuilder {
261 pub fn new(name: String) -> Self {
262 let mut config = DeployConfig::default();
263 config.metadata.name = name;
264 Self { config }
265 }
266
267 pub fn version(mut self, version: String) -> Self {
268 self.config.metadata.version = version;
269 self
270 }
271
272 pub fn description(mut self, description: String) -> Self {
273 self.config.metadata.description = Some(description);
274 self
275 }
276
277 pub fn author(mut self, author: String) -> Self {
278 self.config.metadata.author = Some(author);
279 self
280 }
281
282 pub fn entry_point(mut self, entry_point: String) -> Self {
283 self.config.application.entry_point = entry_point;
284 self
285 }
286
287 pub fn runtime(mut self, runtime: RuntimeType) -> Self {
288 self.config.application.runtime = runtime;
289 self
290 }
291
292 pub fn environment(mut self, key: String, value: String) -> Self {
293 self.config.application.environment.insert(key, value);
294 self
295 }
296
297 pub fn build_command(mut self, command: String) -> Self {
298 self.config.application.build_command = Some(command);
299 self
300 }
301
302 pub fn start_command(mut self, command: String) -> Self {
303 self.config.application.start_command = Some(command);
304 self
305 }
306
307 pub fn min_instances(mut self, min: u32) -> Self {
308 self.config.scaling.min_instances = min;
309 self
310 }
311
312 pub fn max_instances(mut self, max: u32) -> Self {
313 self.config.scaling.max_instances = max;
314 self
315 }
316
317 pub fn domains(mut self, domains: Vec<String>) -> Self {
318 self.config.network.domains = domains;
319 self
320 }
321
322 pub fn build(self) -> DeployConfig {
323 self.config
324 }
325}
326
327pub use DeployConfig as Config;
329pub use DeployMetadata as Metadata;
330pub use ApplicationConfig as AppConfig;
331pub use ScalingConfig as Scaling;
332pub use NetworkConfig as Network;