diff --git a/nixpanel-api/migrations/0001_initial.sql b/nixpanel-api/migrations/0001_initial.sql index 6990697..1860fa3 100644 --- a/nixpanel-api/migrations/0001_initial.sql +++ b/nixpanel-api/migrations/0001_initial.sql @@ -1,12 +1,12 @@ -- Users (panel logins: admin + per-account users) CREATE TABLE IF NOT EXISTS users ( - id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, - username VARCHAR(64) NOT NULL, - email VARCHAR(255) NOT NULL, - password_hash VARCHAR(255) NOT NULL, + id BIGINT NOT NULL AUTO_INCREMENT, + username VARCHAR(64) NOT NULL, + email VARCHAR(255) NOT NULL, + password_hash VARCHAR(255) NOT NULL, role ENUM('admin','user') NOT NULL DEFAULT 'user', - created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, - updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (id), UNIQUE KEY uq_username (username), UNIQUE KEY uq_email (email) @@ -14,18 +14,18 @@ CREATE TABLE IF NOT EXISTS users ( -- Hosting accounts CREATE TABLE IF NOT EXISTS accounts ( - id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, - user_id BIGINT UNSIGNED, - username VARCHAR(64) NOT NULL, - domain VARCHAR(255) NOT NULL, - email VARCHAR(255) NOT NULL, - password_hash VARCHAR(255) NOT NULL, - package_name VARCHAR(64) NOT NULL DEFAULT 'Default', - disk_quota_mb INT NOT NULL DEFAULT 10240, - bandwidth_mb INT NOT NULL DEFAULT 0, + id BIGINT NOT NULL AUTO_INCREMENT, + user_id BIGINT, + username VARCHAR(64) NOT NULL, + domain VARCHAR(255) NOT NULL, + email VARCHAR(255) NOT NULL, + password_hash VARCHAR(255) NOT NULL, + package_name VARCHAR(64) NOT NULL DEFAULT 'Default', + disk_quota_mb INT NOT NULL DEFAULT 10240, + bandwidth_mb INT NOT NULL DEFAULT 0, status ENUM('active','suspended','terminated') NOT NULL DEFAULT 'active', - created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, - updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (id), UNIQUE KEY uq_username (username), UNIQUE KEY uq_domain (domain), @@ -34,12 +34,12 @@ CREATE TABLE IF NOT EXISTS accounts ( -- Domains / DNS zones CREATE TABLE IF NOT EXISTS domains ( - id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, - account_id BIGINT UNSIGNED, - name VARCHAR(255) NOT NULL, + id BIGINT NOT NULL AUTO_INCREMENT, + account_id BIGINT, + name VARCHAR(255) NOT NULL, type ENUM('main','addon','parked','subdomain') NOT NULL DEFAULT 'main', status ENUM('active','inactive') NOT NULL DEFAULT 'active', - created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), UNIQUE KEY uq_name (name), FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE @@ -47,25 +47,25 @@ CREATE TABLE IF NOT EXISTS domains ( -- DNS records CREATE TABLE IF NOT EXISTS dns_records ( - id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, - domain_id BIGINT UNSIGNED NOT NULL, - type VARCHAR(10) NOT NULL, -- A, AAAA, CNAME, MX, TXT, NS, SRV - name VARCHAR(255) NOT NULL, - content VARCHAR(512) NOT NULL, - ttl INT NOT NULL DEFAULT 3600, + id BIGINT NOT NULL AUTO_INCREMENT, + domain_id BIGINT NOT NULL, + type VARCHAR(10) NOT NULL, + name VARCHAR(255) NOT NULL, + content VARCHAR(512) NOT NULL, + ttl INT NOT NULL DEFAULT 3600, priority INT, - created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), FOREIGN KEY (domain_id) REFERENCES domains(id) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- MySQL/MariaDB databases owned by accounts CREATE TABLE IF NOT EXISTS account_databases ( - id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, - account_id BIGINT UNSIGNED NOT NULL, - db_name VARCHAR(64) NOT NULL, - db_user VARCHAR(64) NOT NULL, - created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + id BIGINT NOT NULL AUTO_INCREMENT, + account_id BIGINT NOT NULL, + db_name VARCHAR(64) NOT NULL, + db_user VARCHAR(64) NOT NULL, + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), UNIQUE KEY uq_db_name (db_name), FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE @@ -73,12 +73,12 @@ CREATE TABLE IF NOT EXISTS account_databases ( -- Email accounts CREATE TABLE IF NOT EXISTS email_accounts ( - id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, - account_id BIGINT UNSIGNED NOT NULL, - address VARCHAR(255) NOT NULL, - password_hash VARCHAR(255) NOT NULL, - quota_mb INT NOT NULL DEFAULT 1024, - created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + id BIGINT NOT NULL AUTO_INCREMENT, + account_id BIGINT NOT NULL, + address VARCHAR(255) NOT NULL, + password_hash VARCHAR(255) NOT NULL, + quota_mb INT NOT NULL DEFAULT 1024, + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), UNIQUE KEY uq_address (address), FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE @@ -86,11 +86,11 @@ CREATE TABLE IF NOT EXISTS email_accounts ( -- FTP accounts CREATE TABLE IF NOT EXISTS ftp_accounts ( - id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, - account_id BIGINT UNSIGNED NOT NULL, - username VARCHAR(64) NOT NULL, - home_dir VARCHAR(512) NOT NULL, - created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + id BIGINT NOT NULL AUTO_INCREMENT, + account_id BIGINT NOT NULL, + username VARCHAR(64) NOT NULL, + home_dir VARCHAR(512) NOT NULL, + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), UNIQUE KEY uq_username (username), FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE @@ -98,15 +98,15 @@ CREATE TABLE IF NOT EXISTS ftp_accounts ( -- SSL certificates CREATE TABLE IF NOT EXISTS ssl_certificates ( - id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, - account_id BIGINT UNSIGNED, - domain VARCHAR(255) NOT NULL, - issuer VARCHAR(64) NOT NULL DEFAULT 'letsencrypt', + id BIGINT NOT NULL AUTO_INCREMENT, + account_id BIGINT, + domain VARCHAR(255) NOT NULL, + issuer VARCHAR(64) NOT NULL DEFAULT 'letsencrypt', cert_path VARCHAR(512), key_path VARCHAR(512), expires_at DATETIME, - auto_renew TINYINT(1) NOT NULL DEFAULT 1, - created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + auto_renew TINYINT(1) NOT NULL DEFAULT 1, + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), UNIQUE KEY uq_domain (domain), FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE SET NULL @@ -114,13 +114,13 @@ CREATE TABLE IF NOT EXISTS ssl_certificates ( -- Backup jobs CREATE TABLE IF NOT EXISTS backups ( - id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, - account_id BIGINT UNSIGNED, - type ENUM('full','incremental') NOT NULL DEFAULT 'full', - status ENUM('pending','running','completed','failed') NOT NULL DEFAULT 'pending', - size_bytes BIGINT, - path VARCHAR(512), - created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + id BIGINT NOT NULL AUTO_INCREMENT, + account_id BIGINT, + type ENUM('full','incremental') NOT NULL DEFAULT 'full', + status ENUM('pending','running','completed','failed') NOT NULL DEFAULT 'pending', + size_bytes BIGINT, + path VARCHAR(512), + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, completed_at DATETIME, PRIMARY KEY (id), FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE SET NULL diff --git a/nixpanel-api/src/routes/accounts.rs b/nixpanel-api/src/routes/accounts.rs index 2a05a30..d8d36e6 100644 --- a/nixpanel-api/src/routes/accounts.rs +++ b/nixpanel-api/src/routes/accounts.rs @@ -11,8 +11,8 @@ pub struct Account { pub domain: String, pub email: String, pub package_name: String, - pub disk_quota_mb: i64, - pub bandwidth_mb: i64, + pub disk_quota_mb: i32, // INT column → i32 + pub bandwidth_mb: i32, // INT column → i32 pub status: String, pub created_at: NaiveDateTime, } @@ -24,8 +24,8 @@ pub struct CreateAccountRequest { pub email: String, pub password: String, pub package_name: Option, - pub disk_quota_mb: Option, - pub bandwidth_mb: Option, + pub disk_quota_mb: Option, + pub bandwidth_mb: Option, } pub async fn list_accounts( @@ -71,8 +71,8 @@ pub async fn create_account( let _ = std::fs::create_dir_all(format!("/home/{}/public_html", body.username)); let package_name = body.package_name.unwrap_or_else(|| "Default".into()); - let disk_quota_mb = body.disk_quota_mb.unwrap_or(10240); - let bandwidth_mb = body.bandwidth_mb.unwrap_or(0); + let disk_quota_mb: i32 = body.disk_quota_mb.unwrap_or(10240); + let bandwidth_mb: i32 = body.bandwidth_mb.unwrap_or(0); let insert_id: u64 = sqlx::query( "INSERT INTO accounts (username, domain, email, password_hash, package_name, disk_quota_mb, bandwidth_mb, status)