From d2ab9e326c1ae45d836fb7f12f45eaa365a4c690 Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Tue, 30 May 2023 15:10:17 +0300 Subject: [PATCH] configuration stuff --- bootstrap/app.php | 3 + composer.json | 1 + composer.lock | 213 +++++++++++++++++++++++++++++++++++++++++++- config/app.php | 2 +- config/database.php | 74 +++------------ docker-compose.yaml | 17 ++++ routes/api.php | 76 +++++++++++++--- 7 files changed, 311 insertions(+), 75 deletions(-) create mode 100644 docker-compose.yaml diff --git a/bootstrap/app.php b/bootstrap/app.php index 037e17d..1568672 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -11,10 +11,13 @@ | */ + $app = new Illuminate\Foundation\Application( $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__) ); +$app->register(Jenssegers\Mongodb\MongodbServiceProvider::class); + /* |-------------------------------------------------------------------------- | Bind Important Interfaces diff --git a/composer.json b/composer.json index f952a0c..078b0fc 100644 --- a/composer.json +++ b/composer.json @@ -7,6 +7,7 @@ "require": { "php": "^8.2.0", "guzzlehttp/guzzle": "^7.2", + "jenssegers/mongodb": "^3.9", "laravel/framework": "^9.19", "laravel/sanctum": "^3.0", "laravel/tinker": "^2.7" diff --git a/composer.lock b/composer.lock index cf2db4f..db9a8cf 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "8490a44bb6e588439e76d6ae4e357844", + "content-hash": "ac7eef60dd12024b114a9f511ed7057b", "packages": [ { "name": "brick/math", @@ -974,6 +974,147 @@ ], "time": "2021-10-07T12:57:01+00:00" }, + { + "name": "jean85/pretty-package-versions", + "version": "2.0.5", + "source": { + "type": "git", + "url": "https://github.com/Jean85/pretty-package-versions.git", + "reference": "ae547e455a3d8babd07b96966b17d7fd21d9c6af" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Jean85/pretty-package-versions/zipball/ae547e455a3d8babd07b96966b17d7fd21d9c6af", + "reference": "ae547e455a3d8babd07b96966b17d7fd21d9c6af", + "shasum": "" + }, + "require": { + "composer-runtime-api": "^2.0.0", + "php": "^7.1|^8.0" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^2.17", + "jean85/composer-provided-replaced-stub-package": "^1.0", + "phpstan/phpstan": "^0.12.66", + "phpunit/phpunit": "^7.5|^8.5|^9.4", + "vimeo/psalm": "^4.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Jean85\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Alessandro Lai", + "email": "alessandro.lai85@gmail.com" + } + ], + "description": "A library to get pretty versions strings of installed dependencies", + "keywords": [ + "composer", + "package", + "release", + "versions" + ], + "support": { + "issues": "https://github.com/Jean85/pretty-package-versions/issues", + "source": "https://github.com/Jean85/pretty-package-versions/tree/2.0.5" + }, + "time": "2021-10-08T21:21:46+00:00" + }, + { + "name": "jenssegers/mongodb", + "version": "v3.9.5", + "source": { + "type": "git", + "url": "https://github.com/jenssegers/laravel-mongodb.git", + "reference": "6ce35ace85a5946f943d7f493f93aebb9a6d129d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/jenssegers/laravel-mongodb/zipball/6ce35ace85a5946f943d7f493f93aebb9a6d129d", + "reference": "6ce35ace85a5946f943d7f493f93aebb9a6d129d", + "shasum": "" + }, + "require": { + "illuminate/container": "^9.0", + "illuminate/database": "^9.0", + "illuminate/events": "^9.0", + "illuminate/support": "^9.0", + "mongodb/mongodb": "^1.11" + }, + "require-dev": { + "doctrine/dbal": "^2.13.3|^3.1.4", + "mockery/mockery": "^1.3.1", + "orchestra/testbench": "^7.0", + "phpunit/phpunit": "^9.5.8" + }, + "suggest": { + "jenssegers/mongodb-sentry": "Add Sentry support to Laravel-MongoDB", + "jenssegers/mongodb-session": "Add MongoDB session support to Laravel-MongoDB" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Jenssegers\\Mongodb\\MongodbServiceProvider", + "Jenssegers\\Mongodb\\MongodbQueueServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Jenssegers\\Mongodb\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jens Segers", + "homepage": "https://jenssegers.com" + } + ], + "description": "A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)", + "homepage": "https://github.com/jenssegers/laravel-mongodb", + "keywords": [ + "database", + "eloquent", + "laravel", + "model", + "moloquent", + "mongo", + "mongodb" + ], + "support": { + "issues": "https://github.com/jenssegers/laravel-mongodb/issues", + "source": "https://github.com/jenssegers/laravel-mongodb/tree/v3.9.5" + }, + "funding": [ + { + "url": "https://github.com/jenssegers", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/jenssegers/mongodb", + "type": "tidelift" + } + ], + "time": "2023-02-16T12:20:36+00:00" + }, { "name": "laravel/framework", "version": "v9.52.7", @@ -1758,6 +1899,76 @@ ], "time": "2022-04-17T13:12:02+00:00" }, + { + "name": "mongodb/mongodb", + "version": "1.15.0", + "source": { + "type": "git", + "url": "https://github.com/mongodb/mongo-php-library.git", + "reference": "3a681a3b2f2c0ebac227a3b86bb9057d0e6eb8f8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mongodb/mongo-php-library/zipball/3a681a3b2f2c0ebac227a3b86bb9057d0e6eb8f8", + "reference": "3a681a3b2f2c0ebac227a3b86bb9057d0e6eb8f8", + "shasum": "" + }, + "require": { + "ext-hash": "*", + "ext-json": "*", + "ext-mongodb": "^1.15.0", + "jean85/pretty-package-versions": "^1.2 || ^2.0.1", + "php": "^7.2 || ^8.0", + "symfony/polyfill-php80": "^1.19" + }, + "require-dev": { + "doctrine/coding-standard": "^9.0", + "squizlabs/php_codesniffer": "^3.6", + "symfony/phpunit-bridge": "^5.2", + "vimeo/psalm": "^4.28" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.15.x-dev" + } + }, + "autoload": { + "files": [ + "src/functions.php" + ], + "psr-4": { + "MongoDB\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Andreas Braun", + "email": "andreas.braun@mongodb.com" + }, + { + "name": "Jeremy Mikola", + "email": "jmikola@gmail.com" + } + ], + "description": "MongoDB driver library", + "homepage": "https://jira.mongodb.org/browse/PHPLIB", + "keywords": [ + "database", + "driver", + "mongodb", + "persistence" + ], + "support": { + "issues": "https://github.com/mongodb/mongo-php-library/issues", + "source": "https://github.com/mongodb/mongo-php-library/tree/1.15.0" + }, + "time": "2022-11-23T04:45:35+00:00" + }, { "name": "monolog/monolog", "version": "2.9.1", diff --git a/config/app.php b/config/app.php index ef76a7e..a553d24 100644 --- a/config/app.php +++ b/config/app.php @@ -181,7 +181,7 @@ Illuminate\Translation\TranslationServiceProvider::class, Illuminate\Validation\ValidationServiceProvider::class, Illuminate\View\ViewServiceProvider::class, - + Jenssegers\Mongodb\MongodbServiceProvider::class, /* * Package Service Providers... */ diff --git a/config/database.php b/config/database.php index 137ad18..988dee1 100644 --- a/config/database.php +++ b/config/database.php @@ -15,7 +15,7 @@ | */ - 'default' => env('DB_CONNECTION', 'mysql'), + 'default' => env('DB_CONNECTION', 'mongodb'), /* |-------------------------------------------------------------------------- @@ -32,67 +32,19 @@ | choice installed on your machine before you begin development. | */ - + 'mongodb' => [ + 'driver' => 'mongodb', + 'host' => env('DB_HOST', 'localhost'), + 'port' => env('DB_PORT', 27017), + 'database' => env('DB_DATABASE'), + 'username' => env('DB_USERNAME'), + 'password' => env('DB_PASSWORD'), + 'options' => [ + 'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'), + ], + ], 'connections' => [ - - 'sqlite' => [ - 'driver' => 'sqlite', - 'url' => env('DATABASE_URL'), - 'database' => env('DB_DATABASE', database_path('database.sqlite')), - 'prefix' => '', - 'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true), - ], - - 'mysql' => [ - 'driver' => 'mysql', - 'url' => env('DATABASE_URL'), - 'host' => env('DB_HOST', '127.0.0.1'), - 'port' => env('DB_PORT', '3306'), - 'database' => env('DB_DATABASE', 'forge'), - 'username' => env('DB_USERNAME', 'forge'), - 'password' => env('DB_PASSWORD', ''), - 'unix_socket' => env('DB_SOCKET', ''), - 'charset' => 'utf8mb4', - 'collation' => 'utf8mb4_unicode_ci', - 'prefix' => '', - 'prefix_indexes' => true, - 'strict' => true, - 'engine' => null, - 'options' => extension_loaded('pdo_mysql') ? array_filter([ - PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), - ]) : [], - ], - - 'pgsql' => [ - 'driver' => 'pgsql', - 'url' => env('DATABASE_URL'), - 'host' => env('DB_HOST', '127.0.0.1'), - 'port' => env('DB_PORT', '5432'), - 'database' => env('DB_DATABASE', 'forge'), - 'username' => env('DB_USERNAME', 'forge'), - 'password' => env('DB_PASSWORD', ''), - 'charset' => 'utf8', - 'prefix' => '', - 'prefix_indexes' => true, - 'search_path' => 'public', - 'sslmode' => 'prefer', - ], - - 'sqlsrv' => [ - 'driver' => 'sqlsrv', - 'url' => env('DATABASE_URL'), - 'host' => env('DB_HOST', 'localhost'), - 'port' => env('DB_PORT', '1433'), - 'database' => env('DB_DATABASE', 'forge'), - 'username' => env('DB_USERNAME', 'forge'), - 'password' => env('DB_PASSWORD', ''), - 'charset' => 'utf8', - 'prefix' => '', - 'prefix_indexes' => true, - // 'encrypt' => env('DB_ENCRYPT', 'yes'), - // 'trust_server_certificate' => env('DB_TRUST_SERVER_CERTIFICATE', 'false'), - ], - + ], /* diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..4b701f8 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,17 @@ +version: '3' +services: + mongodb: + image: arm64v8/mongo:4.0 + restart: always + ports: + - 27017:27017 + volumes: + - mongodb_data:/data/db + - ./init-scripts/init.js:/docker-entrypoint-initdb.d/mongo-init.js + environment: + - MONGO_INITDB_DATABASE=mydb + - MONGO_INITDB_ROOT_USERNAME=${MONGO_INITDB_ROOT_USERNAME:-root} + - MONGO_INITDB_ROOT_PASSWORD=${MONGO_INITDB_ROOT_PASSWORD:-root} + platform: linux/arm64/v8 +volumes: + mongodb_data: \ No newline at end of file diff --git a/routes/api.php b/routes/api.php index eb6fa48..f92f2b8 100644 --- a/routes/api.php +++ b/routes/api.php @@ -2,18 +2,70 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; +use GuzzleHttp\Client; +use Illuminate\Support\Facades\DB; +use MongoDB\Client as MongoClient; -/* -|-------------------------------------------------------------------------- -| API Routes -|-------------------------------------------------------------------------- -| -| Here is where you can register API routes for your application. These -| routes are loaded by the RouteServiceProvider within a group which -| is assigned the "api" middleware group. Enjoy building your API! -| -*/ +Route::get('/crawl', function (Request $request) { -Route::middleware('auth:sanctum')->get('/user', function (Request $request) { - return $request->user(); + $url = $request->input('url'); + check_connection_to_mongodb(); + + if (!$url) { + return response()->json([ + 'error' => 'Missing required parameter `url`' + ], 400); + } + $depth = $request->input('depth', 3); // default depth is 3 if not provided + $visitedUrls = []; + crawlWebsite($url, $depth, $visitedUrls); }); + + +function check_connection_to_mongodb() { + $connection = new MongoClient(); + echo '
';
+    echo "IT WORKS";
+    die;
+    print_r($connection);
+    die;
+}
+function crawlWebsite($url, $depth, &$visitedUrls)
+{
+    // Check if URL has already been visited
+    if (in_array($url, $visitedUrls)) {
+        return;
+    }
+
+    $visitedUrls[] = $url;
+
+    // Use GuzzleHttp client to send HTTP requests
+    $client = new Client();
+    $response = $client->get($url);
+
+    // Check if the HTTP response is successful (status code 2xx)
+    if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
+
+        // echo $response->getBody()->getContents();
+
+        // Insert page info into the database
+        // DB::table('pages')->insert([
+        //     'url' => $url,
+        //     'content' => $response->getBody()->getContents()
+        // ]);
+        // Crawl the links on the page
+        // if ($depth > 0) {
+        //     $body = $response->getBody()->getContents();
+        //     $dom = new DOMDocument();
+        //     @$dom->loadHTML($body);
+
+        //     $links = $dom->getElementsByTagName('a');
+        //     foreach ($links as $link) {
+        //         $href = $link->getAttribute('href');
+        //         if (filter_var($href, FILTER_VALIDATE_URL)) {
+        //             crawlWebsite($href, $depth - 1, $visitedUrls);
+        //         }
+        //     }
+        // }
+    }
+}