/
DATKAI
/
ITCUP
Обзор
Документация
Войти
/
DATKAI
/
ITCUP
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
app/Models/NodeApp.php
93 строки
3 KB
DATKAI
fix(node-apps): workdir по умолчанию в /var/www/node-apps + понятная ошибка
01 июл 2026, 21:04
01 июл 2026, 21:04
482463f
Код
Авторство
О чём код?
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Str; class NodeApp extends Model { protected $fillable = [ 'name', 'slug', 'description', 'workdir', 'run_mode', 'npm_script', 'entry_file', 'custom_command', 'node_bin', 'port', 'env', 'repo_url', 'branch', 'domain', 'autostart', 'auto_restart', 'status', 'pid', 'last_started_at', 'last_exit_code', 'last_deploy_at', 'last_deploy_status', 'last_deploy_output', 'notes', ]; protected $casts = [ 'port' => 'integer', 'pid' => 'integer', 'autostart' => 'boolean', 'auto_restart' => 'boolean', 'last_started_at' => 'datetime', 'last_exit_code' => 'integer', 'last_deploy_at' => 'datetime', ]; public const STATUSES = [ 'running' => 'Работает', 'stopped' => 'Остановлено', 'errored' => 'Ошибка', ]; public const STATUS_COLORS = [ 'running' => 'success', 'stopped' => 'gray', 'errored' => 'danger', ]; public const RUN_MODES = [ 'npm' => 'npm-скрипт', 'node' => 'node entry-файл', 'custom' => 'Произвольная команда', ]; /** Базовый каталог для проектов Node.js */ public static function baseDir(): string { return rtrim(env('NODE_APPS_BASE', '/var/www/node-apps'), '/'); } protected static function booted(): void { static::saving(function (NodeApp $app) { if (empty($app->slug)) { $app->slug = Str::slug($app->name); } // Пустая рабочая директория → дефолт в базовом каталоге (доступен www-data) if (empty($app->workdir)) { $app->workdir = self::baseDir() . '/' . $app->slug; } }); } /** Итоговая команда запуска (без учёта окружения/директории) */ public function launchCommand(): string { return match ($this->run_mode) { 'node' => sprintf('%s %s', $this->node_bin ?: 'node', $this->entry_file), 'custom' => (string) $this->custom_command, default => sprintf('npm run %s', $this->npm_script ?: 'start'), }; } /** Разбор env из построчного KEY=VALUE в массив */ public function envPairs(): array { $pairs = []; foreach (preg_split('/\r?\n/', (string) $this->env) as $line) { $line = trim($line); if ($line === '' || str_starts_with($line, '#') || !str_contains($line, '=')) { continue; } [$k, $v] = explode('=', $line, 2); $pairs[trim($k)] = trim($v); } return $pairs; } }