Skip to content

Commit a9ecd90

Browse files
authored
Merge pull request #4 from gtech-mulearn/dev
Dev
2 parents d6b5469 + 575597e commit a9ecd90

File tree

4 files changed

+59
-21
lines changed

4 files changed

+59
-21
lines changed

database.sqlite

-20 KB
Binary file not shown.

src/app.module.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { AppController } from './app.controller';
44
import { AppService } from './app.service';
55
import { TypeOrmModule } from '@nestjs/typeorm';
66
import { SchedulerModule } from './scheduler/scheduler.module';
7+
import { APP_GUARD } from '@nestjs/core';
8+
import { ApiKeyGuard } from './guards/api-key.guard';
79

810
@Module({
911
imports: [
@@ -12,35 +14,35 @@ import { SchedulerModule } from './scheduler/scheduler.module';
1214
imports: [ConfigModule],
1315
inject: [ConfigService],
1416
useFactory: (config: ConfigService) => {
15-
const dbType = config.get<string>('DB_TYPE', 'sqlite');
17+
const dbType = config.get<string>('DB_TYPE');
1618
const entities = [__dirname + '/**/*.entity{.ts,.js}'];
1719

18-
if (dbType === 'mysql') {
19-
return {
20-
type: 'mysql' as const,
21-
host: config.get<string>('DATABASE_HOST', 'localhost'),
22-
port: parseInt(config.get<string>('DATABASE_PORT', '3306'), 10),
23-
username: config.get<string>('DATABASE_USER', 'root'),
24-
password: config.get<string>('DATABASE_PASSWORD', ''),
25-
database: config.get<string>('DATABASE_NAME', 'scheduler'),
26-
entities,
27-
synchronize: config.get<string>('DB_SYNC', 'false') === 'true',
28-
logging: config.get<string>('DB_LOGGING', 'false') === 'true',
29-
};
20+
if (dbType !== 'mysql') {
21+
throw new Error('Only MySQL database is supported. Please check DB_TYPE in .env');
3022
}
3123

3224
return {
33-
type: 'sqlite' as const,
34-
database: config.get<string>('DB_FILE', 'database.sqlite'),
25+
type: 'mysql' as const,
26+
host: config.get<string>('DATABASE_HOST', 'localhost'),
27+
port: parseInt(config.get<string>('DATABASE_PORT', '3306'), 10),
28+
username: config.get<string>('DATABASE_USER', 'root'),
29+
password: config.get<string>('DATABASE_PASSWORD', ''),
30+
database: config.get<string>('DATABASE_NAME', 'scheduler'),
3531
entities,
36-
synchronize: true,
37-
logging: false,
32+
synchronize: config.get<string>('DB_SYNC', 'false') === 'true',
33+
logging: config.get<string>('DB_LOGGING', 'false') === 'true',
3834
};
3935
},
4036
}),
4137
SchedulerModule,
4238
],
4339
controllers: [AppController],
44-
providers: [AppService],
40+
providers: [
41+
AppService,
42+
{
43+
provide: APP_GUARD,
44+
useClass: ApiKeyGuard,
45+
},
46+
],
4547
})
46-
export class AppModule {}
48+
export class AppModule { }

src/guards/api-key.guard.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {
2+
CanActivate,
3+
ExecutionContext,
4+
Injectable,
5+
UnauthorizedException,
6+
} from '@nestjs/common';
7+
import { ConfigService } from '@nestjs/config';
8+
import { Request } from 'express';
9+
10+
@Injectable()
11+
export class ApiKeyGuard implements CanActivate {
12+
constructor(private readonly configService: ConfigService) { }
13+
14+
canActivate(context: ExecutionContext): boolean {
15+
const request = context.switchToHttp().getRequest<Request>();
16+
const authHeader = request.headers.authorization;
17+
18+
if (!authHeader) {
19+
throw new UnauthorizedException('Missing Authorization header');
20+
}
21+
22+
const [type, token] = authHeader.split(' ');
23+
24+
if (type !== 'Bearer' || !token) {
25+
throw new UnauthorizedException('Invalid Authorization header format');
26+
}
27+
28+
const apiKey = this.configService.get<string>('API_KEY');
29+
30+
if (token !== apiKey) {
31+
throw new UnauthorizedException('Invalid API Key');
32+
}
33+
34+
return true;
35+
}
36+
}

src/scheduler/scheduler.controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { SchedulerService } from './scheduler.service';
44

55
@Controller('jobs')
66
export class SchedulerController {
7-
constructor(private readonly svc: SchedulerService) {}
7+
constructor(private readonly svc: SchedulerService) { }
88

99
@Post()
1010
async create(@Body() payload: CreateJobDto) {
@@ -17,7 +17,7 @@ export class SchedulerController {
1717
return this.svc.findAll();
1818
}
1919

20-
20+
2121
@Delete(':id')
2222
@HttpCode(204)
2323
async remove(@Param('id') id: string) {

0 commit comments

Comments
 (0)