File size: 4,015 Bytes
b2dcf0f
 
45dc401
 
 
b2dcf0f
 
 
 
 
 
 
 
 
6773345
b2dcf0f
 
6773345
b2dcf0f
 
 
 
 
 
 
 
 
 
 
 
 
6773345
b2dcf0f
 
 
 
 
6773345
b2dcf0f
 
45dc401
 
 
 
 
 
 
 
 
 
 
b2dcf0f
 
 
 
 
bc7b87b
 
 
 
 
b2dcf0f
 
 
 
 
 
 
 
bc7b87b
 
 
 
 
b2dcf0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9873e42
 
777a095
 
9873e42
 
b2dcf0f
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php

use App\Console\Commands\RollupAnalyticsFunnels;
use App\Console\Commands\RollupAnalyticsPageViews;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use App\Shared\Exceptions\ApiException;
use App\Shared\Exceptions\ValidationExceptionMapper;
use App\Http\Middleware\AuthenticateAccessToken;
use App\Http\Middleware\RequirePermission;
use App\Http\Middleware\RequirePortal;
use App\Shared\Http\ApiResponse;
use App\Shared\Http\RequestIdMiddleware;
use Illuminate\Http\Middleware\HandleCors;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
        apiPrefix: 'api/v1',
    )
    ->withMiddleware(function (Middleware $middleware): void {
        $middleware->prepend([
            HandleCors::class,
            RequestIdMiddleware::class,
        ]);
        $middleware->alias([
            'mdn.auth' => AuthenticateAccessToken::class,
            'mdn.permission' => RequirePermission::class,
            'mdn.portal' => RequirePortal::class,
        ]);
    })
    ->withSchedule(function (Schedule $schedule): void {
        // Analytics rollups run after midnight UTC so all same-day beacon
        // events have been flushed through the queue before aggregation.
        $schedule->command(RollupAnalyticsPageViews::class)->dailyAt('02:00')
            ->withoutOverlapping()
            ->runInBackground();

        $schedule->command(RollupAnalyticsFunnels::class)->dailyAt('02:15')
            ->withoutOverlapping()
            ->runInBackground();
    })
    ->withExceptions(function (Exceptions $exceptions): void {
        $exceptions->shouldRenderJsonWhen(
            fn (Request $request) => $request->is('api/*'),
        );

        $exceptions->render(function (ApiException $exception, Request $request) {
            if (! $request->is('api/*')) {
                return null;
            }

            return ApiResponse::error(
                $exception->apiCode(),
                $exception->getMessage(),
                $exception->details(),
                $exception->status(),
            );
        });

        $exceptions->render(function (ValidationException $exception, Request $request) {
            if (! $request->is('api/*')) {
                return null;
            }

            return ApiResponse::error(
                'VALIDATION_FAILED',
                'The request payload is invalid.',
                ValidationExceptionMapper::details($exception),
                422,
            );
        });

        $exceptions->render(function (NotFoundHttpException $exception, Request $request) {
            if (! $request->is('api/*')) {
                return null;
            }

            return ApiResponse::error('NOT_FOUND', 'The requested endpoint was not found.', [], 404);
        });

        $exceptions->render(function (\Throwable $exception, Request $request) {
            if (! $request->is('api/*')) {
                return null;
            }

            if ($exception instanceof HttpExceptionInterface) {
                return ApiResponse::error(
                    'HTTP_ERROR',
                    $exception->getMessage() ?: 'The request could not be completed.',
                    [],
                    $exception->getStatusCode(),
                );
            }

            return ApiResponse::error(
                'INTERNAL_SERVER_ERROR',
                'An unexpected error occurred.',
                [],
                500,
            );
        });
    })->create();