feat(redirect): hot-path redirect controller with Redis cache, 302/404/410

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
main
boban 2026-05-16 00:52:16 +02:00
parent b59dbfd95b
commit ebdcd141bb
7 changed files with 154 additions and 2 deletions

View File

@ -2,6 +2,7 @@
namespace App\Domains\Link\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasMany;
@ -9,10 +10,15 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Link extends Model
{
use SoftDeletes;
use HasFactory, SoftDeletes;
protected $guarded = ['id'];
protected static function newFactory(): \Database\Factories\LinkFactory
{
return \Database\Factories\LinkFactory::new();
}
protected static function booted(): void
{
static::creating(function (self $model) {

View File

@ -2,6 +2,7 @@
namespace App\Domains\Workspace\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasMany;
@ -9,10 +10,15 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Workspace extends Model
{
use SoftDeletes;
use HasFactory, SoftDeletes;
protected $guarded = ['id'];
protected static function newFactory(): \Database\Factories\WorkspaceFactory
{
return \Database\Factories\WorkspaceFactory::new();
}
protected static function booted(): void
{
static::creating(function (self $model) {

View File

@ -0,0 +1,55 @@
<?php
namespace App\Http\Controllers;
use App\Domains\Link\Models\Link;
use App\Domains\Link\Services\LinkCacheService;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
class RedirectController extends Controller
{
public function __construct(private LinkCacheService $cache) {}
public function __invoke(Request $request, string $slug): RedirectResponse
{
// 1. Redis-first lookup (domain_id = null for default domain)
$data = $this->cache->get($slug);
if ($data === null) {
// Cache miss — try DB
$link = Link::where('slug', $slug)
->whereNull('domain_id')
->first();
if (! $link) {
abort(404);
}
$this->cache->warmFromLink($link);
$data = $this->cache->get($slug);
}
// 2. Status check
if (($data['status'] ?? '') !== 'active') {
abort(410);
}
// 3. Expiry check
$expiresAt = $data['expires_at'] ?? '';
if ($expiresAt !== '' && (int) $expiresAt < time()) {
abort(410);
}
// 4. RecordClickJob dispatched in Task 7
// \App\Domains\Analytics\Jobs\RecordClickJob::dispatch(
// (int) $data['link_id'],
// $request->ip(),
// $request->userAgent(),
// $request->headers->all()
// )->onQueue('clicks');
// 5. 302 redirect
return redirect($data['target'], 302);
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace Database\Factories;
use App\Domains\Link\Models\Link;
use App\Domains\Workspace\Models\Workspace;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class LinkFactory extends Factory
{
protected $model = Link::class;
public function definition(): array
{
return [
'workspace_id' => Workspace::factory(),
'slug' => Str::random(6),
'target_url' => fake()->url(),
'status' => 'active',
];
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace Database\Factories;
use App\Domains\Workspace\Models\Workspace;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class WorkspaceFactory extends Factory
{
protected $model = Workspace::class;
public function definition(): array
{
return [
'name' => fake()->company(),
'slug' => Str::random(12),
'owner_id' => User::factory(),
];
}
}

View File

@ -16,4 +16,10 @@ Route::view('profile', 'profile')
->middleware(['auth'])
->name('profile');
// Redirect service — handles slugs on main domain and custom domains
// Placed after all named routes so dashboard/profile etc. are not caught here
Route::get('/{slug}', \App\Http\Controllers\RedirectController::class)
->where('slug', '[a-zA-Z0-9_-]+')
->name('redirect');
require __DIR__.'/auth.php';

View File

@ -0,0 +1,34 @@
<?php
use App\Domains\Link\Models\Link;
it('redirects to target url for active link', function () {
$link = Link::factory()->create([
'slug' => 'abc123',
'target_url' => 'https://example.com',
'status' => 'active',
'domain_id' => null,
]);
$response = $this->get('/abc123');
$response->assertRedirect('https://example.com');
$response->assertStatus(302);
});
it('returns 404 for unknown slug', function () {
$response = $this->get('/nonexistent-xyz99');
$response->assertStatus(404);
});
it('returns 410 for disabled link', function () {
Link::factory()->create([
'slug' => 'disabled1',
'target_url' => 'https://example.com',
'status' => 'disabled',
'domain_id' => null,
]);
$response = $this->get('/disabled1');
$response->assertStatus(410);
});