33fs = require " fs"
44URL = require " url"
55
6+ pkg = require " ../package.json"
67_ = require " underscore"
78ports = require " ports"
89yaml = require " js-yaml"
@@ -21,9 +22,15 @@ class AppEnv
2122
2223 # -----------------------------------------------------------------------------
2324 constructor : (options = {}) ->
24- @app = getApp options
25- @services = getServices options
2625 @isLocal = not process .env .VCAP_APPLICATION ?
26+ unless @isLocal
27+ try
28+ JSON .parse process .env .VCAP_APPLICATION
29+ catch
30+ @isLocal = true
31+
32+ @app = getApp @ , options
33+ @services = getServices @ , options
2734
2835 @name = getName @ , options
2936 @port = getPort @
@@ -66,6 +73,7 @@ class AppEnv
6673 # -----------------------------------------------------------------------------
6774 getServiceURL : (spec , replacements = {}) ->
6875 service = @ getService spec
76+
6977 credentials = service ? .credentials
7078 return null unless credentials?
7179
@@ -74,7 +82,7 @@ class AppEnv
7482 if replacements .url
7583 url = credentials[replacements .url ]
7684 else
77- url = credentials .url
85+ url = credentials .url || credentials . uri
7886
7987 return null unless url?
8088
@@ -92,43 +100,38 @@ class AppEnv
92100 return URL .format purl
93101
94102# -------------------------------------------------------------------------------
95- getApp = (options ) ->
96- val = options ? .vcap ? .application
97- return val if val?
98-
103+ getApp = (appEnv , options ) ->
99104 string = process .env .VCAP_APPLICATION
100- try
101- return JSON .parse string
102- catch e
103- return null
104105
105- # -------------------------------------------------------------------------------
106- getServices = (options ) ->
107- val = options ? .vcap ? .services
108- return val if val?
106+ envValue = {}
107+ if string?
108+ try
109+ envValue = JSON .parse string
110+ catch e
111+ throwError " env var VCAP_APPLICATION is not JSON: /#{ string} /"
109112
110- string = process .env .VCAP_SERVICES
111- try
112- return JSON .parse string
113- catch e
114- return null
113+ return envValue unless appEnv .isLocal
115114
116- # -------------------------------------------------------------------------------
117- getName = (appEnv , options ) ->
118- return options .name if options .name ?
119-
120- val = appEnv .app ? .name
121- return val if val?
115+ locValue = options ? .vcap ? .application
116+ return locValue if locValue?
117+ return envValue
122118
123- return null unless fs .existsSync " manifest.yml"
119+ # -------------------------------------------------------------------------------
120+ getServices = (appEnv , options ) ->
121+ string = process .env .VCAP_SERVICES
124122
125- yString = fs .readFileSync " manifest.yml" , " utf8"
126- yObject = yaml .safeLoad yString, filename : " manifest.yml"
123+ envValue = {}
124+ if string?
125+ try
126+ envValue = JSON .parse string
127+ catch e
128+ throwError " env var VCAP_SERVICES is not JSON: /#{ string} /"
127129
128- yObject = yObject .applications [0 ] if yObject .applications ?
129- return yObject .name if yObject .name ?
130+ return envValue unless appEnv .isLocal
130131
131- return null
132+ locValue = options ? .vcap ? .services
133+ return locValue if locValue?
134+ return envValue
132135
133136# -------------------------------------------------------------------------------
134137getPort = (appEnv ) ->
@@ -140,29 +143,70 @@ getPort = (appEnv) ->
140143 portString = " #{ ports .getPort appEnv .name } "
141144
142145 port = parseInt portString, 10
143- throw new Error " invalid port string: #{ portString} " if isNaN port
146+ throwError " invalid PORT value: / #{ portString} / " if isNaN port
144147
145148 return port
146149
150+ # -------------------------------------------------------------------------------
151+ getName = (appEnv , options ) ->
152+ return options .name if options .name ?
153+
154+ val = appEnv .app ? .name
155+ return val if val?
156+
157+ if fs .existsSync " manifest.yml"
158+ yString = fs .readFileSync " manifest.yml" , " utf8"
159+ yObject = yaml .safeLoad yString, filename : " manifest.yml"
160+
161+ yObject = yObject .applications [0 ] if yObject .applications ?
162+ return yObject .name if yObject .name ?
163+
164+ if fs .existsSync " package.json"
165+ pString = fs .readFileSync " package.json" , " utf8"
166+ try
167+ pObject = JSON .parse pString
168+ catch
169+ pObject = null
170+
171+ return pObject .name if pObject ? .name
172+
173+ return null
174+
147175# -------------------------------------------------------------------------------
148176getBind = (appEnv ) ->
149177 return appEnv .app ? .host || " localhost"
150178
151179# -------------------------------------------------------------------------------
152180getURLs = (appEnv , options ) ->
181+
153182 uris = appEnv .app ? .uris
154183
155- unless uris
156- protocol = options .protocol || " http:"
157- return [ " #{ protocol} //localhost:#{ appEnv .port } " ]
184+ if appEnv .isLocal
185+ uris = [ " localhost:#{ appEnv .port } " ]
186+
187+ else
188+ unless uris?
189+ throwError " expecting VCAP_APPLICATION to contain uris when not runninng locally"
158190
159- protocol = options .protocol || " https:"
191+ protocol = options .protocol
192+
193+ unless protocol?
194+ if appEnv .isLocal
195+ protocol = " http:"
196+ else
197+ protocol = " https:"
160198
161199 urls = for uri in uris
162200 " #{ protocol} //#{ uri} "
163201
164202 return urls
165203
204+ # -------------------------------------------------------------------------------
205+ throwError = (message ) ->
206+ message = " #{ pkg .name } : #{ message} "
207+ console .log " error: #{ message} "
208+ throw new Error message
209+
166210# -------------------------------------------------------------------------------
167211# Copyright IBM Corp. 2014
168212#
0 commit comments