Skip to content

Commit

Permalink
Build: 252 create new build config for nomad job (#297)
Browse files Browse the repository at this point in the history
* feat: add nomad build config

* feat: nginx forward proxy

* fix: adjust user endpoint

* fix: undo changes

* feat: load api in config.json file

* fix: delete nginx extra router

* build: set auth config dynamically

* feat: update auth config after app config is loaded

* test: fix app config service test

---------

Co-authored-by: Saúl Fernández <[email protected]>
  • Loading branch information
MartaOB and Sftobias authored Feb 3, 2025
1 parent b1b98d5 commit 2d082fe
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 31 deletions.
23 changes: 14 additions & 9 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
"input": "src/assets/images/ai4eosc",
"output": "/assets/images/"
},
{
"glob": "**/*",
"input": "src/assets/i18n/",
"output": "/assets/i18n/"
},
{
"glob": "config.json",
"input": "src/assets/config/ai4eosc/",
Expand Down Expand Up @@ -387,36 +392,36 @@
"builder": "@angular-devkit/build-angular:dev-server",
"configurations": {
"local-ai4eosc": {
"buildTarget": "ai4-dashboard:build:local-ai4eosc"
"buildTarget": "ai4-dashboard:build:local-ai4eosc"
},
"production-ai4eosc": {
"buildTarget": "ai4-dashboard:build:production-ai4eosc"
"buildTarget": "ai4-dashboard:build:production-ai4eosc"
},
"development-ai4eosc": {
"buildTarget": "ai4-dashboard:build:development-ai4eosc"
"buildTarget": "ai4-dashboard:build:development-ai4eosc"
},
"local-imagine": {
"buildTarget": "ai4-dashboard:build:local-imagine"
"buildTarget": "ai4-dashboard:build:local-imagine"
},
"production-imagine": {
"buildTarget": "ai4-dashboard:build:production-imagine"
"buildTarget": "ai4-dashboard:build:production-imagine"
},
"development-imagine": {
"buildTarget": "ai4-dashboard:build:development-imagine"
"buildTarget": "ai4-dashboard:build:development-imagine"
},
"tutorials-ai4eosc": {
"buildTarget": "ai4-dashboard:build:tutorials-ai4eosc"
"buildTarget": "ai4-dashboard:build:tutorials-ai4eosc"
},
"ai4life-ai4eosc": {
"buildTarget": "ai4-dashboard:build:ai4life-ai4eosc"
"buildTarget": "ai4-dashboard:build:ai4life-ai4eosc"
}
},
"defaultConfiguration": "local-ai4eosc"
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"buildTarget": "ai4-dashboard:build"
"buildTarget": "ai4-dashboard:build"
}
},
"lint": {
Expand Down
8 changes: 5 additions & 3 deletions docker/ai4eosc/prod/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#stage 1
FROM node:20.5.1 as node
FROM node:20.5.1 AS node
WORKDIR /app
COPY . .
RUN npm ci
RUN npm run generate-version-file
RUN npm run build-ai4eosc-prod
#stage 2
FROM nginx:alpine
FROM nginx:alpine
RUN apk add --no-cache jq bash
COPY ./docker/nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=node /app/dist/ai4-dashboard /usr/share/nginx/html
COPY --from=node /app/dist/ai4-dashboard /usr/share/nginx/html
CMD ["sh", "-c", "CLEAN_API_SERVER=$(echo $API_SERVER | sed 's:/*$::') && jq --arg cleanApi \"$CLEAN_API_SERVER\" '.apiURL = $cleanApi' /usr/share/nginx/html/assets/config.json > /usr/share/nginx/html/assets/config.tmp && mv /usr/share/nginx/html/assets/config.tmp /usr/share/nginx/html/assets/config.json && nginx -g 'daemon off;'"]
51 changes: 35 additions & 16 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { NgxEchartsModule } from 'ngx-echarts';
import { NotificationsButtonComponent } from './layout/top-navbar/notifications-button/notifications-button.component';
import { CookieService } from 'ngx-cookie-service';
import { gitInfo } from '@environments/version';
import { OAuthModuleConfig } from 'angular-oauth2-oidc';

export function storageFactory(): OAuthStorage {
return localStorage;
Expand All @@ -45,6 +46,16 @@ export function createTranslateLoader(http: HttpClient): TranslateHttpLoader {
'.json?v=' + gitInfo.version
);
}

export function authConfigFactory(): OAuthModuleConfig {
return {
resourceServer: {
allowedUrls: [base],
sendAccessToken: true,
},
};
}

const { base } = environment.api;

const cookieConfig: NgcCookieConsentConfig = {
Expand Down Expand Up @@ -109,12 +120,7 @@ renderer.link = (href, title, text) => {
BrowserModule,
AppRoutingModule,
ReactiveFormsModule,
OAuthModule.forRoot({
resourceServer: {
allowedUrls: [base],
sendAccessToken: true,
},
}),
OAuthModule.forRoot(),
TranslateModule.forRoot({
defaultLanguage: 'en',
useDefaultLang: true,
Expand Down Expand Up @@ -152,24 +158,37 @@ renderer.link = (href, title, text) => {
{
provide: APP_INITIALIZER,
multi: true,
deps: [AppConfigService, NgcCookieConsentConfig],
deps: [AppConfigService, NgcCookieConsentConfig, OAuthModuleConfig],
useFactory: (
appConfigService: AppConfigService,
config: NgcCookieConsentConfig
config: NgcCookieConsentConfig,
authConfig: OAuthModuleConfig
) => {
return () => {
return appConfigService.loadAppConfig().then(() => {
if (config.cookie) {
config.cookie.domain =
appConfigService.analytics.domain;
config.content!.href =
appConfigService.legalLinks[1].url;
}
});
return appConfigService
.loadAppConfig(authConfig)
.then(() => {
if (config.cookie) {
config.cookie.domain =
appConfigService.analytics.domain;
config.content!.href =
appConfigService.legalLinks[1].url;
}
if (
appConfigService.apiURL &&
appConfigService.apiURL !== ''
) {
environment.api.base = appConfigService.apiURL;
}
});
};
},
},
{ provide: OAuthStorage, useFactory: storageFactory },
{
provide: OAuthModuleConfig,
useFactory: authConfigFactory,
},
Title,
CookieService,
provideHttpClient(withInterceptorsFromDi()),
Expand Down
9 changes: 8 additions & 1 deletion src/app/core/services/app-config/app-config.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ const mockedConfig = {
],
};

const mockedOauthConfig = {
resourceServer: {
allowedUrls: ['https://api.test.ai4eosc.eu/v1'],
sendAccessToken: true,
},
};

describe('AppConfigService', () => {
let service: AppConfigService;

Expand All @@ -44,7 +51,7 @@ describe('AppConfigService', () => {
const spy = jest
.spyOn(service, 'loadAppConfig')
.mockReturnValue(Promise.resolve());
service.loadAppConfig();
service.loadAppConfig(mockedOauthConfig);
expect(spy).toHaveBeenCalledTimes(1);
expect(service['appConfig']).toEqual(mockedConfig);
});
Expand Down
13 changes: 12 additions & 1 deletion src/app/core/services/app-config/app-config.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { OAuthModuleConfig } from 'angular-oauth2-oidc';
import { firstValueFrom } from 'rxjs';

@Injectable({
Expand All @@ -10,10 +11,15 @@ export class AppConfigService {

constructor(private http: HttpClient) {}

loadAppConfig() {
loadAppConfig(oauthConfig: OAuthModuleConfig) {
return firstValueFrom(this.http.get('/assets/config.json')).then(
(config) => {
this.appConfig = config;
if (this.appConfig.apiURL && this.appConfig.apiURL !== '') {
oauthConfig.resourceServer.allowedUrls = [
this.appConfig.apiURL,
];
}
}
);
}
Expand Down Expand Up @@ -69,4 +75,9 @@ export class AppConfigService {
this.checkConfigFileLoaded();
return this.appConfig.analytics;
}

get apiURL() {
this.checkConfigFileLoaded();
return this.appConfig.apiURL;
}
}
3 changes: 2 additions & 1 deletion src/assets/config/ai4eosc/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@
"analytics": {
"domain": "dashboard.cloud.ai4eosc.eu",
"src": "https://stats.services.ai4os.eu/js/script.js"
}
},
"apiURL": ""
}

0 comments on commit 2d082fe

Please sign in to comment.