Skip to content
Snippets Groups Projects
Select Git revision
  • d9bf9cc4a9c2f72888de90f8d52c62f11b974742
  • main default protected
  • 3-add-makefile-with-structure
  • 9-add-destroy-function-4
  • 4-add-create-init-function
  • 5-add-push-function-2
  • 9-add-destroy-function-3
  • 9-add-destroy-function-2
  • v0.1
9 results

stack_test.c

Blame
  • ClientsSharedConfig.ts 3.95 KiB
    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,
        exerciseMaxPerAssignment: number
    }
    
    
    class ClientsSharedConfig {
        private static config: ClientsSharedConfig | undefined = undefined;
    
        public apiURL!: string;
    
    
        public gitlab!: {
            URL: string, dojoAccount: { id: number; username: string; };
        };
    
        public login!: {
            gitlab: {
                client: {
                    id: string
                }, url: {
                    redirect: string, token: string
                }
            }
        };
    
        public assignment!: {
            filename: string, neededFiles: Array<string>, name: string, secret: string;
        };
    
        public exercise!: {
            maxPerAssignment: number
        };
    
        public dockerCompose!: {
            projectName: string
        };
    
        public exerciseResultsFolderMaxSizeInBytes!: number;
    
        public filenames!: {
            results: string;
        };
    
    
        public constructor() {
            this.login = {
                gitlab: {
                    client: {
                        id: ''
                    },
                    url   : {
                        redirect: '',
                        token   : ''
                    }
                }
            };
        }
    
        public envVarGetter(): (envName: string, defaultValue: string) => string {
            return (envName: string, defaultValue: string) => {
                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 = {
                URL        : downloadedConfig.gitlabUrl,
                dojoAccount: {
                    id      : downloadedConfig.gitlabAccountId,
                    username: downloadedConfig.gitlabAccountUsername
                }
            };
    
            this.login.gitlab.client.id = downloadedConfig.loginGitlabClientId;
    
            this.exercise = {
                maxPerAssignment: downloadedConfig.exerciseMaxPerAssignment
            };
        }
    
        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', '[]')),
                name       : process.env.DOJO_ASSIGNMENT_NAME || '',
                secret     : process.env.DOJO_ASSIGNMENT_SECRET || ''
            };
    
            this.dockerCompose = {
                projectName: getEnvVar('DOCKER_COMPOSE_PROJECT_NAME', '')
            };
    
            this.exerciseResultsFolderMaxSizeInBytes = Number(getEnvVar('EXERCISE_RESULTS_FOLDER_MAX_SIZE_IN_BYTES', '0'));
    
            this.filenames = {
                results: getEnvVar('EXERCISE_RESULTS_FILENAME', '')
            };
        }
    }
    
    
    export default new ClientsSharedConfig();