diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 6e6b5c3..eb3d192 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -22,6 +22,10 @@ jobs: with: php-version: 8.3 + - name: Set up dummy env for unit tests + run: | + touch .env && echo "APP_ENV=local" >> .env + - name: Install librdkafka and rdkafka extension run: | sudo apt-get install -y librdkafka-dev diff --git a/src/Libraries/StorageDrivers/MinioAdapter.php b/src/Libraries/StorageDrivers/MinioAdapter.php index 9e81450..6e8aa35 100644 --- a/src/Libraries/StorageDrivers/MinioAdapter.php +++ b/src/Libraries/StorageDrivers/MinioAdapter.php @@ -122,14 +122,15 @@ public function move(string $path, string $newpath): bool /** * Get all files in a directory * - * @param string $dirpath path of the directory + * @param string $dirpath path of the directory + * @param bool $recursive whether to get files recursively or not * * @return bool */ - public function allFiles(string $dirpath): array + public function allFiles(string $dirpath, bool $recursive = false): array { try { - $paths = $this->listContents($dirpath); + $paths = $this->listContents($dirpath, $recursive); return $paths; } catch (\Throwable $th) { throw $th; diff --git a/src/Libraries/StorageDrivers/NFS.php b/src/Libraries/StorageDrivers/NFS.php index b19110c..ac01180 100644 --- a/src/Libraries/StorageDrivers/NFS.php +++ b/src/Libraries/StorageDrivers/NFS.php @@ -91,7 +91,8 @@ public function delete(string $filepath): bool if (!is_file($filepath)) { return true; } - if (!exec("rm $filepath")) { + exec("rm $filepath", $output, $exit_code); + if ($exit_code != 0) { throw new RuntimeException("Failed to delete $filepath"); } return true; @@ -175,7 +176,7 @@ public function securelinkFolder(string $dirpath): array * * @return void */ - private function createDirectory(string $dirpath): void + public function createDirectory(string $dirpath): void { try { if (!is_dir($dirpath)) { diff --git a/src/Logs/TraitLog.php b/src/Logs/TraitLog.php index 2d1ace7..53e35cf 100644 --- a/src/Logs/TraitLog.php +++ b/src/Logs/TraitLog.php @@ -63,6 +63,8 @@ public function info(array $data): void */ private function getEmbeddedInfo(array &$data): void { + $data['taskname'] = env('TASK_NAME'); + $data['hostname'] = gethostname(); if ($meta = $this->context->get(Metadata::class)) { if ($meta instanceof Metadata) { if (isset($meta->req_id)) { diff --git a/src/Providers/MinioStorageServiceProvider.php b/src/Providers/MinioStorageServiceProvider.php new file mode 100644 index 0000000..ecca2e6 --- /dev/null +++ b/src/Providers/MinioStorageServiceProvider.php @@ -0,0 +1,86 @@ + + * @license https://mit-license.org/ MIT License + * @version GIT: 0.3.1 + * @link https://github.com/spotlibs + */ + +declare(strict_types=1); + +namespace Spotlibs\PhpLib\Providers; + +use Aws\S3\S3Client; +use Illuminate\Support\ServiceProvider; +use League\Flysystem\AwsS3v3\AwsS3Adapter; +use Illuminate\Support\Facades\Storage; +use Spotlibs\PhpLib\Libraries\StorageDrivers\MinioAdapter; + +/** + * MinioStorageServiceProvider + * + * Service provider for MinIO storage + * + * @category StandardService + * @package ServiceProvider + * @author Made Mas Adi Winata + * @license https://mit-license.org/ MIT License + * @link https://github.com/spotlibs + */ +class MinioStorageServiceProvider extends ServiceProvider +{ + /** + * Bootstrap the application services. + * + * @return void + */ + public function boot() + { + Storage::extend('minio', function ($app, $config) { + // Client for internal Docker network communication + $client = new S3Client([ + 'credentials' => [ + 'key' => $config["key"], + 'secret' => $config["secret"] + ], + 'region' => $config["region"], + 'version' => "latest", + 'bucket_endpoint' => false, + 'use_path_style_endpoint' => true, + 'endpoint' => $config["endpoint"], // e.g., http://minio:9000 + ]); + + $adapter = new AwsS3Adapter($client, $config["bucket"]); + + // Public client for browser-accessible URLs + $publicClient = new S3Client([ + 'credentials' => [ + 'key' => $config['key'], + 'secret' => $config['secret'] + ], + 'region' => $config['region'], + 'version' => 'latest', + 'use_path_style_endpoint' => true, + 'endpoint' => $config['url'] ?? $config['endpoint'], // e.g., http://localhost:9000 + ]); + + // Return your custom MinioAdapter (which extends Filesystem) + return new MinioAdapter($adapter, $config, $publicClient); + }); + } + + /** + * Register the application services. + * + * @return void + */ + public function register() + { + + } +} diff --git a/tests/Logs/LogTest.php b/tests/Logs/LogTest.php index dd6bd62..38557a1 100644 --- a/tests/Logs/LogTest.php +++ b/tests/Logs/LogTest.php @@ -13,7 +13,6 @@ class LogTest extends TestCase { private function setContext(): void { - putenv('APP_ENV=local'); $meta = new Metadata(); $meta->req_id = '123123'; $meta->identifier = 'spotlibs-unittest'; @@ -23,7 +22,6 @@ private function setContext(): void private function setContext2(): void { - putenv('APP_ENV=local'); $meta = new Metadata(); $meta->task_id = '123123'; $meta->identifier = 'spotlibs-unittest'; @@ -31,7 +29,7 @@ private function setContext2(): void $context->set(Metadata::class, $meta); } - private string $expected = '{"test":"let me know","traceID":"123123","identifier":"spotlibs-unittest"}'; + private string $expected = '{"test":"let me know","taskname":null,"hostname":"--- IGNORE ---","traceID":"123123","identifier":"spotlibs-unittest"}'; public function testCreateRuntimeErrorLog() { @@ -44,6 +42,7 @@ public function testCreateRuntimeErrorLog() $temp = explode(':: ', $logDetail); $logLevel = $temp[0]; $logMessage = trim($temp[1]); + $logMessage = json_encode($this->removeHostname(json_decode($logMessage, true))); $this->assertEquals('ERROR', $logLevel); $this->assertEquals($this->expected, $logMessage); } @@ -58,6 +57,7 @@ public function testCreateRuntimeInfoLog() $temp = explode(':: ', $logDetail); $logLevel = $temp[0]; $logMessage = trim($temp[1]); + $logMessage = json_encode($this->removeHostname(json_decode($logMessage, true))); $this->assertEquals('INFO', $logLevel); $this->assertEquals($this->expected, $logMessage); } @@ -72,6 +72,7 @@ public function testCreateRuntimeWarningLog() $temp = explode(':: ', $logDetail); $logLevel = $temp[0]; $logMessage = trim($temp[1]); + $logMessage = json_encode($this->removeHostname(json_decode($logMessage, true))); $this->assertEquals('WARNING', $logLevel); $this->assertEquals($this->expected, $logMessage); } @@ -86,6 +87,7 @@ public function testCreateActivityInfoLog() $temp = explode(':: ', $logDetail); $logLevel = $temp[0]; $logMessage = trim($temp[1]); + $logMessage = json_encode($this->removeHostname(json_decode($logMessage, true))); $this->assertEquals('INFO', $logLevel); $this->assertEquals($this->expected, $logMessage); } @@ -100,7 +102,13 @@ public function testCreateWorkerInfoLog() $temp = explode(':: ', $logDetail); $logLevel = $temp[0]; $logMessage = trim($temp[1]); + $logMessage = json_encode($this->removeHostname(json_decode($logMessage, true))); $this->assertEquals('INFO', $logLevel); $this->assertEquals($this->expected, $logMessage); } + private function removeHostname(array $logData): array + { + $logData['hostname'] = '--- IGNORE ---'; + return $logData; + } } \ No newline at end of file