Skip to content

Commit

Permalink
feat(redirects): make it works with custom domain
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainLanz committed Dec 14, 2024
1 parent 8511936 commit 5b66084
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion apps/romainlanz.com/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ NODE_ENV=development
LOG_LEVEL=info
APP_KEY=
APP_URL=http://romainlanz.localhost:3333
VITE_APP_URL=$APP_URL
REDIRECT_DOMAIN=redirect.localhost

# Session
SESSION_DRIVER=cookie
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import type { HttpContext } from '@adonisjs/core/http';
export default class ProcessRedirectController {
constructor(private repository: RedirectRepository) {}

async handle({ request, response }: HttpContext) {
const sanitizedUrl = request.url().slice(3);
const redirect = await this.repository.findByUrl(sanitizedUrl);
async execute({ params, response }: HttpContext) {
const redirect = await this.repository.findByUrl(params['*']);

if (!redirect) {
return response.status(404).send('Not found');
Expand Down
1 change: 1 addition & 0 deletions apps/romainlanz.com/start/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default await Env.create(new URL('../', import.meta.url), {
APP_KEY: Env.schema.string(),
HOST: Env.schema.string({ format: 'host' }),
LOG_LEVEL: Env.schema.string(),
REDIRECT_DOMAIN: Env.schema.string(),

/*
|----------------------------------------------------------
Expand Down
4 changes: 1 addition & 3 deletions apps/romainlanz.com/start/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import './routes/admin.js';
import './routes/app.js';
import './routes/fragments.js';
import './routes/paste.js';
import './routes/redirect.js';
import router from '@adonisjs/core/services/router';
import { middleware } from '#start/kernel';

// region Controller's Imports
const ProcessRedirectController = () => import('#redirects/controllers/process_redirect_controller');
const UploadImageController = () => import('#media/controllers/upload_image_controller');
// endregion

router.get('r/*', [ProcessRedirectController]).as('redirects.show');

router
.group(() => {
router.post('assets/images', [UploadImageController]).as('assets.store');
Expand Down
8 changes: 8 additions & 0 deletions apps/romainlanz.com/start/routes/redirect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import router from '@adonisjs/core/services/router';
import env from '#start/env';

// region Controller's Imports
const ProcessRedirectController = () => import('#redirects/controllers/process_redirect_controller');
// endregion

router.get('*', [ProcessRedirectController, 'execute']).domain(env.get('REDIRECT_DOMAIN'));

0 comments on commit 5b66084

Please sign in to comment.