-
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathCachetCoreServiceProvider.php
More file actions
250 lines (214 loc) · 8.04 KB
/
CachetCoreServiceProvider.php
File metadata and controls
250 lines (214 loc) · 8.04 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
namespace Cachet;
use BladeUI\Icons\Factory;
use Cachet\Documentation\AddAuthenticationToOperation;
use Cachet\Listeners\SendWebhookListener;
use Cachet\Listeners\WebhookCallEventListener;
use Cachet\Models\Incident;
use Cachet\Models\Schedule;
use Cachet\Settings\AppSettings;
use Cachet\View\ViewManager;
use Dedoc\Scramble\Scramble;
use Dedoc\Scramble\Support\Generator\OpenApi;
use Dedoc\Scramble\Support\Generator\SecurityScheme;
use Filament\Support\Colors\Color;
use Filament\Support\Facades\FilamentColor;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Console\AboutCommand;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Spatie\LaravelSettings\Exceptions\MissingSettings;
use Spatie\WebhookServer\Events\WebhookCallFailedEvent;
use Spatie\WebhookServer\Events\WebhookCallSucceededEvent;
class CachetCoreServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
if (! defined('CACHET_PATH')) {
define('CACHET_PATH', dirname(__DIR__).'/');
}
$this->app->singleton(Cachet::class);
$this->app->singleton(ViewManager::class);
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
if (! $this->app->configurationIsCached()) {
$this->mergeConfigFrom(__DIR__.'/../config/cachet.php', 'cachet');
}
Route::middlewareGroup('cachet', config('cachet.middleware', []));
Route::middlewareGroup('cachet:api', config('cachet.api_middleware', []));
Relation::morphMap([
'incident' => Incident::class,
'schedule' => Schedule::class,
]);
$this->registerCommands();
$this->registerSchedules();
$this->registerResources();
$this->registerPublishing();
$this->registerBladeComponents();
Event::listen('Cachet\Events\Incidents\*', SendWebhookListener::class);
Event::listen([WebhookCallSucceededEvent::class, WebhookCallFailedEvent::class], WebhookCallEventListener::class);
Http::globalRequestMiddleware(fn ($request) => $request->withHeader(
'User-Agent', Cachet::USER_AGENT
));
FilamentColor::register([
'cachet' => Color::rgb('rgb(4, 193, 71)'),
]);
$this->configureScramble();
}
/**
* Register the package's resources such as routes, migrations, etc.
*/
private function registerResources(): void
{
$this->loadViewsFrom(__DIR__.'/../resources/views', 'cachet');
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
$this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'cachet');
$this->configureRateLimiting();
$this->registerRoutes();
}
/**
* Configure the rate limiting for the application.
*/
private function configureRateLimiting(): void
{
RateLimiter::for('cachet-api', function ($request) {
return Limit::perMinute(config('cachet.api_rate_limit', 300))
->by($request->user()?->id ?: $request->ip());
});
}
/**
* Register Cachet's routes.
*/
private function registerRoutes(): void
{
$this->callAfterResolving('router', function (Router $router, Application $application) {
$apiEnabled = true;
$apiProtected = false;
try {
$settings = app(AppSettings::class);
$apiEnabled = $settings->api_enabled;
$apiProtected = $settings->api_protected;
} catch (MissingSettings $exception) {
if(! $application->runningInConsole()) {
throw new \Exception("Please run `php artisan migrate` to load missing settings.");
}
} catch (\Exception $exception) {
// do nothing
}
if ($apiEnabled) {
$middleware = ['cachet:api', 'throttle:cachet-api'];
if ($apiProtected) {
$middleware[] = 'auth:sanctum';
}
$router->group([
'domain' => config('cachet.domain'),
'as' => 'cachet.api.',
'prefix' => Cachet::path().'/api',
'middleware' => $middleware,
], function (Router $router) {
$this->loadRoutesFrom(__DIR__.'/../routes/api.php');
});
}
Cachet::routes()
->register();
});
}
/**
* Register the package's publishable resources.
*/
private function registerPublishing(): void
{
if (! $this->app->runningInConsole()) {
return;
}
$this->publishes([
__DIR__.'/../config/cachet.php' => config_path('cachet.php'),
], ['cachet', 'cachet-config']);
$this->publishes([
__DIR__.'/../resources/views/status-page/index.blade.php' => resource_path('views/vendor/cachet/status-page/index.blade.php'),
], ['cachet', 'cachet-views']);
$this->publishes([
__DIR__.'/../public/' => public_path('vendor/cachethq/cachet'),
], ['cachet', 'cachet-assets']);
}
/**
* Register the package's Blade components.
*/
private function registerBladeComponents(): void
{
Blade::componentNamespace('Cachet\\View\\Components', 'cachet');
$this->callAfterResolving(Factory::class, function (Factory $factory) {
$factory->add('cachet', [
'path' => __DIR__.'/../resources/svg',
'prefix' => 'cachet',
]);
});
}
/**
* Register the package's commands.
*/
private function registerCommands(): void
{
if ($this->app->runningInConsole()) {
$this->commands([
Commands\MakeUserCommand::class,
Commands\SendBeaconCommand::class,
Commands\VersionCommand::class,
]);
AboutCommand::add('Cachet', fn () => [
'Beacon' => AboutCommand::format(config('cachet.beacon'), console: fn ($value) => $value ? '<fg=yellow;options=bold>ENABLED</>' : 'OFF'),
'Enabled' => AboutCommand::format(config('cachet.enabled'), console: fn ($value) => $value ? '<fg=yellow;options=bold>ENABLED</>' : 'OFF'),
'Install ID' => app(AppSettings::class)->install_id,
'Version' => app(Cachet::class)->version(),
]);
}
}
/**
* Register the package's schedules.
*/
private function registerSchedules(): void
{
if (! $this->app->runningInConsole()) {
return;
}
$this->app->booted(function () {
$schedule = $this->app->make(\Illuminate\Console\Scheduling\Schedule::class);
$demoMode = fn () => Cachet::demoMode();
$schedule->command('cachet:beacon')->daily();
$schedule->command('db:seed', [
'--class' => \Cachet\Database\Seeders\DatabaseSeeder::class,
'--force' => true,
])->everyThirtyMinutes()->when($demoMode);
});
}
/**
* Scramble is installed as dev dependency hence the class existence check.
*/
private function configureScramble(): void
{
if (! class_exists(Scramble::class)) {
return;
}
Scramble::afterOpenApiGenerated(function (OpenApi $openApi) {
$openApi->info->description = 'API documentation for Cachet, the open-source, self-hosted status page system.';
$openApi->secure(
SecurityScheme::http('bearer')
);
});
Scramble::registerExtension(AddAuthenticationToOperation::class);
}
}