Commit 1c80fb87 authored by Ильнур Табулдин's avatar Ильнур Табулдин :skull_crossbones:
Browse files

s

parent b3410ffb
No related merge requests found
Showing with 424 additions and 51 deletions
+424 -51
.gitignore 100644 → 100755
File mode changed from 100644 to 100755
composer.json 100644 → 100755
......@@ -20,12 +20,23 @@
"yiisoft/yii2": "~2.0.14",
"yiisoft/yii2-queue": "^2.3",
"enqueue/amqp-lib": "^0.10.18",
"ramsey/uuid": "^4.2"
"ramsey/uuid": "^4.2",
"deck/soft-delete": "1.2.0.2",
"deck/php-clickhouse": "1.1.0.1",
"wbraganca/yii2-dynamicform": "^2.0"
},
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
},
{
"type": "git",
"url": "https://gitlab.deck.lc/deckko/soft-delete.git"
},
{
"type": "git",
"url": "https://gitlab.deck.lc/deckko/php-clickhouse.git"
}
],
"config": {
......
......@@ -2,7 +2,9 @@
namespace deck\ServiceManager;
use app\modules\infrastructure\api\ServiceManagerApi;
use deck\ServiceManager\api\ServiceManagerApi;
use Yii;
class ServiceManager extends \yii\base\Module
{
......@@ -16,5 +18,24 @@ class ServiceManager extends \yii\base\Module
]
];
parent::init();
$this->registerTranslations();
}
public function registerTranslations()
{
Yii::$app->i18n->translations['service-manager/*'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'ru-RU',
'basePath' => '@vendor/deck/service-manager/messages',
'forceTranslation' => true,
'fileMap' => [
'service-manager/models' => 'models.php',
],
];
}
public static function t(string $category, string $message, array $params = [], ?string $language = null): string
{
return Yii::t('service-manager/' . $category, $message, $params, $language);
}
}
......@@ -4,17 +4,12 @@ namespace deck\ServiceManager;
use yii\base\BootstrapInterface;
class Bootstrap implements BootstrapInterface
class ServiceManagerBootstrap implements BootstrapInterface
{
/**
* @param $app
* @return void
* @throws \yii\base\InvalidConfigException
*/
public function bootstrap($app): void
{
$rules = [
'GET,POST service-manager/api/v1/service/<alias>' => 'infrastructure/api/v1/service/view'
'GET,POST service-manager/api/v1/service/<alias>' => 'service-manager/api/v1/service/view'
];
$app->getUrlManager()->addRules($rules, false);
}
......
<?php
namespace app\modules\infrastructure\api;
namespace deck\ServiceManager\api;
use app\modules\infrastructure\api;
use app\modules\infrastructure\utilities\ApiKeyAuthBehavior;
use deck\ServiceManager\api\v1\ServiceManagerApiV1;
use deck\ServiceManager\behaviors\ApiKeyAuthBehavior;
use yii\base\Module;
use yii\web\Application;
/**
* v1 module definition class
*/
class ServiceManagerApi extends Module
{
/**
* @return void
*/
public function init()
{
$this->modules = [
'v1' => [
'class' => api\v1\ServiceManagerApiV1::class
'class' => ServiceManagerApiV1::class
],
];
parent::init();
if (\Yii::$app instanceof Application) {
\Yii::$app->response->format = 'djson';
\Yii::$app->response->format = 'api';
}
}
......
<?php
namespace app\modules\infrastructure\api\v1;
namespace deck\ServiceManager\api\v1;
use yii\base\Module;
/**
* v1 module definition class
*/
class ServiceManagerApiV1 extends Module
{
public $controllerNamespace = 'app\modules\infrastructure\api\v1\controllers';
}
<?php
namespace app\modules\infrastructure\api\v1\controllers\Service;
namespace deck\ServiceManager\api\v1\controllers\Service;
use app\modules\infrastructure\enums\ParameterEnum;
use app\modules\infrastructure\models\Service;
use app\modules\infrastructure\models\ServiceLogTarget;
use app\modules\infrastructure\models\ServiceParameter;
use app\modules\infrastructure\utilities\Security;
use deck\ServiceManager\enums\ParameterEnum;
use deck\ServiceManager\models\Service;
use deck\ServiceManager\models\ServiceLogTarget;
use deck\ServiceManager\models\ServiceParameter;
use deck\ServiceManager\utilities\Security;
class ServiceSerializer implements \JsonSerializable
{
......
<?php
namespace app\modules\infrastructure\api\v1\controllers\Service;
namespace deck\ServiceManager\api\v1\controllers\Service;
use deck\form\Form;
......
<?php
namespace app\modules\infrastructure\api\v1\controllers;
namespace deck\ServiceManager\api\v1\controllers;
use app\components\controller\rest\Controller;
use app\modules\infrastructure\api\v1\controllers\Service\ServiceSerializer;
use app\modules\infrastructure\api\v1\controllers\Service\VersionForm;
use app\modules\infrastructure\models\Service;
use deck\exceptions\ValidationException;
use deck\ServiceManager\api\v1\controllers\Service\ServiceSerializer;
use deck\ServiceManager\api\v1\controllers\Service\VersionForm;
use deck\ServiceManager\models\Service;
use Yii;
use yii\rest\Controller;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
use yii\web\ServerErrorHttpException;
class ServiceController extends Controller
{
/**
* @param string $alias
* @return ServiceSerializer
* @throws BadRequestHttpException
* @throws NotFoundHttpException
* @throws ServerErrorHttpException
* @throws ValidationException
*/
public function actionView(string $alias): ServiceSerializer
{
\Yii::$app->response->format = 'djson';
\Yii::$app->response->format = 'api';
/** @var ?Service $service */
$service = Service::find()->where(['alias' => $alias])->one();
if ($service === null) {
......
......@@ -8,11 +8,6 @@ use yii\web\ForbiddenHttpException;
class ApiKeyAuthBehavior extends ActionFilter
{
/**
* @param $action
* @return true
* @throws ForbiddenHttpException
*/
public function beforeAction($action): bool
{
$key = Yii::$app->request->getHeaders()->get('X-Api-Key');
......
<?php
namespace deck\ServiceManager\enums;
use yii\log\Logger;
class LogLevelEnum
{
const DEBUG = 7;
const INFO = 6;
const NOTICE = 5;
const WARNING = 4;
const ERROR = 3;
const CRITICAL = 2;
const ALERT = 1;
const EMERGENCY = 0;
public static function toString(int $level): string
{
switch ($level) {
case self::DEBUG:
return 'DEBUG';
case self::INFO:
return 'INFO';
case self::NOTICE:
return 'NOTICE';
case self::WARNING:
return 'WARNING';
case self::ERROR:
return 'ERROR';
case self::CRITICAL:
return 'CRITICAL';
case self::ALERT:
return 'ALERT';
case self::EMERGENCY:
return 'EMERGENCY';
default:
return 'UNKNOWN';
}
}
public static function list(): array
{
return [
0 => 'emergency',
1 => 'alert',
2 => 'critical',
3 => 'error',
4 => 'warning',
5 => 'notice',
6 => 'info',
7 => 'debug',
];
}
public static function from(int $level): int
{
switch ($level) {
case Logger::LEVEL_ERROR:
return self::ERROR;
case Logger::LEVEL_WARNING:
return self::WARNING;
case Logger::LEVEL_TRACE:
return self::DEBUG;
case Logger::LEVEL_INFO:
default:
return self::INFO;
}
}
public static function to(int $level): array
{
switch ($level) {
case self::EMERGENCY:
case self::ALERT:
case self::CRITICAL:
case self::ERROR:
$levels = [Logger::LEVEL_ERROR];
break;
case self::WARNING:
case self::NOTICE:
$levels = [Logger::LEVEL_ERROR, Logger::LEVEL_WARNING];
break;
case self::INFO:
$levels = [Logger::LEVEL_ERROR, Logger::LEVEL_WARNING, Logger::LEVEL_INFO];
break;
case self::DEBUG:
$levels = [Logger::LEVEL_ERROR, Logger::LEVEL_WARNING, Logger::LEVEL_INFO, Logger::LEVEL_TRACE];
break;
default:
$levels = [Logger::LEVEL_ERROR, Logger::LEVEL_WARNING, Logger::LEVEL_INFO, Logger::LEVEL_TRACE];
}
return array_map(function ($level) {
return Logger::getLevelName($level);
}, $levels);
}
}
\ No newline at end of file
<?php
namespace deck\ServiceManager\enums;
class LogTargetEnum
{
const RabbitMQ = 1;
const File = 2;
const Syslog = 3;
const Http = 4;
const Stdout = 5;
public static function toString(int $target): string
{
switch ($target) {
case self::RabbitMQ:
return 'RabbitMQ';
case self::File:
return 'File';
case self::Syslog:
return 'Syslog';
case self::Http:
return 'Http';
case self::Stdout:
return 'Stdout';
default:
return 'Unknown';
}
}
public static function list(): array
{
return [
1 => 'RabbitMQ',
2 => 'File',
3 => 'Syslog',
5 => 'Stdout',
];
}
}
\ No newline at end of file
<?php
namespace deck\ServiceManager\enums;
class ParameterEnum
{
const ApiKey = 'api_key';
}
\ No newline at end of file
<?php
namespace deck\ServiceManager\enums;
class RuleEnum
{
const InfrastructureServiceIndex = 'infrastructure/service/index';
const InfrastructureLogIndex = 'infrastructure/log/index';
const InfrastructureServiceCreate = 'infrastructure/service/create';
const InfrastructureServiceUpdate = 'infrastructure/service/update';
const InfrastructureServiceDelete = 'infrastructure/service/delete';
const InfrastructureServiceParametersIndex = 'infrastructure/service/parameters/index';
const InfrastructureServiceParametersUpdate = 'infrastructure/service/parameters/update';
}
\ No newline at end of file
<?php
namespace deck\ServiceManager\enums;
use deck\ServiceManager\ServiceManager;
class ServiceTypeEnum
{
const Unknown = 0;
const Daemon = 1;
const Cron = 2;
public static function toString(int $type): string
{
switch ($type) {
case self::Daemon:
return ServiceManager::t('enums', 'ServiceType.Daemon');
case self::Cron:
return ServiceManager::t('enums', 'ServiceType.Cron');
default:
return ServiceManager::t('enums', 'ServiceType.Unknown');
}
}
public static function list(): array
{
return [
self::Daemon => ServiceManager::t('enums', 'ServiceType.Daemon'),
self::Cron => ServiceManager::t('enums', 'ServiceType.Cron'),
];
}
}
\ No newline at end of file
<?php
namespace deck\ServiceManager\formatters;
use Yii;
use yii\helpers\Json;
use yii\web\JsonResponseFormatter;
use yii\web\Response;
class ApiResponseFormatter extends JsonResponseFormatter
{
public function format($response)
{
if ($this->contentType === null) {
$this->contentType = $this->useJsonp
? self::CONTENT_TYPE_JSONP
: self::CONTENT_TYPE_JSON;
} elseif (strpos($this->contentType, 'charset') === false) {
$this->contentType .= '; charset=UTF-8';
}
$response->getHeaders()->set('Content-Type', $this->contentType);
if ($this->useJsonp) {
$this->formatJsonp($response);
} else {
$this->formatJson($response);
}
}
/**
* Formats response data in JSON format.
* @param Response $response
*/
protected function formatJson($response)
{
if (in_array($response->statusCode, [200, 201])) {
$response->data = [
'data' => $response->data,
'error' => null,
];
} else {
$response->data = [
'data' => null,
'error' => $response->data,
];
}
if ($response->data !== null) {
$options = $this->encodeOptions;
if ($this->prettyPrint) {
$options |= JSON_PRETTY_PRINT;
}
$response->content = Json::encode($response->data, $options);
} elseif ($response->content === null) {
$response->content = 'null';
}
}
/**
* Formats response data in JSONP format.
* @param Response $response
*/
protected function formatJsonp($response)
{
if (is_array($response->data)
&& isset($response->data['data'], $response->data['callback'])
) {
$response->content = sprintf(
'%s(%s);',
$response->data['callback'],
Json::htmlEncode($response->data['data'])
);
} elseif ($response->data !== null) {
$response->content = '';
Yii::warning(
"The 'jsonp' response requires that the data be an array consisting of both 'data' and 'callback' elements.",
__METHOD__
);
}
}
}
\ No newline at end of file
<?php
return [
'service.id' => 'ID',
'service.name' => 'Name',
'service.alias' => 'Alias',
'service.description' => 'Description',
'service.log_level' => 'Log Level',
'service.state' => 'State',
'service.version' => 'Version',
'service.service_type' => 'Service Type',
'service.cron' => 'Launch Interval',
'service.command' => 'Executable Command',
'service.created_at' => 'Creation Time',
'service.updated_at' => 'Update Time',
'service.deleted_at' => 'Deletion Time',
];
\ No newline at end of file
<?php
return [
'service.id' => 'Идентификатор',
'service.name' => 'Наименование',
'service.alias' => 'Алиас',
'service.description' => 'Описание',
'service.log_level' => 'Уровень логирования',
'service.state' => 'Состояние',
'service.version' => 'Версия',
'service.service_type' => 'Тип сервиса',
'service.cron' => 'Интервал запуска',
'service.command' => 'Запускаемая команда',
'service.created_at' => 'Время создания',
'service.updated_at' => 'Время обновления',
'service.deleted_at' => 'Время удаления',
];
\ No newline at end of file
<?php
namespace deck\ServiceManager\migrations\clickhouse;
use deck\php_clickhouse\Migration;
class M240920144720CreateLogTable extends Migration
{
public function up()
{
$this->execute("create table default.log
(
id UUID,
uuid Nullable(UUID),
datetime DateTime64(6),
message Nullable(String),
user Nullable(String),
severity String,
module String,
category Nullable(String),
code String,
ip Nullable(String),
trace Nullable(String)
)
engine = ReplacingMergeTree PARTITION BY toYYYYMM(datetime)
ORDER BY (datetime, severity, module, category, uuid, id)
SETTINGS index_granularity = 8192, allow_nullable_key = 1;
");
}
public function down()
{
$this->execute("drop table default.log");
}
}
\ No newline at end of file
<?php
namespace deck\ServiceManager\migrations;
use yii\db\Migration;
/**
* Class m240919_062035_create_infrastructure_schema
*/
class m240919_062035_create_infrastructure_schema extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->execute('CREATE SCHEMA service_manager');
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->execute('DROP SCHEMA service_manager"');
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment