1use kotoba_core::types::{Result, Value, ContentHash, KotobaError};
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9use chrono::{DateTime, Utc};
10
11#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
13pub enum RuntimeType {
14 Deno,
16 NodeJs,
18 Python,
20 Rust,
22 Go,
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
28pub enum DeploymentStatus {
29 Creating,
31 Preparing,
33 Running,
35 Stopping,
37 Stopped,
39 Error(String),
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
45pub enum DeploymentPriority {
46 Low,
48 Normal,
50 High,
52 Critical,
54}
55
56#[derive(Debug, Clone, Serialize, Deserialize)]
58pub struct ResourceUsage {
59 pub cpu_usage: f64,
61 pub memory_usage: f64,
63 pub network_usage: f64,
65 pub disk_usage: f64,
67 pub request_rate: f64,
69}
70
71#[derive(Debug, Clone, Serialize, Deserialize)]
73pub struct DeployMetadata {
74 pub name: String,
76 pub version: String,
78 pub description: Option<String>,
80 pub author: Option<String>,
82 pub created_at: DateTime<Utc>,
84 pub updated_at: Option<DateTime<Utc>>,
86 pub config_hash: Option<ContentHash>,
88}
89
90#[derive(Debug, Clone, Serialize, Deserialize)]
92pub struct ApplicationConfig {
93 pub entry_point: String,
95 pub runtime: RuntimeType,
97 pub environment: HashMap<String, String>,
99 pub build_command: Option<String>,
101 pub start_command: Option<String>,
103}
104
105#[derive(Debug, Clone, Serialize, Deserialize)]
107pub struct ScalingConfig {
108 pub min_instances: u32,
110 pub max_instances: u32,
112 pub cpu_threshold: f64,
114 pub memory_threshold: f64,
116 pub auto_scaling_enabled: bool,
118}
119
120#[derive(Debug, Clone, Serialize, Deserialize)]
122pub struct NetworkConfig {
123 pub domains: Vec<String>,
125 pub ssl: Option<SslConfig>,
127 pub cors: Option<CorsConfig>,
129 pub cdn: Option<CdnConfig>,
131}
132
133#[derive(Debug, Clone, Serialize, Deserialize)]
135pub struct SslConfig {
136 pub cert_type: CertType,
138 pub cert_path: Option<String>,
140 pub key_path: Option<String>,
142}
143
144#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
146pub enum CertType {
147 LetsEncrypt,
149 Custom,
151 SelfSigned,
153}
154
155#[derive(Debug, Clone, Serialize, Deserialize)]
157pub struct CorsConfig {
158 pub allowed_origins: Vec<String>,
160 pub allowed_methods: Vec<String>,
162 pub allowed_headers: Vec<String>,
164 pub allow_credentials: bool,
166}
167
168#[derive(Debug, Clone, Serialize, Deserialize)]
170pub struct CdnConfig {
171 pub provider: CdnProvider,
173 pub edge_locations: Vec<String>,
175 pub cache_config: CacheConfig,
177}
178
179#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
181pub enum CdnProvider {
182 Cloudflare,
184 Fastly,
186 Akamai,
188 CloudFront,
190}
191
192#[derive(Debug, Clone, Serialize, Deserialize)]
194pub struct CacheConfig {
195 pub ttl: u32,
197 pub invalidation_patterns: Vec<String>,
199}
200
201#[derive(Debug, Clone, Serialize, Deserialize)]
203pub struct DeployConfig {
204 pub metadata: DeployMetadata,
206 pub application: ApplicationConfig,
208 pub scaling: ScalingConfig,
210 pub network: NetworkConfig,
212 pub custom: Value,
214}
215
216impl Default for DeployConfig {
217 fn default() -> Self {
218 Self {
219 metadata: DeployMetadata {
220 name: "default".to_string(),
221 version: "1.0.0".to_string(),
222 description: None,
223 author: None,
224 created_at: Utc::now(),
225 updated_at: None,
226 config_hash: None,
227 },
228 application: ApplicationConfig {
229 entry_point: "index.js".to_string(),
230 runtime: RuntimeType::Deno,
231 environment: HashMap::new(),
232 build_command: None,
233 start_command: None,
234 },
235 scaling: ScalingConfig {
236 min_instances: 1,
237 max_instances: 10,
238 cpu_threshold: 0.8,
239 memory_threshold: 0.8,
240 auto_scaling_enabled: true,
241 },
242 network: NetworkConfig {
243 domains: vec!["localhost".to_string()],
244 ssl: None,
245 cors: None,
246 cdn: None,
247 },
248 custom: Value::Null,
249 }
250 }
251}
252
253#[derive(Debug, Clone)]
255pub struct DeployConfigBuilder {
256 config: DeployConfig,
257}
258
259impl DeployConfigBuilder {
260 pub fn new(name: String) -> Self {
261 let mut config = DeployConfig::default();
262 config.metadata.name = name;
263 Self { config }
264 }
265
266 pub fn version(mut self, version: String) -> Self {
267 self.config.metadata.version = version;
268 self
269 }
270
271 pub fn description(mut self, description: String) -> Self {
272 self.config.metadata.description = Some(description);
273 self
274 }
275
276 pub fn author(mut self, author: String) -> Self {
277 self.config.metadata.author = Some(author);
278 self
279 }
280
281 pub fn entry_point(mut self, entry_point: String) -> Self {
282 self.config.application.entry_point = entry_point;
283 self
284 }
285
286 pub fn runtime(mut self, runtime: RuntimeType) -> Self {
287 self.config.application.runtime = runtime;
288 self
289 }
290
291 pub fn environment(mut self, key: String, value: String) -> Self {
292 self.config.application.environment.insert(key, value);
293 self
294 }
295
296 pub fn build_command(mut self, command: String) -> Self {
297 self.config.application.build_command = Some(command);
298 self
299 }
300
301 pub fn start_command(mut self, command: String) -> Self {
302 self.config.application.start_command = Some(command);
303 self
304 }
305
306 pub fn min_instances(mut self, min: u32) -> Self {
307 self.config.scaling.min_instances = min;
308 self
309 }
310
311 pub fn max_instances(mut self, max: u32) -> Self {
312 self.config.scaling.max_instances = max;
313 self
314 }
315
316 pub fn domains(mut self, domains: Vec<String>) -> Self {
317 self.config.network.domains = domains;
318 self
319 }
320
321 pub fn build(self) -> DeployConfig {
322 self.config
323 }
324}
325
326pub use DeployConfig as Config;
328pub use DeployMetadata as Metadata;
329pub use ApplicationConfig as AppConfig;
330pub use ScalingConfig as Scaling;
331pub use NetworkConfig as Network;