Change the way config is stored

On ember-cli config is stored in config/environment.js file and it can be
accessed at any time of app being booted. Till now we were using Travis.config
which was making things hard, because we needed an application instance to get
any config value. This commit moves config to config/environment.js and allows
to access it at any point of loading the app.
This commit is contained in:
Piotr Sarnacki 2015-01-30 15:29:44 +01:00
parent 6460c6692d
commit b0b1ef305b
12 changed files with 103 additions and 81 deletions

View File

@ -7,7 +7,8 @@ App = Ember.Application.extend(Ember.Evented,
#LOG_RESOLVER: true #LOG_RESOLVER: true
setup: -> setup: ->
@pusher = new Travis.Pusher(key: Travis.config.pusher_key, host: Travis.config.pusher_host) if Travis.config.pusher_key if @config.pusher.key
@pusher = new Travis.Pusher(@config.pusher)
@tailing = new Travis.Tailing($(window), '#tail', '#log') @tailing = new Travis.Tailing($(window), '#tail', '#log')
@toTop = new Travis.ToTop($(window), '.to-top', '#log-container') @toTop = new Travis.ToTop($(window), '.to-top', '#log-container')

View File

@ -1,4 +1,4 @@
require 'travis/limited-array' require 'utils/limited-array'
Repo = Travis.Repo Repo = Travis.Repo
limit = Ember.computed.limit limit = Ember.computed.limit

View File

@ -1,76 +1,16 @@
loadConfig = -> require 'config/environment'
pages_endpoint = $('meta[rel="travis.pages_endpoint"]').attr('href')
billing_endpoint = $('meta[rel="travis.billing_endpoint"]').attr('href')
customer_io_site_id = $('meta[name="travis.customer_io_site_id"]').attr('value')
setupCustomerio(customer_io_site_id) if customer_io_site_id
enterprise = $('meta[name="travis.enterprise"]').attr('value') == 'true' config = ENV.config
# for now I set pro to true also for enterprise, but it should be changed
# to allow more granular config later
pro = $('meta[name="travis.pro"]').attr('value') == 'true' || enterprise
if config.pro
pusher =
channels: []
channel_prefix: 'private-'
encrypted: true
key: ''
else
pusher =
channels: ['common']
channel_prefix: ''
encrypted: false
return {
syncingPageRedirectionTime: 5000
api_endpoint: $('meta[rel="travis.api_endpoint"]').attr('href')
source_endpoint: $('meta[rel="travis.source_endpoint"]').attr('href')
pusher_key: $('meta[name="travis.pusher_key"]').attr('value')
pusher_host: $('meta[name="travis.pusher_host"]').attr('value')
pusher_path: $('meta[name="travis.pusher_path"]').attr('value')
ga_code: $('meta[name="travis.ga_code"]').attr('value')
code_climate: $('meta[name="travis.code_climate"]').attr('value')
ssh_key_enabled: $('meta[name="travis.ssh_key_enabled"]').attr('value') == 'true'
code_climate_url: $('meta[name="travis.code_climate_url"]').attr('value')
caches_enabled: $('meta[name="travis.caches_enabled"]').attr('value') == 'true'
show_repos_hint: 'private'
avatar_default_url: 'https://travis-ci.org/images/ui/default-avatar.png'
pusher_log_fallback: $('meta[name="travis.pusher_log_fallback"]').attr('value') == 'true'
pro: pro
enterprise: enterprise
sidebar_support_box: pro && !enterprise
pages_endpoint: pages_endpoint || billing_endpoint
billing_endpoint: billing_endpoint
url_legal: "#{billing_endpoint}/pages/legal"
url_imprint: "#{billing_endpoint}/pages/imprint"
url_security: "#{billing_endpoint}/pages/security"
url_terms: "#{billing_endpoint}/pages/terms"
customer_io_site_id: customer_io_site_id
intervals: { times: -1, updateTimes: 1000 }
pusher: pusher
}
initialize = (container, application) -> initialize = (container, application) ->
config = application.config
application.register 'config:main', config, { instantiate: false } application.register 'config:main', config, { instantiate: false }
application.inject('controller', 'config', 'config:main') application.inject('controller', 'config', 'config:main')
application.inject('route', 'config', 'config:main') application.inject('route', 'config', 'config:main')
application.inject('auth', 'config', 'config:main')
application.pusher.config = config
ConfigInitializer = ConfigInitializer =
name: 'config' name: 'config'
initialize: initialize initialize: initialize
Ember.onLoad 'Ember.Application', (Application) -> Ember.onLoad 'Ember.Application', (Application) ->
Application.config loadConfig()
Application.ajax.pro = Application.config.pro
Application.initializer ConfigInitializer Application.initializer ConfigInitializer

View File

@ -1,4 +1,4 @@
Travis.Model = Model Model = Travis.Model
SshKey = Model.extend SshKey = Model.extend
value: DS.attr() value: DS.attr()

View File

@ -1,6 +1,8 @@
require 'travis/location' require 'travis/location'
require 'routes/application' require 'routes/application'
config = ENV.config
Router = Ember.Router.extend Router = Ember.Router.extend
location: 'history' location: 'history'
@ -32,14 +34,14 @@ Router.map ->
@resource 'pullRequests', path: '/pull_requests' @resource 'pullRequests', path: '/pull_requests'
@resource 'branches', path: '/branches' @resource 'branches', path: '/branches'
@resource 'requests', path: '/requests' @resource 'requests', path: '/requests'
@resource 'caches', path: '/caches' @resource 'caches', path: '/caches' if config.endpoints.caches
@resource 'request', path: '/requests/:request_id' @resource 'request', path: '/requests/:request_id'
@resource 'settings', -> @resource 'settings', ->
@route 'index', path: '/' @route 'index', path: '/'
@resource 'env_vars', -> @resource 'env_vars', ->
@route 'new' @route 'new'
@resource 'ssh_key' @resource 'ssh_key' if config.endpoints.ssh_key
@route 'first_sync' @route 'first_sync'
@route 'insufficient_oauth_permissions' @route 'insufficient_oauth_permissions'

View File

@ -1,3 +1,5 @@
config = ENV.config
Auth = Ember.Object.extend Auth = Ember.Object.extend
state: "signed-out" state: "signed-out"
receivingEnd: "#{location.protocol}//#{location.host}" receivingEnd: "#{location.protocol}//#{location.host}"
@ -9,7 +11,7 @@ Auth = Ember.Object.extend
Travis.sessionStorage.getItem('travis.token') Travis.sessionStorage.getItem('travis.token')
endpoint: (-> endpoint: (->
@config.api_endpoint config.api_endpoint
).property(), ).property(),
signOut: -> signOut: ->
@ -50,7 +52,7 @@ Auth = Ember.Object.extend
validateUser: (user) -> validateUser: (user) ->
fieldsToValidate = ['id', 'login', 'token', 'correct_scopes'] fieldsToValidate = ['id', 'login', 'token', 'correct_scopes']
if @config.pro if config.pro
fieldsToValidate.push 'channels' fieldsToValidate.push 'channels'
fieldsToValidate.every( (field) => @validateHas(field, user) ) && user.correct_scopes fieldsToValidate.every( (field) => @validateHas(field, user) ) && user.correct_scopes

View File

@ -1,4 +1,4 @@
Ember.computed.limit = function(dependentKey, limitKey) { limit = function(dependentKey, limitKey) {
var options = { var options = {
addedItem: function(array, item, changeMeta, instanceMeta) { addedItem: function(array, item, changeMeta, instanceMeta) {
var limit = Ember.get(this, limitKey); var limit = Ember.get(this, limitKey);
@ -24,3 +24,5 @@ Ember.computed.limit = function(dependentKey, limitKey) {
}; };
return Ember.arrayComputed(dependentKey, limitKey, options); return Ember.arrayComputed(dependentKey, limitKey, options);
}; };
Ember.computed.limit = limit;

View File

@ -1,7 +1,9 @@
computedLimit = Ember.computed.limit
LimitedArray = Ember.ArrayProxy.extend LimitedArray = Ember.ArrayProxy.extend
limit: 10 limit: 10
isLoadedBinding: 'content.isLoaded' isLoadedBinding: 'content.isLoaded'
arrangedContent: Ember.computed.limit('content', 'limit') arrangedContent: computedLimit('content', 'limit')
totalLength: (-> totalLength: (->
@get('content.length') @get('content.length')

View File

@ -1,3 +1,5 @@
config = ENV.config
TravisPusher = (config) -> TravisPusher = (config) ->
@init(config) @init(config)
this this
@ -7,7 +9,7 @@ TravisPusher.prototype.active_channels = []
TravisPusher.prototype.init = (config) -> TravisPusher.prototype.init = (config) ->
Pusher.warn = @warn.bind(this) Pusher.warn = @warn.bind(this)
Pusher.host = config.host if config.host Pusher.host = config.host if config.host
@pusher = new Pusher(config.key, encrypted: @config.pusher.encrypted, disableStats: true) @pusher = new Pusher(config.key, encrypted: config.encrypted, disableStats: true)
@callbacksToProcess = [] @callbacksToProcess = []
@ -97,9 +99,9 @@ TravisPusher.prototype.ignoreCode = (code) ->
TravisPusher.prototype.ignoreMessage = (message) -> TravisPusher.prototype.ignoreMessage = (message) ->
message.indexOf('Existing subscription') == 0 or message.indexOf('No current subscription') == 0 message.indexOf('Existing subscription') == 0 or message.indexOf('No current subscription') == 0
Pusher.SockJSTransport.isSupported = -> false if pusher_host != 'ws.pusherapp.com' Pusher.SockJSTransport.isSupported = -> false if config.pusher.host != 'ws.pusherapp.com'
if Travis.config.pro if config.pro
Pusher.channel_auth_transport = 'bulk_ajax' Pusher.channel_auth_transport = 'bulk_ajax'
Pusher.authorizers.bulk_ajax = (socketId, _callback) -> Pusher.authorizers.bulk_ajax = (socketId, _callback) ->
@ -122,8 +124,8 @@ if Travis.config.pro
Pusher.getDefaultStrategy = (config) -> Pusher.getDefaultStrategy = (config) ->
[ [
[":def", "ws_options", { [":def", "ws_options", {
hostUnencrypted: config.wsHost + ":" + config.wsPort + (pusher_path && "/#{pusher_path}" || ''), hostUnencrypted: config.wsHost + ":" + config.wsPort + (config.pusher.path && "/#{config.pusher.path}" || ''),
hostEncrypted: config.wsHost + ":" + config.wssPort + (pusher_path && "/#{pusher_path}" || '') hostEncrypted: config.wsHost + ":" + config.wssPort + (config.pusher.path && "/#{config.pusher.path}" || '')
path: config.path path: config.path
}], }],
[":def", "sockjs_options", { [":def", "sockjs_options", {

View File

@ -0,0 +1,70 @@
var billing_endpoint, customer_io_site_id, enterprise, pages_endpoint, pro, pusher, pusher_host, pusher_key, pusher_log_fallback, pusher_path;
pages_endpoint = $('meta[rel="travis.pages_endpoint"]').attr('href');
billing_endpoint = $('meta[rel="travis.billing_endpoint"]').attr('href');
customer_io_site_id = $('meta[name="travis.customer_io_site_id"]').attr('value');
if (customer_io_site_id) {
setupCustomerio(customer_io_site_id);
}
enterprise = $('meta[name="travis.enterprise"]').attr('value') === 'true';
pro = $('meta[name="travis.pro"]').attr('value') === 'true' || enterprise;
pusher_key = $('meta[name="travis.pusher_key"]').attr('value');
pusher_host = $('meta[name="travis.pusher_host"]').attr('value');
pusher_path = $('meta[name="travis.pusher_path"]').attr('value');
pusher_log_fallback = $('meta[name="travis.pusher_log_fallback"]').attr('value') === 'true';
if (pro) {
pusher = {
channels: [],
channel_prefix: 'private-',
encrypted: true
};
} else {
pusher = {
channels: ['common'],
channel_prefix: '',
encrypted: false
};
}
pusher.key = pusher_key;
pusher.host = pusher_host;
pusher.path = pusher_path;
pusher.log_fallback = pusher_log_fallback;
config = {
syncingPageRedirectionTime: 5000,
api_endpoint: $('meta[rel="travis.api_endpoint"]').attr('href'),
source_endpoint: $('meta[rel="travis.source_endpoint"]').attr('href'),
ga_code: $('meta[name="travis.ga_code"]').attr('value'),
code_climate: $('meta[name="travis.code_climate"]').attr('value'),
endpoints: {
ssh_key: $('meta[name="travis.ssh_key_enabled"]').attr('value') === 'true',
caches: $('meta[name="travis.caches_enabled"]').attr('value') === 'true'
},
code_climate_url: $('meta[name="travis.code_climate_url"]').attr('value'),
show_repos_hint: 'private',
avatar_default_url: 'https://travis-ci.org/images/ui/default-avatar.png',
pro: pro,
enterprise: enterprise,
sidebar_support_box: pro && !enterprise,
pages_endpoint: pages_endpoint || billing_endpoint,
billing_endpoint: billing_endpoint,
url_legal: billing_endpoint + "/pages/legal",
url_imprint: billing_endpoint + "/pages/imprint",
url_security: billing_endpoint + "/pages/security",
url_terms: billing_endpoint + "/pages/terms",
customer_io_site_id: customer_io_site_id,
intervals: {
times: -1,
updateTimes: 1000
},
pusher: pusher
};
if(!window.ENV) {
window.ENV = {};
}
window.ENV.config = config;

View File

@ -1,3 +1,5 @@
config = ENV.config
jQuery.support.cors = true jQuery.support.cors = true
default_options = default_options =
@ -18,7 +20,7 @@ Travis.ajax = Em.Object.create
@ajax(url, 'patch', data: data, success: callback) @ajax(url, 'patch', data: data, success: callback)
needsAuth: (method, url) -> needsAuth: (method, url) ->
return true if Travis.ajax.pro return true if config.pro
return true if method != 'GET' return true if method != 'GET'
publicEndpoint = @publicEndpoints.find (pattern) -> publicEndpoint = @publicEndpoints.find (pattern) ->
@ -36,7 +38,7 @@ Travis.ajax = Em.Object.create
method = method || "GET" method = method || "GET"
method = method.toUpperCase() method = method.toUpperCase()
endpoint = Travis.config.api_endpoint || '' endpoint = config.api_endpoint || ''
options = options || {} options = options || {}
token = Travis.sessionStorage.getItem('travis.token') token = Travis.sessionStorage.getItem('travis.token')

View File

@ -1,6 +1,5 @@
require 'ext/jquery' require 'config/environment'
require 'ext/ember/namespace' require 'utils/computed-limit'
require 'ext/ember/computed'
require 'app' require 'app'
window.ENV ||= {} window.ENV ||= {}