Skip to content

Commit

Permalink
Handle api and web middleware differently
Browse files Browse the repository at this point in the history
  • Loading branch information
GitSahib committed Nov 3, 2023
1 parent c5fd243 commit d5a2885
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 17 deletions.
10 changes: 0 additions & 10 deletions src/AppInsightsHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ public function __construct(AppInsightsServer $appInsights)
*/
public function trackPageViewDuration($request)
{
if($request->expectsJson())
{
return;
}

if (!$this->telemetryEnabled())
{
return;
Expand Down Expand Up @@ -158,11 +153,6 @@ private function getRequestPropertiesFromException(Throwable $e)
*/
public function flashPageInfo($request)
{
if($request->expectsJson())
{
return;
}

if (!$this->telemetryEnabled())
{
return;
Expand Down
46 changes: 46 additions & 0 deletions src/Middleware/AppInsightsApiMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace Larasahib\AppInsightsLaravel\Middleware;

use Closure;
use Larasahib\AppInsightsLaravel\AppInsightsHelpers;

class AppInsightsApiMiddleware
{

/**
* @var AppInsightsHelpers
*/
private AppInsightsHelpers $appInsightsHelpers;


/**
* @param AppInsightsHelpers $appInsightssHelpers
*/
public function __construct(AppInsightsHelpers $appInsightsHelpers)
{
$this->appInsightsHelpers = $appInsightsHelpers;
}

/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
return $next($request);
}

/**
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Response $response
* @return void
*/
public function terminate($request, $response)
{
$this->appInsightsHelpers->trackRequest($request, $response);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Closure;
use Larasahib\AppInsightsLaravel\AppInsightsHelpers;

class AppInsightsMiddleware
class AppInsightsWebMiddleware
{

/**
Expand All @@ -19,7 +19,7 @@ class AppInsightsMiddleware
public function __construct(AppInsightsHelpers $appInsightsHelpers)
{
$this->appInsightsHelpers = $appInsightsHelpers;
}
}

/**
* Handle an incoming request.
Expand Down
17 changes: 12 additions & 5 deletions src/Providers/AppInsightsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use ApplicationInsights\Telemetry_Client;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
use Larasahib\AppInsightsLaravel\Queue\AppInsightsTelemeteryQueue;
use Larasahib\AppInsightsLaravel\Middleware\AppInsightsMiddleware;
use Larasahib\AppInsightsLaravel\Middleware\AppInsightsWebMiddleware;
use Larasahib\AppInsightsLaravel\Middleware\AppInsightsApiMiddleware;
use Larasahib\AppInsightsLaravel\AppInsightsClient;
use Larasahib\AppInsightsLaravel\AppInsightsHelpers;
use Larasahib\AppInsightsLaravel\AppInsightsServer;
Expand Down Expand Up @@ -40,9 +41,14 @@ public function register()
return new AppInsightsServer($telemetryClient);
});

$this->app->singleton('AppInsightsMiddleware', function ($app) {
$this->app->singleton('AppInsightsWebMiddleware', function ($app) {
$appInsightsHelpers = new AppInsightsHelpers($app['AppInsightsServer']);
return new AppInsightsMiddleware($appInsightsHelpers);
return new AppInsightsWebMiddleware($appInsightsHelpers);
});

$this->app->singleton('AppInsightsApiMiddleware', function ($app) {
$appInsightsHelpers = new AppInsightsHelpers($app['AppInsightsServer']);
return new AppInsightsApiMiddleware($appInsightsHelpers);
});

$this->app->singleton('AppInsightsClient', function ($app) {
Expand All @@ -59,8 +65,9 @@ public function provides() {

return [
'AppInsightsServer',
'AppInsightsMiddleware',
'AppInsightsClient'
'AppInsightsWebMiddleware',
'AppInsightsClient',
"AppInsightsApiMiddleware"
];
}

Expand Down

0 comments on commit d5a2885

Please sign in to comment.