Skip to content

Commit

Permalink
ability to not register web routes for Log Viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
arukompas committed Apr 10, 2024
1 parent f7ae8b1 commit 20a3bab
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
2 changes: 2 additions & 0 deletions config/log-viewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

'enabled' => env('LOG_VIEWER_ENABLED', true),

'api_only' => env('LOG_VIEWER_API_ONLY', false),

'require_auth_in_production' => true,

/*
Expand Down
18 changes: 10 additions & 8 deletions src/LogViewerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,16 @@ protected function registerRoutes()
$this->loadRoutesFrom(self::basePath('/routes/api.php'));
});

Route::group([
'domain' => config('log-viewer.route_domain', null),
'prefix' => config('log-viewer.route_path'),
'namespace' => 'Opcodes\LogViewer\Http\Controllers',
'middleware' => config('log-viewer.middleware', null),
], function () {
$this->loadRoutesFrom(self::basePath('/routes/web.php'));
});
if (! config('log-viewer.api_only', false)) {
Route::group([
'domain' => config('log-viewer.route_domain', null),
'prefix' => config('log-viewer.route_path'),
'namespace' => 'Opcodes\LogViewer\Http\Controllers',
'middleware' => config('log-viewer.middleware', null),
], function () {
$this->loadRoutesFrom(self::basePath('/routes/web.php'));
});
}
}

protected function registerResources()
Expand Down
22 changes: 22 additions & 0 deletions tests/Feature/RoutesTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Symfony\Component\Routing\Exception\RouteNotFoundException;

test('the default url can be changed', function () {
config()->set('log-viewer.route_path', 'new-log-route');

Expand All @@ -25,6 +27,22 @@
expect(route('log-viewer.index'))->toBe('http://localhost');
});

test('only use api', function () {
config()->set('log-viewer.api_only', true);

reloadRoutes();

route('log-viewer.index');
})->throws(RouteNotFoundException::class);

test('only both api and web', function () {
config()->set('log-viewer.api_only', false);

reloadRoutes();

expect(route('log-viewer.index'))->toBe('http://localhost/log-viewer');
});

/*
|--------------------------------------------------------------------------
| HELPERS
Expand All @@ -33,5 +51,9 @@

function reloadRoutes(): void
{
// unset any routes that were set previously
app('router')->setRoutes(new \Illuminate\Routing\RouteCollection());

// boot the service provider to register the routes again
(new \Opcodes\LogViewer\LogViewerServiceProvider(app()))->boot();
}

0 comments on commit 20a3bab

Please sign in to comment.