use kotoba_core::types::{Result, Value, ContentHash};
use kotoba_errors::KotobaError;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use chrono::{DateTime, Utc};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum RuntimeType {
Deno,
NodeJs,
Python,
Rust,
Go,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum DeploymentStatus {
Creating,
Preparing,
Running,
Stopping,
Stopped,
Error(String),
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum DeploymentPriority {
Low,
Normal,
High,
Critical,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceUsage {
pub cpu_usage: f64,
pub memory_usage: f64,
pub network_usage: f64,
pub disk_usage: f64,
pub request_rate: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeployMetadata {
pub name: String,
pub version: String,
pub description: Option<String>,
pub author: Option<String>,
pub created_at: DateTime<Utc>,
pub updated_at: Option<DateTime<Utc>>,
pub config_hash: Option<ContentHash>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApplicationConfig {
pub entry_point: String,
pub runtime: RuntimeType,
pub environment: HashMap<String, String>,
pub build_command: Option<String>,
pub start_command: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScalingConfig {
pub min_instances: u32,
pub max_instances: u32,
pub cpu_threshold: f64,
pub memory_threshold: f64,
pub auto_scaling_enabled: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkConfig {
pub domains: Vec<String>,
pub ssl: Option<SslConfig>,
pub cors: Option<CorsConfig>,
pub cdn: Option<CdnConfig>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SslConfig {
pub cert_type: CertType,
pub cert_path: Option<String>,
pub key_path: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum CertType {
LetsEncrypt,
Custom,
SelfSigned,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CorsConfig {
pub allowed_origins: Vec<String>,
pub allowed_methods: Vec<String>,
pub allowed_headers: Vec<String>,
pub allow_credentials: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CdnConfig {
pub provider: CdnProvider,
pub edge_locations: Vec<String>,
pub cache_config: CacheConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum CdnProvider {
Cloudflare,
Fastly,
Akamai,
CloudFront,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheConfig {
pub ttl: u32,
pub invalidation_patterns: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeployConfig {
pub metadata: DeployMetadata,
pub application: ApplicationConfig,
pub scaling: ScalingConfig,
pub network: NetworkConfig,
pub custom: Value,
}
impl Default for DeployConfig {
fn default() -> Self {
Self {
metadata: DeployMetadata {
name: "default".to_string(),
version: "1.0.0".to_string(),
description: None,
author: None,
created_at: Utc::now(),
updated_at: None,
config_hash: None,
},
application: ApplicationConfig {
entry_point: "index.js".to_string(),
runtime: RuntimeType::Deno,
environment: HashMap::new(),
build_command: None,
start_command: None,
},
scaling: ScalingConfig {
min_instances: 1,
max_instances: 10,
cpu_threshold: 0.8,
memory_threshold: 0.8,
auto_scaling_enabled: true,
},
network: NetworkConfig {
domains: vec!["localhost".to_string()],
ssl: None,
cors: None,
cdn: None,
},
custom: Value::Null,
}
}
}
#[derive(Debug, Clone)]
pub struct DeployConfigBuilder {
config: DeployConfig,
}
impl DeployConfigBuilder {
pub fn new(name: String) -> Self {
let mut config = DeployConfig::default();
config.metadata.name = name;
Self { config }
}
pub fn version(mut self, version: String) -> Self {
self.config.metadata.version = version;
self
}
pub fn description(mut self, description: String) -> Self {
self.config.metadata.description = Some(description);
self
}
pub fn author(mut self, author: String) -> Self {
self.config.metadata.author = Some(author);
self
}
pub fn entry_point(mut self, entry_point: String) -> Self {
self.config.application.entry_point = entry_point;
self
}
pub fn runtime(mut self, runtime: RuntimeType) -> Self {
self.config.application.runtime = runtime;
self
}
pub fn environment(mut self, key: String, value: String) -> Self {
self.config.application.environment.insert(key, value);
self
}
pub fn build_command(mut self, command: String) -> Self {
self.config.application.build_command = Some(command);
self
}
pub fn start_command(mut self, command: String) -> Self {
self.config.application.start_command = Some(command);
self
}
pub fn min_instances(mut self, min: u32) -> Self {
self.config.scaling.min_instances = min;
self
}
pub fn max_instances(mut self, max: u32) -> Self {
self.config.scaling.max_instances = max;
self
}
pub fn domains(mut self, domains: Vec<String>) -> Self {
self.config.network.domains = domains;
self
}
pub fn build(self) -> DeployConfig {
self.config
}
}
pub use DeployConfig as Config;
pub use DeployMetadata as Metadata;
pub use ApplicationConfig as AppConfig;
pub use ScalingConfig as Scaling;
pub use NetworkConfig as Network;