Skip to content
This repository was archived by the owner on May 15, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ request with the header `Host: pow`. The available endpoints are:
environment, which is inherited by each application worker.
* `/config.json`: A JSON-encoded object representing the running
server's [configuration](http://pow.cx/docs/configuration.html).
* `/apps.json`: A JSON-encoded object representing the running
server's running applications.

Example of requesting an endpoint with `curl`:

Expand Down Expand Up @@ -463,6 +465,9 @@ looks like.
redirection is now handled by the pf packet filter.
* Upgrades bundled Node version to 0.10.32.

* **0.4.4** (September 16, 2014):
* Added support for `http://pow/apps.json` endpoint.

* **0.4.3** (April 3, 2014):
* Upgrade Nack to 0.16 to fix issues when requiring the json
library.
Expand Down
13 changes: 13 additions & 0 deletions lib/http_server.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 21 additions & 2 deletions lib/rack_application.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions src/http_server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ module.exports = class HttpServer extends connect.HTTPServer
version: version
requestCount: @requestCount

# Gets an array of objects describing the server's currently
# running applications passed to `JSON.stringify`.
runningApps: ->
apps = []
for rootPath of @rackApplications
apps.push @rackApplications[rootPath]
apps

# The first middleware in the stack logs each incoming request's
# source address, method, hostname, and path to the access log
# (`~/Library/Logs/Pow/access.log` by default).
Expand All @@ -109,6 +117,8 @@ module.exports = class HttpServer extends connect.HTTPServer
# applications inherit.
# * `/status.json`: Returns information about the current server
# version, number of requests handled, and process ID.
# * `/apps.json`: Returns information about the currently running
# applications from the `rackApplications` instance.
#
# Third-party utilities may use these endpoints to inspect a running
# Pow server.
Expand All @@ -125,6 +135,9 @@ module.exports = class HttpServer extends connect.HTTPServer
when "/status.json"
res.writeHead 200
res.end JSON.stringify this
when "/apps.json"
res.writeHead 200
res.end JSON.stringify @runningApps()
else
@handleLocationNotFound req, res, next

Expand Down
21 changes: 19 additions & 2 deletions src/rack_application.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ module.exports = class RackApplication
@quitCallbacks = []
@statCallbacks = []

# Gets an object describing the server's current status that can be
# passed to `JSON.stringify`.
toJSON: ->
root: @root
host: @firstHost
startedAt: Math.round(@started / 1000)
lastRestartedAt: Math.round(@mtime / 1000)
lastRequestAt: Math.round(@lastRequestTime / 1000)
requestCount: @requestCount
timeout: Math.round(@timeout / 1000)
mightIdleAt: Math.round((@lastRequestTime + @timeout) / 1000)
status: if ((+new Date) - @lastRequestTime) > @timeout then 'idle' else 'active'

# Queue `callback` to be invoked when the application becomes ready,
# then start the initialization process. If the application's state
# is ready, the callback is invoked immediately.
Expand Down Expand Up @@ -138,6 +151,7 @@ module.exports = class RackApplication
# uninitialized state. (If the application is terminating, queue a
# call to `initialize` after all workers have exited.)
initialize: ->
@requestCount = 0
if @state
if @state is "terminating"
@quit => @initialize()
Expand All @@ -160,11 +174,12 @@ module.exports = class RackApplication
# the application's environment or the global configuration.
else
@state = "ready"

@started = +new Date
@timeout = (env?.POW_TIMEOUT ? @configuration.timeout) * 1000
@pool = nack.createPool join(@root, "config.ru"),
env: env
size: env?.POW_WORKERS ? @configuration.workers
idle: (env?.POW_TIMEOUT ? @configuration.timeout) * 1000
idle: @timeout

# Log the workers' stderr and stdout, and log each worker's
# PID as it spawns and exits.
Expand Down Expand Up @@ -214,6 +229,8 @@ module.exports = class RackApplication
req.proxyMetaVariables =
SERVER_PORT: @configuration.dstPort.toString()
try
@lastRequestTime = +new Date
@requestCount++
@pool.proxy req, res, (err) =>
@quit() if err
next err
Expand Down
8 changes: 8 additions & 0 deletions test/test_http_server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,11 @@ module.exports = testCase
test.same 200, response.statusCode
test.same server.toJSON(), JSON.parse body
done -> test.done()

"http://pow/apps.json": (test) ->
test.expect 2
serveRoot "apps", (request, done, server) ->
request "GET", "/apps.json", host: "pow", (body, response) ->
test.same 200, response.statusCode
test.same server.runningApps(), JSON.parse body
done -> test.done()