From 84f875d9514a4cda17afac39e19d03ef9616d14e Mon Sep 17 00:00:00 2001 From: Eugen Ciur <eugen@papermerge.com> Date: Fri, 18 Feb 2022 19:43:30 +0100 Subject: [PATCH] get REST API host from window object --- app/adapters/application.js | 2 +- app/authenticators/auth-token.js | 2 +- app/controllers/login.js | 2 +- app/services/requests.js | 3 +- app/services/websockets.js | 2 +- app/services/ws_nodes_move.js | 2 +- app/utils/host.js | 54 ++++++++++++++++++++++++++++++++ app/utils/index.js | 52 ------------------------------ config/environment.js | 3 +- 9 files changed, 62 insertions(+), 60 deletions(-) create mode 100644 app/utils/host.js diff --git a/app/adapters/application.js b/app/adapters/application.js index c087b2c..8b0b00f 100644 --- a/app/adapters/application.js +++ b/app/adapters/application.js @@ -1,5 +1,5 @@ import JSONAPIAdapter from '@ember-data/adapter/json-api'; -import { inject as service } from '@ember/service'; +import { service } from '@ember/service'; import ENV from 'papermerge/config/environment'; diff --git a/app/authenticators/auth-token.js b/app/authenticators/auth-token.js index 8a00242..6dc1171 100644 --- a/app/authenticators/auth-token.js +++ b/app/authenticators/auth-token.js @@ -1,5 +1,5 @@ import Base from 'ember-simple-auth/authenticators/base'; -import { base_url } from 'papermerge/utils'; +import { base_url } from 'papermerge/utils/host'; export default class AuthToken extends Base { diff --git a/app/controllers/login.js b/app/controllers/login.js index d9ea8ea..520e587 100644 --- a/app/controllers/login.js +++ b/app/controllers/login.js @@ -2,7 +2,7 @@ import Controller from '@ember/controller'; import { service } from '@ember/service'; import { action } from '@ember/object'; import { tracked } from '@glimmer/tracking'; -import { base_url } from 'papermerge/utils'; +import { base_url } from 'papermerge/utils/host'; export default class LoginController extends Controller { diff --git a/app/services/requests.js b/app/services/requests.js index a370cbef..afab04b 100644 --- a/app/services/requests.js +++ b/app/services/requests.js @@ -3,11 +3,12 @@ import Service from '@ember/service'; import { computed } from '@ember/object'; import { service } from '@ember/service'; import { - base_url, insert_blob, extract_file_name } from 'papermerge/utils'; +import { base_url } from 'papermerge/utils/host'; + export default class Requests extends Service { @service session; diff --git a/app/services/websockets.js b/app/services/websockets.js index 408c284..a3ebbbb 100644 --- a/app/services/websockets.js +++ b/app/services/websockets.js @@ -1,5 +1,5 @@ import Service from '@ember/service'; -import { ws_base_url } from 'papermerge/utils'; +import { ws_base_url } from 'papermerge/utils/host'; export default class Websockets extends Service { diff --git a/app/services/ws_nodes_move.js b/app/services/ws_nodes_move.js index e0d4c5b..7bb4510 100644 --- a/app/services/ws_nodes_move.js +++ b/app/services/ws_nodes_move.js @@ -1,4 +1,4 @@ -import { ws_base_url } from 'papermerge/utils'; +import { ws_base_url } from 'papermerge/utils/host'; import Service from '@ember/service'; diff --git a/app/utils/host.js b/app/utils/host.js new file mode 100644 index 0000000..70219f4 --- /dev/null +++ b/app/utils/host.js @@ -0,0 +1,54 @@ +import ENV from 'papermerge/config/environment'; + + +function base_url() { + /* + Returns backend's REST API base url. + + base url is extracted from `window` object, which + means this method is valid only in browser environment. + + Notice there is no `/` at the end of returned string. + */ + let base = `${window.location.protocol}//${window.location.host}`; + + if (ENV.APP.HOST) { + // user can override BACKEND HOST by providing + // ENV.APP.HOST value + // e.g. ENV.APP.HOST = 'http://127.0.0.1:8000'; + base = ENV.APP.HOST; + } + + if (!ENV.APP.NAMESPACE) { + return base; + } + + return `${base}/${ENV.APP.NAMESPACE}`; +} + + +function ws_base_url() { + /* + websockets base url + */ + let base = `ws://${window.location.host}`; + + if (ENV.APP.WS_HOST) { + // user can override BACKEND HOST by providing + // ENV.APP.HOST value + // e.g. ENV.APP.HOST = 'ws://127.0.0.1:8000'; + base = ENV.WS_APP.HOST; + } + + if (!ENV.APP.WS_NAMESPACE) { + return base; + } + + return `${base}/${ENV.APP.WS_NAMESPACE}`; +} + + +export { + base_url, + ws_base_url, +}; diff --git a/app/utils/index.js b/app/utils/index.js index db4624e..e2559ed 100644 --- a/app/utils/index.js +++ b/app/utils/index.js @@ -63,56 +63,6 @@ function are_sets_equal(set1, set2) { return same_size(set1, set2) && same_values(set1, set2); } -function base_url() { - /* - Returns backend's REST API base url - - Notice there is no `/` at the end of returned string. - */ - let base = `${window.location.protocol}:/${window.location.host}`; - - if (!ENV.APP.HOST) { - - if (!ENV.APP.NAMESPACE) { - return base; - } - - return `${base}/${ENV.APP.NAMESPACE}`; - } - - if (!ENV.APP.NAMESPACE) { - return `${ENV.APP.HOST}`; - } - - return `${ENV.APP.HOST}/${ENV.APP.NAMESPACE}`; -} - -function ws_base_url() { - /* - websockets base url - */ - let base = `ws://${window.location.host}`; - - if (window.location.protocol == "https:") { - base = `wss://${window.location.host}`; - } - - if (!ENV.APP.WS_HOST) { - - if (!ENV.APP.WS_NAMESPACE) { - return base; - } - - return `${base}/${ENV.APP.WS_NAMESPACE}`; - } - - if (!ENV.APP.WS_NAMESPACE) { - return `${ENV.APP.WS_HOST}`; - } - - return `${ENV.APP.WS_HOST}/${ENV.APP.WS_NAMESPACE}`; -} - /** * Extracts file name from a response with accessible Content-Disposition header */ @@ -158,8 +108,6 @@ function insert_blob(file_name, blob) { export { group_perms_by_model, are_sets_equal, - base_url, - ws_base_url, insert_blob, extract_file_name }; diff --git a/config/environment.js b/config/environment.js index be4e8a2..10fba31 100644 --- a/config/environment.js +++ b/config/environment.js @@ -28,6 +28,7 @@ module.exports = function (environment) { if (environment === 'development') { ENV.APP.HOST = 'http://127.0.0.1:8000'; ENV.APP.WS_HOST = 'ws://127.0.0.1:8000'; + // ENV.APP.LOG_RESOLVER = true; // ENV.APP.LOG_ACTIVE_GENERATION = true; // ENV.APP.LOG_TRANSITIONS = true; @@ -53,8 +54,6 @@ module.exports = function (environment) { if (environment === 'production') { // here you can enable a production-specific feature - ENV.APP.HOST = 'http://papermerge.local'; - ENV.APP.WS_HOST = 'ws://papermerge.local'; } return ENV; -- GitLab