Skip to content

Commit ed50dc2

Browse files
committed
refactored controller and jobs
1 parent 0dc94ad commit ed50dc2

File tree

8 files changed

+87
-33
lines changed

8 files changed

+87
-33
lines changed

render.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ services:
1111
envVars:
1212
- key: DATABASE_URL
1313
fromDatabase:
14-
name: tarostack
14+
name: tofustack
1515
property: connectionString
1616
- key: REDIS_URL
1717
fromService:
@@ -29,5 +29,5 @@ services:
2929

3030
databases:
3131
- name: db
32-
databaseName: tarostack
32+
databaseName: tofustack
3333
ipAllowList: []
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Hono } from "hono";
2+
import type { HonoTypes } from "../types/hono.type";
3+
import type { BlankSchema, Env, Schema } from "hono/types";
4+
5+
export abstract class Controler {
6+
protected readonly controller: Hono<HonoTypes, BlankSchema, '/'>;
7+
constructor() {
8+
this.controller = new Hono();
9+
}
10+
abstract routes(): Hono<HonoTypes, BlankSchema, '/'>;
11+
}

src/lib/server/api/common/inferfaces/controller.interface.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Hono } from 'hono';
22
import type { BlankSchema } from 'hono/types';
33
import type { HonoTypes } from '../types/hono.type';
44

5-
export interface Controller {
6-
// controller: Hono<HonoTypes, BlankSchema, '/'>;
7-
routes(): Hono<HonoTypes, BlankSchema, '/'>;
8-
}
5+
// export interface Controller {
6+
// routes()
7+
// }

src/lib/server/api/controllers/iam.controller.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Hono } from 'hono';
1+
import { Hono, type Schema } from 'hono';
22
import { setCookie } from 'hono/cookie';
33
import { inject, injectable } from 'tsyringe';
44
import { zValidator } from '@hono/zod-validator';
@@ -8,19 +8,18 @@ import { signInEmailDto } from '../../../dtos/signin-email.dto';
88
import { updateEmailDto } from '../../../dtos/update-email.dto';
99
import { verifyEmailDto } from '../../../dtos/verify-email.dto';
1010
import { registerEmailDto } from '../../../dtos/register-email.dto';
11-
import type { HonoTypes } from '../common/types/hono.type';
12-
import type { Controller } from '../common/inferfaces/controller.interface';
1311
import { limiter } from '../middlewares/rate-limiter.middlware';
1412
import { requireAuth } from '../middlewares/auth.middleware';
13+
import { Controler } from '../common/classes/controller.class';
1514

1615
@injectable()
17-
export class IamController implements Controller {
18-
private controller = new Hono<HonoTypes>();
19-
16+
export class IamController extends Controler {
2017
constructor(
2118
@inject(IamService) private iamService: IamService,
2219
@inject(LuciaProvider) private lucia: LuciaProvider,
23-
) { }
20+
) {
21+
super();
22+
}
2423

2524
routes() {
2625
return this.controller

src/lib/server/api/index.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,37 @@ import { container } from 'tsyringe';
55
import { IamController } from './controllers/iam.controller';
66
import { env } from './configs/envs.config';
77
import { validateAuthSession, verifyOrigin } from './middlewares/auth.middleware';
8-
// import { TestJob } from './jobs/test.job';
9-
import { glob, globSync } from 'glob';
10-
import path from 'path';
8+
import { AuthCleanupJobs } from './jobs/auth-cleanup.job';
119

12-
console.log('API SERVER STARTED');
13-
/* ----------------------------------- Api ---------------------------------- */
14-
const app = new Hono().basePath('/api');
10+
/* -------------------------------------------------------------------------- */
11+
/* App */
12+
/* -------------------------------------------------------------------------- */
13+
export const app = new Hono().basePath('/api');
1514

16-
/* --------------------------- Global Middlewares --------------------------- */
15+
/* -------------------------------------------------------------------------- */
16+
/* Global Middlewares */
17+
/* -------------------------------------------------------------------------- */
1718
app.use(verifyOrigin).use(validateAuthSession);
1819

19-
/* --------------------------------- Routes --------------------------------- */
20+
/* -------------------------------------------------------------------------- */
21+
/* Routes */
22+
/* -------------------------------------------------------------------------- */
2023
const routes = app
2124
.route('/iam', container.resolve(IamController).routes())
2225

26+
/* -------------------------------------------------------------------------- */
27+
/* Cron Jobs */
28+
/* -------------------------------------------------------------------------- */
29+
container.resolve(AuthCleanupJobs).deleteStaleEmailVerificationRequests();
30+
container.resolve(AuthCleanupJobs).deleteStaleLoginRequests();
31+
2332
/* -------------------------------------------------------------------------- */
2433
/* Exports */
2534
/* -------------------------------------------------------------------------- */
26-
export const rpc = hc<typeof routes>(env.ORIGIN);
35+
const rpc = hc<typeof routes>(env.ORIGIN);
2736
export type ApiClient = typeof rpc;
2837
export type ApiRoutes = typeof routes;
29-
export { app };
3038

31-
async function resolveJobs() {
32-
const jobFiles = globSync('**/*.job.*');
3339

34-
for (const file of jobFiles) {
35-
const module = await import(path.resolve(file));
36-
container.resolve(module.default)
37-
}
38-
}
3940

40-
resolveJobs();
41+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { inject, injectable } from "tsyringe";
2+
import { JobsService } from "../services/jobs.service";
3+
4+
@injectable()
5+
export class AuthCleanupJobs {
6+
private queue;
7+
8+
constructor(
9+
@inject(JobsService) private jobsService: JobsService,
10+
) {
11+
/* ------------------------------ Create Queue ------------------------------ */
12+
this.queue = this.jobsService.createQueue('test')
13+
14+
/* ---------------------------- Register Workers ---------------------------- */
15+
this.worker();
16+
}
17+
18+
async deleteStaleEmailVerificationRequests() {
19+
this.queue.add('delete_stale_email_verifiactions', null, {
20+
repeat: {
21+
pattern: '0 0 * * 0' // Runs once a week at midnight on Sunday
22+
}
23+
})
24+
}
25+
26+
async deleteStaleLoginRequests() {
27+
this.queue.add('delete_stale_login_requests', null, {
28+
repeat: {
29+
pattern: '0 0 * * 0' // Runs once a week at midnight on Sunday
30+
}
31+
})
32+
}
33+
34+
private async worker() {
35+
return this.jobsService.createWorker(this.queue.name, async (job) => {
36+
if (job.name === "delete_stale_email_verifiactions") {
37+
// delete stale email verifications
38+
}
39+
if (job.name === "delete_stale_login_requests") {
40+
// delete stale email verifications
41+
}
42+
})
43+
}
44+
}

src/lib/server/api/packages/lucia.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { container } from 'tsyringe';
21
import { Lucia } from 'lucia';
32
import { DrizzlePostgreSQLAdapter } from '@lucia-auth/adapter-drizzle';
43
import { sessionsTable, usersTable } from '../databases/tables';

src/lib/server/api/services/jobs.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { RedisProvider } from "../providers/redis.provider";
44

55
@injectable()
66
export class JobsService {
7-
constructor(@inject(RedisProvider) private readonly redis: RedisProvider) { }
7+
constructor(@inject(RedisProvider) private readonly redis: RedisProvider) {
8+
}
89

910
createQueue(name: string) {
1011
return new Queue(name, { connection: this.redis })

0 commit comments

Comments
 (0)