feat: inital setup info route (#3847)
This commit is contained in:
@@ -118,7 +118,7 @@ services:
|
|||||||
restart: always
|
restart: always
|
||||||
environment:
|
environment:
|
||||||
# Edit the below line to match your PostgresDB URL if you have an outside DB (make sure to update the .env file as well)
|
# Edit the below line to match your PostgresDB URL if you have an outside DB (make sure to update the .env file as well)
|
||||||
- DATABASE_URL=postgresql://postgres:testpass@hoppscotch-db:5432/hoppscotch?connect_timeout=300
|
# - DATABASE_URL=postgresql://postgres:testpass@hoppscotch-db:5432/hoppscotch?connect_timeout=300
|
||||||
- PORT=3000
|
- PORT=3000
|
||||||
volumes:
|
volumes:
|
||||||
# Uncomment the line below when modifying code. Only applicable when using the "dev" target.
|
# Uncomment the line below when modifying code. Only applicable when using the "dev" target.
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import { Injectable, ExecutionContext, CanActivate } from '@nestjs/common';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class RESTAdminGuard implements CanActivate {
|
||||||
|
canActivate(context: ExecutionContext): boolean {
|
||||||
|
const request = context.switchToHttp().getRequest();
|
||||||
|
const user = request.user;
|
||||||
|
|
||||||
|
return user.isAdmin;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
import { Controller, Get, HttpStatus, Put, UseGuards } from '@nestjs/common';
|
||||||
|
import { ThrottlerBehindProxyGuard } from 'src/guards/throttler-behind-proxy.guard';
|
||||||
|
import { InfraConfigService } from './infra-config.service';
|
||||||
|
import * as E from 'fp-ts/Either';
|
||||||
|
import { JwtAuthGuard } from 'src/auth/guards/jwt-auth.guard';
|
||||||
|
import { RESTAdminGuard } from 'src/admin/guards/rest-admin.guard';
|
||||||
|
import { throwHTTPErr } from 'src/auth/helper';
|
||||||
|
import { AuthError } from 'src/types/AuthError';
|
||||||
|
import { InfraConfigEnumForClient } from 'src/types/InfraConfig';
|
||||||
|
|
||||||
|
@UseGuards(ThrottlerBehindProxyGuard)
|
||||||
|
@Controller({ path: 'site', version: '1' })
|
||||||
|
export class SiteController {
|
||||||
|
constructor(private infraConfigService: InfraConfigService) {}
|
||||||
|
|
||||||
|
@Get('setup')
|
||||||
|
@UseGuards(JwtAuthGuard, RESTAdminGuard)
|
||||||
|
async fetchSetupInfo() {
|
||||||
|
const status = await this.infraConfigService.get(
|
||||||
|
InfraConfigEnumForClient.IS_FIRST_TIME_INFRA_SETUP,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (E.isLeft(status))
|
||||||
|
throwHTTPErr(<AuthError>{
|
||||||
|
message: status.left,
|
||||||
|
statusCode: HttpStatus.NOT_FOUND,
|
||||||
|
});
|
||||||
|
return status.right;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Put('setup')
|
||||||
|
@UseGuards(JwtAuthGuard, RESTAdminGuard)
|
||||||
|
async setSetupAsComplete() {
|
||||||
|
const res = await this.infraConfigService.update(
|
||||||
|
InfraConfigEnumForClient.IS_FIRST_TIME_INFRA_SETUP,
|
||||||
|
false.toString(),
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (E.isLeft(res))
|
||||||
|
throwHTTPErr(<AuthError>{
|
||||||
|
message: res.left,
|
||||||
|
statusCode: HttpStatus.FORBIDDEN,
|
||||||
|
});
|
||||||
|
return res.right;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { InfraConfigService } from './infra-config.service';
|
import { InfraConfigService } from './infra-config.service';
|
||||||
import { PrismaModule } from 'src/prisma/prisma.module';
|
import { PrismaModule } from 'src/prisma/prisma.module';
|
||||||
|
import { SiteController } from './infra-config.controller';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [PrismaModule],
|
imports: [PrismaModule],
|
||||||
providers: [InfraConfigService],
|
providers: [InfraConfigService],
|
||||||
exports: [InfraConfigService],
|
exports: [InfraConfigService],
|
||||||
|
controllers: [SiteController],
|
||||||
})
|
})
|
||||||
export class InfraConfigModule {}
|
export class InfraConfigModule {}
|
||||||
|
|||||||
@@ -34,7 +34,9 @@ export class InfraConfigService implements OnModuleInit {
|
|||||||
await this.initializeInfraConfigTable();
|
await this.initializeInfraConfigTable();
|
||||||
}
|
}
|
||||||
|
|
||||||
getDefaultInfraConfigs(): { name: InfraConfigEnum; value: string }[] {
|
async getDefaultInfraConfigs(): Promise<
|
||||||
|
{ name: InfraConfigEnum; value: string }[]
|
||||||
|
> {
|
||||||
// Prepare rows for 'infra_config' table with default values (from .env) for each 'name'
|
// Prepare rows for 'infra_config' table with default values (from .env) for each 'name'
|
||||||
const infraConfigDefaultObjs: { name: InfraConfigEnum; value: string }[] = [
|
const infraConfigDefaultObjs: { name: InfraConfigEnum; value: string }[] = [
|
||||||
{
|
{
|
||||||
@@ -73,6 +75,10 @@ export class InfraConfigService implements OnModuleInit {
|
|||||||
name: InfraConfigEnum.VITE_ALLOWED_AUTH_PROVIDERS,
|
name: InfraConfigEnum.VITE_ALLOWED_AUTH_PROVIDERS,
|
||||||
value: getConfiguredSSOProviders(),
|
value: getConfiguredSSOProviders(),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: InfraConfigEnum.IS_FIRST_TIME_INFRA_SETUP,
|
||||||
|
value: (await this.prisma.infraConfig.count()) === 0 ? 'true' : 'false',
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
return infraConfigDefaultObjs;
|
return infraConfigDefaultObjs;
|
||||||
@@ -88,7 +94,7 @@ export class InfraConfigService implements OnModuleInit {
|
|||||||
const enumValues = Object.values(InfraConfigEnum);
|
const enumValues = Object.values(InfraConfigEnum);
|
||||||
|
|
||||||
// Fetch the default values (value in .env) for configs to be saved in 'infra_config' table
|
// Fetch the default values (value in .env) for configs to be saved in 'infra_config' table
|
||||||
const infraConfigDefaultObjs = this.getDefaultInfraConfigs();
|
const infraConfigDefaultObjs = await this.getDefaultInfraConfigs();
|
||||||
|
|
||||||
// Check if all the 'names' are listed in the default values
|
// Check if all the 'names' are listed in the default values
|
||||||
if (enumValues.length !== infraConfigDefaultObjs.length) {
|
if (enumValues.length !== infraConfigDefaultObjs.length) {
|
||||||
@@ -147,11 +153,13 @@ export class InfraConfigService implements OnModuleInit {
|
|||||||
* Update InfraConfig by name
|
* Update InfraConfig by name
|
||||||
* @param name Name of the InfraConfig
|
* @param name Name of the InfraConfig
|
||||||
* @param value Value of the InfraConfig
|
* @param value Value of the InfraConfig
|
||||||
|
* @param restartEnabled If true, restart the app after updating the InfraConfig
|
||||||
* @returns InfraConfig model
|
* @returns InfraConfig model
|
||||||
*/
|
*/
|
||||||
async update(
|
async update(
|
||||||
name: InfraConfigEnumForClient | InfraConfigEnum,
|
name: InfraConfigEnumForClient | InfraConfigEnum,
|
||||||
value: string,
|
value: string,
|
||||||
|
restartEnabled = false,
|
||||||
) {
|
) {
|
||||||
const isValidate = this.validateEnvValues([{ name, value }]);
|
const isValidate = this.validateEnvValues([{ name, value }]);
|
||||||
if (E.isLeft(isValidate)) return E.left(isValidate.left);
|
if (E.isLeft(isValidate)) return E.left(isValidate.left);
|
||||||
@@ -162,7 +170,7 @@ export class InfraConfigService implements OnModuleInit {
|
|||||||
data: { value },
|
data: { value },
|
||||||
});
|
});
|
||||||
|
|
||||||
stopApp();
|
if (restartEnabled) stopApp();
|
||||||
|
|
||||||
return E.right(this.cast(infraConfig));
|
return E.right(this.cast(infraConfig));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -261,6 +269,7 @@ export class InfraConfigService implements OnModuleInit {
|
|||||||
const isUpdated = await this.update(
|
const isUpdated = await this.update(
|
||||||
InfraConfigEnum.VITE_ALLOWED_AUTH_PROVIDERS,
|
InfraConfigEnum.VITE_ALLOWED_AUTH_PROVIDERS,
|
||||||
updatedAuthProviders.join(','),
|
updatedAuthProviders.join(','),
|
||||||
|
true,
|
||||||
);
|
);
|
||||||
if (E.isLeft(isUpdated)) return E.left(isUpdated.left);
|
if (E.isLeft(isUpdated)) return E.left(isUpdated.left);
|
||||||
|
|
||||||
@@ -316,13 +325,24 @@ export class InfraConfigService implements OnModuleInit {
|
|||||||
*/
|
*/
|
||||||
async reset() {
|
async reset() {
|
||||||
try {
|
try {
|
||||||
const infraConfigDefaultObjs = this.getDefaultInfraConfigs();
|
const infraConfigDefaultObjs = await this.getDefaultInfraConfigs();
|
||||||
|
|
||||||
await this.prisma.infraConfig.deleteMany({
|
await this.prisma.infraConfig.deleteMany({
|
||||||
where: { name: { in: infraConfigDefaultObjs.map((p) => p.name) } },
|
where: { name: { in: infraConfigDefaultObjs.map((p) => p.name) } },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Hardcode t
|
||||||
|
const updatedInfraConfigDefaultObjs = infraConfigDefaultObjs.filter(
|
||||||
|
(obj) => obj.name !== InfraConfigEnum.IS_FIRST_TIME_INFRA_SETUP,
|
||||||
|
);
|
||||||
await this.prisma.infraConfig.createMany({
|
await this.prisma.infraConfig.createMany({
|
||||||
data: infraConfigDefaultObjs,
|
data: [
|
||||||
|
...updatedInfraConfigDefaultObjs,
|
||||||
|
{
|
||||||
|
name: InfraConfigEnum.IS_FIRST_TIME_INFRA_SETUP,
|
||||||
|
value: 'true',
|
||||||
|
},
|
||||||
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
stopApp();
|
stopApp();
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ export enum InfraConfigEnum {
|
|||||||
MICROSOFT_CLIENT_SECRET = 'MICROSOFT_CLIENT_SECRET',
|
MICROSOFT_CLIENT_SECRET = 'MICROSOFT_CLIENT_SECRET',
|
||||||
|
|
||||||
VITE_ALLOWED_AUTH_PROVIDERS = 'VITE_ALLOWED_AUTH_PROVIDERS',
|
VITE_ALLOWED_AUTH_PROVIDERS = 'VITE_ALLOWED_AUTH_PROVIDERS',
|
||||||
|
|
||||||
|
IS_FIRST_TIME_INFRA_SETUP = 'IS_FIRST_TIME_INFRA_SETUP',
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum InfraConfigEnumForClient {
|
export enum InfraConfigEnumForClient {
|
||||||
@@ -26,4 +28,6 @@ export enum InfraConfigEnumForClient {
|
|||||||
|
|
||||||
MICROSOFT_CLIENT_ID = 'MICROSOFT_CLIENT_ID',
|
MICROSOFT_CLIENT_ID = 'MICROSOFT_CLIENT_ID',
|
||||||
MICROSOFT_CLIENT_SECRET = 'MICROSOFT_CLIENT_SECRET',
|
MICROSOFT_CLIENT_SECRET = 'MICROSOFT_CLIENT_SECRET',
|
||||||
|
|
||||||
|
IS_FIRST_TIME_INFRA_SETUP = 'IS_FIRST_TIME_INFRA_SETUP',
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user