From 85ded3c5d18e8c9511ae686d7e5700780ca06496 Mon Sep 17 00:00:00 2001 From: wandoubaba Date: Mon, 16 Oct 2023 11:40:55 +0800 Subject: [PATCH] env --- .gitignore | 1 + Applications/JsonRpc/start.php | 5 ++-- Applications/Statistics/start.php | 10 +++---- composer.json | 3 ++- start | 45 ++++++++----------------------- start.php | 5 ++++ 6 files changed, 26 insertions(+), 43 deletions(-) diff --git a/.gitignore b/.gitignore index 38eaad2..18ee32d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .buildpath composer.lock vendor/ +.env diff --git a/Applications/JsonRpc/start.php b/Applications/JsonRpc/start.php index bda6270..b156f63 100644 --- a/Applications/JsonRpc/start.php +++ b/Applications/JsonRpc/start.php @@ -18,7 +18,7 @@ require_once __DIR__ . '/Clients/StatisticClient.php'; require_once __DIR__.'/Protocols/JsonNL.php'; // 开启的端口 -$worker = new Worker('JsonNL://0.0.0.0:2015'); +$worker = new Worker('JsonNL://0.0.0.0:' . $_ENV['RPC_PORT'] ?: '2015'); // 启动多少服务进程 $worker->count = 16; // worker名称,php start.php status 时展示使用 @@ -27,7 +27,7 @@ $worker->name = 'JsonRpc'; $worker->onMessage = function($connection, $data) { - $statistic_address = 'udp://127.0.0.1:55656'; + $statistic_address = 'udp://127.0.0.1:' . $_ENV['STATISTIC_WORKER_PORT'] ?: '55656'; // 判断数据是否正确 if(empty($data['class']) || empty($data['method']) || !isset($data['param_array'])) { @@ -78,7 +78,6 @@ $worker->onMessage = function($connection, $data) }; - // 如果不是在根目录启动,则运行runAll方法 if(!defined('GLOBAL_START')) { diff --git a/Applications/Statistics/start.php b/Applications/Statistics/start.php index 8b3b250..c62ca1b 100644 --- a/Applications/Statistics/start.php +++ b/Applications/Statistics/start.php @@ -20,18 +20,18 @@ use Bootstrap\StatisticWorker; use \Workerman\Worker; use \Workerman\WebServer; // StatisticProvider -$statistic_provider = new StatisticProvider("Text://0.0.0.0:55858"); +$statistic_provider = new StatisticProvider("Text://0.0.0.0:" . $_ENV['STATISTIC_PROVIDER_PORT'] ?: '55858'); $statistic_provider->name = 'StatisticProvider'; // StatisticWorker -$statistic_worker = new StatisticWorker("Statistic://0.0.0.0:55656"); +$statistic_worker = new StatisticWorker("Statistic://0.0.0.0:" . $_ENV['STATISTIC_WORKER_PORT'] ?: '55656'); $statistic_worker->transport = 'udp'; $statistic_worker->name = 'StatisticWorker'; // WebServer -$web = new WebServer("http://0.0.0.0:55757"); +$web = new WebServer("http://0.0.0.0:" . $_ENV['STATISTIC_WEB_PORT'] ?: '55757'); $web->name = 'StatisticWeb'; -$web->addRoot('www.your_domain.com', __DIR__.'/Web'); +$web->addRoot($_ENV['DOMAIN'] ?: 'localhost', __DIR__.'/Web'); // recv udp broadcast -$udp_finder = new Worker("Text://0.0.0.0:55858"); +$udp_finder = new Worker("Text://0.0.0.0:" . $_ENV['STATISTIC_PROVIDER_PORT'] ?: '55858'); $udp_finder->name = 'StatisticFinder'; $udp_finder->transport = 'udp'; $udp_finder->onMessage = function ($connection, $data) diff --git a/composer.json b/composer.json index 4cbe5bd..8e50e7c 100644 --- a/composer.json +++ b/composer.json @@ -5,6 +5,7 @@ "homepage": "http://www.workerman.net", "license" : "MIT", "require": { - "workerman/statistics" : ">=1.0.0" + "workerman/statistics" : ">=1.0.0", + "vlucas/phpdotenv": "^5.5" } } diff --git a/start b/start index 24445e0..f5da0fb 100755 --- a/start +++ b/start @@ -2,40 +2,17 @@ service_name="workerman-jsonrpc" compose_file="docker-compose-dev.yml" -if [ $# -eq 0 ] || [ $1 == "start" ] - then - docker rm -f $service_name - docker-compose -f $compose_file up -d - docker exec $service_name php start.php start -else - docker rm -f $service_name - docker-compose -f $compose_file up -d - docker exec $service_name php start.php "$@" -fi#!/bin/bash -service_name="workerman-jsonrpc" -compose_file="docker-compose-dev.yml" +if ! docker inspect -f '{{.State.Running}}' $service_name >/dev/null 2>&1; then + if ! docker-compose -f $compose_file up -d >/dev/null 2>&1; then + if ! docker compose -f $compose_file up -d >/dev/null 2>&1; then + echo "Failed to start the container using docker-compose or docker compose." + exit 1 + fi + fi +fi -if [ $# -eq 0 ] || [ $1 == "start" ] - then - docker rm -f $service_name - if docker-compose -f $compose_file up -d; then - docker exec $service_name php start.php start - else - if docker compose -f $compose_file up -d; then - docker exec $service_name php start.php start - else - echo "Failed to start the container using docker-compose or docker compose." - fi - fi +if [ $# -eq 0 ] || [ $1 == "start" ]; then + docker exec $service_name php start.php restart else - docker rm -f $service_name - if docker-compose -f $compose_file up -d; then - docker exec $service_name php start.php "$@" - else - if docker compose -f $compose_file up -d; then - docker exec $service_name php start.php "$@" - else - echo "Failed to start the container using docker-compose or docker compose." - fi - fi + docker exec $service_name php start.php "$@" fi \ No newline at end of file diff --git a/start.php b/start.php index 12096d7..17bc2e5 100644 --- a/start.php +++ b/start.php @@ -23,10 +23,15 @@ define('GLOBAL_START', 1); require_once __DIR__ . '/vendor/autoload.php'; +// 加载.env文件 +$dotenv = Dotenv\Dotenv::createImmutable(__DIR__); +$dotenv->load(); + // 加载所有Applications/*/start.php,以便启动所有服务 foreach(glob(__DIR__.'/Applications/*/start*.php') as $start_file) { require_once $start_file; } + // 运行所有服务 Worker::runAll();