Skip to content
Snippets Groups Projects
Commit 938f9e74 authored by michael.minelli's avatar michael.minelli
Browse files

ClientSharedConfig => Add function to initiate config downloaded from server

parent 4de1ab94
No related branches found
No related tags found
No related merge requests found
import axios from 'axios';
import DojoBackendResponse from '../../shared/types/Dojo/DojoBackendResponse';
import ApiRoute from '../types/Dojo/ApiRoute';
interface ClientsConfig {
gitlabUrl: string,
gitlabAccountId: number,
gitlabAccountUsername: string,
loginGitlabClientId: string
}
class ClientsSharedConfig { class ClientsSharedConfig {
public apiURL: string; private static config: ClientsSharedConfig | undefined = undefined;
public assignment: { public apiURL!: string;
filename: string, neededFiles: Array<string>
public gitlab!: {
URL: string, dojoAccount: { id: number; username: string; };
}; };
public gitlab: { public login!: {
dojoAccount: { id: number; username: string; }; gitlab: {
client: {
id: string
}, url: {
redirect: string, token: string
}
}
}; };
public readonly dockerCompose: {
public assignment!: {
filename: string, neededFiles: Array<string>
};
public dockerCompose!: {
projectName: string projectName: string
}; };
public readonly exerciseResultsFolderMaxSizeInBytes: number; public exerciseResultsFolderMaxSizeInBytes!: number;
public readonly filenames: { public filenames!: {
results: string; results: string;
}; };
constructor() { public constructor() {
this.apiURL = process.env.API_URL ?? ''; this.login = {
gitlab: {
client: {
id: ''
},
url : {
redirect: '',
token : ''
}
}
};
}
this.assignment = { public envVarGetter(): (envName: string, defaultValue: string) => string {
filename : process.env.ASSIGNMENT_FILENAME ?? '', return (envName: string, defaultValue: string) => {
neededFiles: JSON.parse(process.env.EXERCISE_NEEDED_FILES ?? '[]') let value = process.env[envName] ?? defaultValue;
if ( value.includes('{{GITLAB_URL}}') ) {
value = value.replace('{{GITLAB_URL}}', this.gitlab.URL);
}
if ( value.includes('{{GITLAB_ACCOUNT_ID}}') ) {
value = value.replace('{{GITLAB_ACCOUNT_ID}}', String(this.gitlab.dojoAccount.id));
}
if ( value.includes('{{GITLAB_ACCOUNT_USERNAME}}') ) {
value = value.replace('{{GITLAB_ACCOUNT_USERNAME}}', this.gitlab.dojoAccount.username);
}
if ( value.includes('{{LOGIN_GITLAB_CLIENT_ID}}') ) {
value = value.replace('{{LOGIN_GITLAB_CLIENT_ID}}', this.login.gitlab.client.id);
}
return value;
}; };
}
private async fetchConfigFromApi() {
const downloadedConfig: ClientsConfig = (await axios.get<DojoBackendResponse<ClientsConfig>>(`${ this.apiURL }${ ApiRoute.CLIENTS_CONFIG }`)).data.data;
this.gitlab = { this.gitlab = {
URL : downloadedConfig.gitlabUrl,
dojoAccount: { dojoAccount: {
id : Number(process.env.GITLAB_DOJO_ACCOUNT_ID ?? -1), id : downloadedConfig.gitlabAccountId,
username: process.env.GITLAB_DOJO_ACCOUNT_USERNAME ?? '' username: downloadedConfig.gitlabAccountUsername
} }
}; };
this.login.gitlab.client.id = downloadedConfig.loginGitlabClientId;
}
async init(apiUrl: string) {
this.apiURL = apiUrl;
await this.fetchConfigFromApi();
const getEnvVar = this.envVarGetter();
this.login.gitlab.url = {
redirect: getEnvVar('LOGIN_GITLAB_URL_REDIRECT', ''),
token : getEnvVar('LOGIN_GITLAB_URL_TOKEN', '')
};
this.assignment = {
filename : getEnvVar('ASSIGNMENT_FILENAME', ''),
neededFiles: JSON.parse(getEnvVar('EXERCISE_NEEDED_FILES', '[]'))
};
this.dockerCompose = { this.dockerCompose = {
projectName: process.env.DOCKER_COMPOSE_PROJECT_NAME ?? '' projectName: getEnvVar('DOCKER_COMPOSE_PROJECT_NAME', '')
}; };
this.exerciseResultsFolderMaxSizeInBytes = Number(process.env.EXERCISE_RESULTS_FOLDER_MAX_SIZE_IN_BYTES ?? 0); this.exerciseResultsFolderMaxSizeInBytes = Number(getEnvVar('EXERCISE_RESULTS_FOLDER_MAX_SIZE_IN_BYTES', '0'));
this.filenames = { this.filenames = {
results: process.env.EXERCISE_RESULTS_FILENAME ?? '' results: getEnvVar('EXERCISE_RESULTS_FILENAME', '')
}; };
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment