first release
This commit is contained in:
1 parent
647d62f787
commit
f7e5575cfa
6 files changed
+139
-16
No files matched your search
@@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
// config for MattitjaAB/LaravelPlausibleProxy
|
// Config for Laravel Plausible Proxy
|
||||||
return [
|
|
||||||
|
|
||||||
|
return [
|
||||||
|
'domain' => env('PLAUSIBLE_DOMAIN', 'https://plausible.io'),
|
||||||
|
'site' => env('PLAUSIBLE_SITE', 'example.com'),
|
||||||
];
|
];
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Concurrency;
|
||||||
|
use Illuminate\Support\Facades\Http;
|
||||||
|
use Illuminate\Support\Facades\Route;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
|
||||||
|
Route::post('api/event', function (Request $request) {
|
||||||
|
$validator = Validator::make($request->all(), [
|
||||||
|
'n' => 'required|string',
|
||||||
|
'u' => 'required|url',
|
||||||
|
'd' => 'required|string',
|
||||||
|
'r' => 'nullable|url',
|
||||||
|
'm' => 'nullable|string',
|
||||||
|
'p' => 'nullable|array',
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($validator->fails()) {
|
||||||
|
return response()->json(['status' => 'error', 'errors' => $validator->errors()], 422);
|
||||||
|
}
|
||||||
|
|
||||||
|
Concurrency::defer(fn () => Http::post(config('plausible-proxy.domain').'/api/event', $request->all()));
|
||||||
|
|
||||||
|
return response()->json(['status' => 'queued']);
|
||||||
|
});
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
use Illuminate\Support\Facades\Http;
|
||||||
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
|
Route::get('/js/script.js', function () {
|
||||||
|
$plausibleDomain = config('plausible-proxy.domain');
|
||||||
|
|
||||||
|
if (Cache::has('plausible_script_js')) {
|
||||||
|
return response(Cache::get('plausible_script_js'))
|
||||||
|
->header('Content-Type', 'application/javascript')
|
||||||
|
->header('Cache-Control', 'public, max-age=86400');
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = Http::timeout(5)->get("{$plausibleDomain}/js/script.js");
|
||||||
|
|
||||||
|
if ($response->successful()) {
|
||||||
|
$script = $response->body();
|
||||||
|
Cache::put('plausible_script_js', $script, now()->addHours(6));
|
||||||
|
|
||||||
|
return response($script)
|
||||||
|
->header('Content-Type', 'application/javascript')
|
||||||
|
->header('Cache-Control', 'public, max-age=86400');
|
||||||
|
}
|
||||||
|
|
||||||
|
return response('Error fetching Plausible script', 500);
|
||||||
|
});
|
||||||
|
|
||||||
|
Route::get('/api/routes', function () {
|
||||||
|
return response()->json(
|
||||||
|
collect(Route::getRoutes())->map(fn ($route) => $route->uri())
|
||||||
|
);
|
||||||
|
});
|
||||||
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace MattitjaAB\LaravelPlausibleProxy;
|
namespace MattitjaAB\LaravelPlausibleProxy;
|
||||||
|
|
||||||
use MattitjaAB\LaravelPlausibleProxy\Commands\LaravelPlausibleProxyCommand;
|
|
||||||
use Spatie\LaravelPackageTools\Package;
|
use Spatie\LaravelPackageTools\Package;
|
||||||
use Spatie\LaravelPackageTools\PackageServiceProvider;
|
use Spatie\LaravelPackageTools\PackageServiceProvider;
|
||||||
|
|
||||||
@@ -10,16 +9,9 @@ class LaravelPlausibleProxyServiceProvider extends PackageServiceProvider
|
|||||||
{
|
{
|
||||||
public function configurePackage(Package $package): void
|
public function configurePackage(Package $package): void
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
* This class is a Package Service Provider
|
|
||||||
*
|
|
||||||
* More info: https://github.com/spatie/laravel-package-tools
|
|
||||||
*/
|
|
||||||
$package
|
$package
|
||||||
->name('laravel-plausible-proxy')
|
->name('laravel-plausible-proxy')
|
||||||
->hasConfigFile()
|
->hasConfigFile()
|
||||||
->hasViews()
|
->hasRoutes(['web', 'api']);
|
||||||
->hasMigration('create_laravel_plausible_proxy_table')
|
|
||||||
->hasCommand(LaravelPlausibleProxyCommand::class);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
it('can test', function () {
|
|
||||||
expect(true)->toBeTrue();
|
|
||||||
});
|
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
use Illuminate\Support\Facades\Http;
|
||||||
|
|
||||||
|
it('returns plausible script from cache', function () {
|
||||||
|
Cache::put('plausible_script_js', 'console.log("Cached script");', now()->addHours(6));
|
||||||
|
|
||||||
|
$response = $this->get('/js/script.js');
|
||||||
|
|
||||||
|
$response->assertStatus(200)
|
||||||
|
->assertHeader('Content-Type', 'application/javascript')
|
||||||
|
->assertSee('Cached script');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fetches plausible script if not cached', function () {
|
||||||
|
Http::fake([
|
||||||
|
'https://plausible.io/js/script.js' => Http::response('console.log("Plausible script");', 200),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response = $this->get('/js/script.js');
|
||||||
|
|
||||||
|
$response->assertStatus(200)
|
||||||
|
->assertHeader('Content-Type', 'application/javascript')
|
||||||
|
->assertSee('Plausible script');
|
||||||
|
|
||||||
|
expect(Cache::get('plausible_script_js'))->toBe('console.log("Plausible script");');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles failed script fetch', function () {
|
||||||
|
Http::fake([
|
||||||
|
'https://plausible.io/js/script.js' => Http::response('', 500),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response = $this->get('/js/script.js');
|
||||||
|
|
||||||
|
$response->assertStatus(500)
|
||||||
|
->assertSee('Error fetching Plausible script');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects invalid event data', function () {
|
||||||
|
$response = $this->postJson('/api/event', [
|
||||||
|
'n' => 'pageview',
|
||||||
|
'u' => 'invalid-url',
|
||||||
|
'd' => 'example.com',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response->assertStatus(422)
|
||||||
|
->assertJsonStructure(['status', 'errors']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('accepts valid event data and defers request', function () {
|
||||||
|
Http::fake();
|
||||||
|
|
||||||
|
$concurrencyMock = Mockery::mock('alias:Illuminate\Support\Facades\Concurrency');
|
||||||
|
$concurrencyMock->shouldReceive('defer')->once()->andReturnUsing(function ($callback) {
|
||||||
|
$callback();
|
||||||
|
});
|
||||||
|
|
||||||
|
$response = $this->postJson('/api/event', [
|
||||||
|
'n' => 'pageview',
|
||||||
|
'u' => 'https://example.com/page',
|
||||||
|
'd' => 'example.com',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response->assertStatus(200)
|
||||||
|
->assertJson(['status' => 'queued']);
|
||||||
|
|
||||||
|
Http::assertSent(fn ($request) => $request->url() === config('plausible-proxy.domain').'/api/event' &&
|
||||||
|
$request['n'] === 'pageview'
|
||||||
|
);
|
||||||
|
|
||||||
|
Mockery::close();
|
||||||
|
});
|
||||||
Reference in new issue
Block a user