From b276fa118370f77ecb484a5186e81a69d5fa2e7d Mon Sep 17 00:00:00 2001 From: phantomjinx Date: Wed, 17 Jan 2018 11:41:08 +0000 Subject: [PATCH 1/2] Modify the npm start script for running the app * package.json * Replace the development server start command with a cli start script * start.sh * Can start server through either node or ng * Use npm start for production node execution * Use npm start -- -d for starting using 'ng serve' * server.js * Scaffold the angular compiled artefacts in the dist/ directory as static files in express to allow them to be server through node. --- ngapp/README.md | 6 +++++- ngapp/package.json | 3 ++- ngapp/server.js | 15 +++++++++++++++ ngapp/start.sh | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 ngapp/server.js create mode 100755 ngapp/start.sh diff --git a/ngapp/README.md b/ngapp/README.md index e66b7bea..d68ff074 100644 --- a/ngapp/README.md +++ b/ngapp/README.md @@ -2,9 +2,13 @@ This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 1.3.2. +## Running with [Nodejs](https://nodejs.org) + +Execute `npm start` to serve the Angular app using nodejs. Navigate to `http://localhost:8080`. This command is meant for production and the files in the `dist` directory will have to be recompiled for code changes to be displayed. + ## Development server -Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files. +Execute `npm start -- -d` for a development server. Navigate to `http://localhost:8080/`. The app will automatically reload if you change any of the source files. ## Code scaffolding diff --git a/ngapp/package.json b/ngapp/package.json index e5a1630c..1a902473 100644 --- a/ngapp/package.json +++ b/ngapp/package.json @@ -4,8 +4,8 @@ "license": "MIT", "scripts": { "ng": "ng", - "start": "ng serve --host 0.0.0.0 --port 8080 --disable-host-check", "build": "ng build", + "start": "./start.sh", "test": "ng test", "lint": "ng lint", "e2e": "ng e2e" @@ -23,6 +23,7 @@ "@angular/router": "^4.4.6", "@swimlane/ngx-datatable": "^11.1.5", "core-js": "^2.4.1", + "express": "^4.16.2", "ng2-codemirror": "^1.1.3", "ngx-bootstrap": "^1.9.3", "patternfly": "^3.31.0", diff --git a/ngapp/server.js b/ngapp/server.js new file mode 100644 index 00000000..15ea47ab --- /dev/null +++ b/ngapp/server.js @@ -0,0 +1,15 @@ +var express = require("express"); +var bodyParser = require("body-parser"); + +var app = express(); +app.use(bodyParser.json()); + +// Create link to Angular build directory +var distDir = __dirname + "/dist/"; +app.use(express.static(distDir)); + +// Initialize the app. +var server = app.listen(process.env.PORT || 8080, function () { + var port = server.address().port; + console.log("App now running on port", port); +}); diff --git a/ngapp/start.sh b/ngapp/start.sh new file mode 100755 index 00000000..05fa3cc9 --- /dev/null +++ b/ngapp/start.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +################# +# +# Show help and exit +# +################# +function show_help { + echo "Usage: $0 [-d] [-h]" + echo "-d: run in development mode" + echo "" + echo "To passthrough additional arguments, enter '--' followed by the extra arguments" + exit 1 +} + +# +# Determine the command line options +# +while getopts ":d" opt; +do + case $opt in + d) DEV=1 ;; + h) show_help ;; + esac +done +shift "$(expr $OPTIND - 1)" + +if [ "${DEV}" == "1" ]; then + ng serve --host 0.0.0.0 --port 8080 --disable-host-check +else + ng build && node ./server.js +fi From 1bb974925b0e5f652c8e97778c38c975a92aec45 Mon Sep 17 00:00:00 2001 From: phantomjinx Date: Thu, 18 Jan 2018 13:21:11 +0000 Subject: [PATCH 2/2] TEIIDTOOLS-307: Connects BTL to the teiid-komodo in Openshift * environment/* * Centralises references to komodoEngine and komodoRestVersion constants * Removes references to host and port as these are now accessible using api.service * api.service.ts * Provides method for fetching the base url of the application * app-settings.service.ts * Fetches the user profile from teiid-komodo, providing BTL with the user details including their workspace. --- ngapp/src/app/core/api.service.ts | 18 +++-- ngapp/src/app/core/app-settings.service.ts | 90 +++++++++++++++++----- ngapp/src/environments/environment.prod.ts | 16 +++- ngapp/src/environments/environment.ts | 16 +++- 4 files changed, 108 insertions(+), 32 deletions(-) diff --git a/ngapp/src/app/core/api.service.ts b/ngapp/src/app/core/api.service.ts index f2fb54cc..0abff1ea 100644 --- a/ngapp/src/app/core/api.service.ts +++ b/ngapp/src/app/core/api.service.ts @@ -29,20 +29,26 @@ export abstract class ApiService { protected appSettings: AppSettingsService; protected logger: LoggerService; - constructor( appSettings: AppSettingsService, logger: LoggerService ) { + constructor( appSettings: AppSettingsService, logger: LoggerService) { this.appSettings = appSettings; this.logger = logger; } /** - * Get the Auth RequestOptions - * TODO: User and password are currently hardcoded to the DSB kit server credentials (dsbUser | 1demo-user1) + * @returns the base url of the application + */ + protected getBaseUrl(): string { + return window.location.protocol + '://' + window.location.hostname; + } + + /** + * Get the Auth RequestOptions if any + * Note: Since usage of the oauth-proxy no additional auth request options are necessary + * * @returns {RequestOptions} */ protected getAuthRequestOptions(): RequestOptions { - const userPasswordStr = this.appSettings.getKomodoUser() + ":" + this.appSettings.getKomodoUserPassword(); - const headers = new Headers({ "Authorization": "Basic " + btoa(userPasswordStr) }); - return new RequestOptions({ headers }); + return this.appSettings.getAuthRequestOptions(); } /** diff --git a/ngapp/src/app/core/app-settings.service.ts b/ngapp/src/app/core/app-settings.service.ts index d6d95b04..37244b78 100644 --- a/ngapp/src/app/core/app-settings.service.ts +++ b/ngapp/src/app/core/app-settings.service.ts @@ -16,7 +16,12 @@ */ import { Injectable } from "@angular/core"; +import { Http, Headers, RequestOptions, Response } from "@angular/http"; +import { LoggerService } from "@core/logger.service"; import { LayoutType } from "@shared/layout-type.enum"; +import { Observable } from "rxjs/Observable"; +import { ErrorObservable } from "rxjs/observable/ErrorObservable"; +import { environment } from "@environments/environment"; @Injectable() export class AppSettingsService { @@ -30,20 +35,24 @@ export class AppSettingsService { public readonly GIT_REPO_PATH_KEY = "repo-path-property"; public readonly GIT_REPO_FILE_PATH_KEY = "file-path-property"; - private readonly komodoRoot = "tko:komodo/tko:workspace"; - - // TODO: temporary location for user and password - private readonly komodoUser = "dsbUser"; - private readonly komodoUserPassword = "1demo-user1"; - // Map to maintain the target git repository properties private readonly gitRepoProperties: Map; + private static readonly userProfileUrl = environment.komodoServiceUrl + "/userProfile"; + // page layouts private svcPageLayout: LayoutType = LayoutType.CARD; private connPageLayout: LayoutType = LayoutType.CARD; - constructor() { + private http: Http; + private logger: LoggerService; + + private userProfile: Object; + + constructor(http: Http, logger: LoggerService) { + this.http = http; + this.logger = logger; + // TODO: The git repository properties will be picked up based on the Openshift install location this.gitRepoProperties = new Map(); this.gitRepoProperties.set(this.GIT_REPO_PATH_KEY, "https://github.com/GIT_USER/GIT_REPO"); @@ -52,30 +61,75 @@ export class AppSettingsService { this.gitRepoProperties.set(this.GIT_REPO_PASSWORD_KEY, "MY_PASS"); this.gitRepoProperties.set(this.GIT_REPO_AUTHOR_NAME_KEY, "MY_USER"); this.gitRepoProperties.set(this.GIT_REPO_AUTHOR_EMAIL_KEY, "USER@SOMEWHERE.COM"); + + // + // Do a call to fetch the user profile on init of service. + // The fetchProfile method returns an observable + // which we subscribe to and on completion assigns the variable + // values accordingly + // + this.fetchUserProfile().subscribe( + (profile) => { + this.userProfile = profile; + }, + (error) => { + this.logger.error( "[fetchUserProfile] Error: %o", error ); + }); } - /* - * Get the komodo workspace path for the current user - * @returns {string} the komodo workspace path + private handleError(error: Response): ErrorObservable { + this.logger.error( this.constructor.name + "::handleError" ); + return Observable.throw(error); + } + + private fetchUserProfile(): Observable { + return this.http.get(AppSettingsService.userProfileUrl, this.getAuthRequestOptions()) + .map((response) => { + const userInfo = response.json(); + return userInfo.Information; + }) + .catch((error) => this.handleError(error)); + } + + /** + * Get the Auth RequestOptions if any + * Note: Since usage of the oauth-proxy no additional auth request options are necessary + * + * @returns {RequestOptions} */ - public getKomodoUserWorkspacePath( ): string { - return this.komodoRoot + "/" + this.komodoUser; + public getAuthRequestOptions(): RequestOptions { + const headers = new Headers({}); + return new RequestOptions({ headers }); } /* * Get the logged in komodo user * @returns {string} the komodo user */ - public getKomodoUser( ): string { - return this.komodoUser; + public getKomodoUser(): string { + if (! this.userProfile) + throw new Error('Failed to retrieve the user profile so cannot provide a user name'); + + let komodoUser = this.userProfile['User Name']; + if (! komodoUser) + throw new Error('Failed to retrieve the user name from the user profile'); + + return komodoUser; } /* - * Get the logged in komodo user password - * @returns {string} the komodo user password + * Get the komodo workspace path for the current user + * @returns {string} the komodo workspace path */ - public getKomodoUserPassword( ): string { - return this.komodoUserPassword; + public getKomodoUserWorkspacePath(): string { + if (! this.userProfile) + throw new Error('Failed to retrieve the user profile so cannot provide a workspace path'); + + let workspace = this.userProfile['Workspace']; + if (! workspace) + throw new Error('Failed to retrieve the workspace path from the user profile'); + + return workspace; } /* diff --git a/ngapp/src/environments/environment.prod.ts b/ngapp/src/environments/environment.prod.ts index 0532bbc8..c59cdcdf 100644 --- a/ngapp/src/environments/environment.prod.ts +++ b/ngapp/src/environments/environment.prod.ts @@ -17,23 +17,31 @@ import { DataservicesConstants } from "@dataservices/shared/dataservices-constants"; +export const komodoEngine = "vdb-builder"; + +export const komodoRestVersion = "v1"; + export const environment = { production: true, + komodoEngine: komodoEngine, + + komodoRestVersion: komodoRestVersion, + // the home page path homePagePath: DataservicesConstants.dataservicesRootPath, // REST URL - Komodo import export url - komodoImportExportUrl: "https://localhost:8443/vdb-builder/v1/importexport", + komodoImportExportUrl: "/" + komodoEngine + "/" + komodoRestVersion + "/importexport", // REST URL - Komodo workspace - komodoWorkspaceUrl: "https://localhost:8443/vdb-builder/v1/workspace", + komodoWorkspaceUrl: "/" + komodoEngine + "/" + komodoRestVersion + "/workspace", // REST URL - Komodo teiid server - komodoTeiidUrl: "https://localhost:8443/vdb-builder/v1/teiid", + komodoTeiidUrl: "/" + komodoEngine + "/" + komodoRestVersion + "/teiid", // REST URL - Komodo service - komodoServiceUrl: "https://localhost:8443/vdb-builder/v1/service", + komodoServiceUrl: "/" + komodoEngine + "/" + komodoRestVersion + "/service" }; diff --git a/ngapp/src/environments/environment.ts b/ngapp/src/environments/environment.ts index b5154bc5..b9045402 100644 --- a/ngapp/src/environments/environment.ts +++ b/ngapp/src/environments/environment.ts @@ -21,22 +21,30 @@ import { DataservicesConstants } from "@dataservices/shared/dataservices-constan // `ng build --env=prod` then `environment.prod.ts` will be used instead. // The list of which env maps to which file can be found in `.angular-cli.json`. +export const komodoEngine = "vdb-builder"; + +export const komodoRestVersion = "v1"; + export const environment = { production: false, + komodoEngine: komodoEngine, + + komodoRestVersion: komodoRestVersion, + // the home page path homePagePath: DataservicesConstants.dataservicesRootPath, // REST URL - Komodo import export url - komodoImportExportUrl: "https://localhost:8443/vdb-builder/v1/importexport", + komodoImportExportUrl: "/" + komodoEngine + "/" + komodoRestVersion + "/importexport", // REST URL - Komodo workspace - komodoWorkspaceUrl: "https://localhost:8443/vdb-builder/v1/workspace", + komodoWorkspaceUrl: "/" + komodoEngine + "/" + komodoRestVersion + "/workspace", // REST URL - Komodo teiid server - komodoTeiidUrl: "https://localhost:8443/vdb-builder/v1/teiid", + komodoTeiidUrl: "/" + komodoEngine + "/" + komodoRestVersion + "/teiid", // REST URL - Komodo service - komodoServiceUrl: "https://localhost:8443/vdb-builder/v1/service", + komodoServiceUrl: "/" + komodoEngine + "/" + komodoRestVersion + "/service" };