Skip to content

Commit 75e6205

Browse files
committed
conversion to new name and version (changed apis)
1 parent 8a6612f commit 75e6205

8 files changed

Lines changed: 567 additions & 177 deletions

File tree

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ The `options` parameter is optional, and can contain the following properties:
7272

7373
If not specified, the name will looked for in the following places:
7474

75-
* the `application_name` property of the `VCAP_APPLICATION` environment variable
76-
* the `name` property from the`manifest.yml` file in the current directory
75+
* the `name` property of the `VCAP_APPLICATION` environment variable
76+
* the `name` property from the `manifest.yml` file in the current directory
77+
* the `name` property from the `package.json` file in the current directory
7778

7879
* `protocol` - protocol used in the generated URLs
7980

@@ -87,6 +88,9 @@ The `options` parameter is optional, and can contain the following properties:
8788
properties `application` and/or `services`, whose values are the same
8889
as the values serialized in the respective environment variables.
8990

91+
Note that the `url` and `urls` properties of the returned object are not
92+
based on the vcap `application` object, when running locally.
93+
9094
This option property is ignored if not running locally.
9195

9296
This function returns an object with the following properties:
@@ -250,6 +254,16 @@ Assume you run the following code:
250254

251255
The `url` result will be `https://userid:passw0rd@example.com/database`
252256

257+
Note that there **MUST** be a `url` property in the credentials,
258+
or replacement for it, in the
259+
service, or the call will return `null`. Also, because the `url` is parsed
260+
first with `url.parse()`, there will be a `host` property in the result, so
261+
you won't be able to use the `hostname` and `port` values directly. You
262+
can **ONLY** set the resultant hostname and port with the `host` property.
263+
264+
Since the `appEnv.getServiceURL()` method operates against the
265+
`appEnv.services` property, you can fudge this object if that makes your
266+
life easier.
253267

254268

255269
testing with Cloud Foundry

jbuild.coffee

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ tasks = defineTasks exports,
1414
build: "build the server"
1515
test: "run tests"
1616

17-
WatchSpec = "lib-src/**/* tests/**/*"
17+
WatchSpec = "lib-src lib-src/* tests tests/*"
1818

1919
#-------------------------------------------------------------------------------
2020
mkdir "-p", "tmp"
@@ -35,8 +35,6 @@ tasks.build = ->
3535

3636
cleanDir "lib"
3737

38-
log "running build"
39-
4038
log "- compiling server coffee files"
4139
coffee "--output lib lib-src"
4240

lib-src/cfenv.coffee

Lines changed: 81 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
fs = require "fs"
44
URL = require "url"
55

6+
pkg = require "../package.json"
67
_ = require "underscore"
78
ports = require "ports"
89
yaml = 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
#-------------------------------------------------------------------------------
134137
getPort = (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
#-------------------------------------------------------------------------------
148176
getBind = (appEnv) ->
149177
return appEnv.app?.host || "localhost"
150178

151179
#-------------------------------------------------------------------------------
152180
getURLs = (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
#

lib-src/server.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ exports.main = ->
99
appEnv = cfenv.getAppEnv()
1010

1111
dump = generateDump appEnv
12-
console.log "#{dump}\n"
12+
# console.log "#{dump}\n"
1313

1414
server = http.createServer()
1515

0 commit comments

Comments
 (0)