From 80c33f5303e2c083841933de4338f7e5d15de972 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Mon, 3 Apr 2017 09:20:59 +0200 Subject: Factor out hardcoded SERVER_BASE_URL --- .gitignore | 3 +++ gulpfile.js | 26 +++++++++++++++++++++----- sample_config.json | 3 ++- src/404.html | 2 +- src/index.html | 4 ++-- src/scripts/controllers/connection/socket.ts | 2 +- src/scripts/splash.entry.ts | 4 ++-- src/scripts/user.ts | 17 ++--------------- tslint.json | 2 +- 9 files changed, 35 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index 49684fa2..11181e8c 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,6 @@ yarn.lock # WebStorm .idea/ + +# Standard config file +config.json diff --git a/gulpfile.js b/gulpfile.js index e57c31d6..579e795f 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -4,6 +4,9 @@ * Usage: * $ gulp --config=config.json # for a single build * $ gulp watch --config=config.json # to run once, watch for changes, and rebuild when something changed + * + * If the `config` argument is omitted, the config file is assumed to be named `config.json` and present in this + * directory. */ 'use strict'; @@ -16,6 +19,7 @@ const gulpUtil = require('gulp-util'); const rename = require('gulp-rename'); const replace = require('gulp-replace'); const del = require('del'); +const fs = require('fs'); const runSequence = require('run-sequence'); const source = require('vinyl-source-stream'); const es = require('event-stream'); @@ -35,12 +39,17 @@ const bower = require('gulp-bower'); * @returns {Object} the config file contents. */ function getConfigFile() { - const configInput = argv.config; + let configInput = argv.config; if (configInput === undefined) { - gulpUtil.log(gulpUtil.colors.red('Config file argument missing\n'), 'Usage:\n' + - ' $ gulp --config=config.json'); - throw new Exception(); + if (!fs.existsSync("./config.json")) { + gulpUtil.log(gulpUtil.colors.red('Config file argument missing\n'), 'Usage:\n' + + ' $ gulp --config=config.json'); + throw new Exception(); + } else { + gulpUtil.log(gulpUtil.colors.magenta('No config file argument, assuming `config.json`.')); + configInput = "config.json"; + } } try { @@ -91,6 +100,8 @@ const scriptsFilePaths = scriptsFileNames.map(function (fileName) { }); gulp.task('scripts', function () { + const configFile = getConfigFile(); + const tasks = scriptsFilePaths.map(function (entry, index) { return browserify({ entries: [entry], @@ -102,6 +113,7 @@ gulp.task('scripts', function () { .plugin(tsify) .bundle() .pipe(source(scriptsFileNames[index] + postfix + '.js')) + .pipe(replace('SERVER_BASE_URL', configFile.SERVER_BASE_URL)) .pipe(gulp.dest(scriptsDestDir)); }); return es.merge.apply(null, tasks) @@ -109,11 +121,14 @@ gulp.task('scripts', function () { }); function getWatchifyHandler(bundler, fileName) { + const configFile = getConfigFile(); + return () => { - gulpUtil.log("Beginning build for " + fileName); + gulpUtil.log('Beginning build for ' + fileName); return bundler .bundle() .pipe(source(fileName + postfix + '.js')) + .pipe(replace('SERVER_BASE_URL', configFile.SERVER_BASE_URL)) .pipe(gulp.dest(scriptsDestDir)); }; } @@ -166,6 +181,7 @@ gulp.task('html', function () { return gulp.src(htmlFilePaths) .pipe(replace('GOOGLE_OAUTH_CLIENT_ID', configFile.GOOGLE_OAUTH_CLIENT_ID)) + .pipe(replace('SERVER_BASE_URL', configFile.SERVER_BASE_URL)) .pipe(processHTML()) .pipe(gulp.dest(htmlDestDir)) .pipe(notify({message: 'HTML task complete', onLast: true})); diff --git a/sample_config.json b/sample_config.json index 628476ed..58ff0e72 100644 --- a/sample_config.json +++ b/sample_config.json @@ -1,3 +1,4 @@ { - "GOOGLE_OAUTH_CLIENT_ID": "the-google-oauth-client-id" + "GOOGLE_OAUTH_CLIENT_ID": "the-google-oauth-client-id", + "SERVER_BASE_URL": "http://localhost:8081" } diff --git a/src/404.html b/src/404.html index 84182341..10f79525 100644 --- a/src/404.html +++ b/src/404.html @@ -20,7 +20,7 @@
Got lost?_
- GET ME BACK TO OPENDC + GET ME BACK TO OPENDC diff --git a/src/index.html b/src/index.html index 8ea4ce49..c58aaf01 100644 --- a/src/index.html +++ b/src/index.html @@ -11,8 +11,8 @@ - - + + diff --git a/src/scripts/controllers/connection/socket.ts b/src/scripts/controllers/connection/socket.ts index c23495f1..91a0f9e4 100644 --- a/src/scripts/controllers/connection/socket.ts +++ b/src/scripts/controllers/connection/socket.ts @@ -15,7 +15,7 @@ export class SocketController { this.callbacks = {}; this._cacheController = new CacheController(); - this._socket = io.connect('https://opendc.ewi.tudelft.nl:443'); + this._socket = io.connect('SERVER_BASE_URL'); this._socket.on('connect', onConnect); this._socket.on('response', (jsonResponse: string) => { diff --git a/src/scripts/splash.entry.ts b/src/scripts/splash.entry.ts index 700f52bb..6abd9518 100644 --- a/src/scripts/splash.entry.ts +++ b/src/scripts/splash.entry.ts @@ -119,7 +119,7 @@ window["renderButton"] = () => { // Calculate token expiration time (in seconds since epoch) const expirationTime = (new Date()).getTime() / 1000 + googleUser.getAuthResponse().expires_in - 5; - $.post('https://opendc.ewi.tudelft.nl/tokensignin', { + $.post('SERVER_BASE_URL/tokensignin', { idtoken: id_token }, (data: any) => { // Save user information in session storage for later use on other pages @@ -146,7 +146,7 @@ window["renderButton"] = () => { }); }, 'onfailure': () => { - console.log("Oops, something went wrong with your Google signin... Try again?") + console.log("Oops, something went wrong with your Google signin... Try again?"); } }); }; diff --git a/src/scripts/user.ts b/src/scripts/user.ts index 66e44b21..e9d13091 100644 --- a/src/scripts/user.ts +++ b/src/scripts/user.ts @@ -2,24 +2,11 @@ import * as $ from "jquery"; -const LOCAL_MODE = (document.location.hostname === "localhost"); - // Redirect the user to the splash page, if not signed in -if (!LOCAL_MODE && localStorage.getItem("googleToken") === null) { +if (localStorage.getItem("googleToken") === null) { window.location.replace("/"); } -// Fill session storage with mock data during LOCAL_MODE -if (LOCAL_MODE) { - localStorage.setItem("googleToken", ""); - localStorage.setItem("googleTokenExpiration", "2000000000"); - localStorage.setItem("googleName", "John Doe"); - localStorage.setItem("googleEmail", "john@doe.com"); - localStorage.setItem("userId", "2"); - localStorage.setItem("simulationId", "1"); - localStorage.setItem("simulationAuthLevel", "OWN"); -} - // Set the username in the navbar $("nav .user .username").text(localStorage.getItem("googleName")); @@ -59,7 +46,7 @@ window["gapiSigninButton"] = () => { }, 'onfailure': () => { window.location.href = "/"; - console.log("Oops, something went wrong with your Google signin... Try again?") + console.log("Oops, something went wrong with your Google signin... Try again?"); } }); }; diff --git a/tslint.json b/tslint.json index b2c0ac60..75b8d041 100644 --- a/tslint.json +++ b/tslint.json @@ -32,7 +32,7 @@ "no-eval": true, "no-inferrable-types": true, "no-shadowed-variable": true, - "no-string-literal": true, + "no-string-literal": false, "no-switch-case-fall-through": true, "no-trailing-whitespace": true, "no-unused-expression": true, -- cgit v1.2.3