-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.coffee
90 lines (75 loc) · 3.64 KB
/
application.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# $ requires npm install connect into
# require.paths [ '$HOME/.node_modules', '$HOME/.node_libraries', '/usr/lib/node' ]
fs = require 'fs'
connect = require 'connect'
process.on 'uncaughtException', (err) ->
console.log "Type: " + err.type
console.log "Message: " + err.message
console.log "Arguments: " + err.arguments
console.log err.stack
neo4jconfig = require "./neo4jconfig.coffee"
neo4jserver = require "./neo4jserver.coffee"
proxy = require "./proxy.coffee"
class ServerManager
constructor: ->
proxies = {}
auth = { user: "admin", pass: "admin"}
serverName = 'undefinded'
bringUp = (id) ->
config = neo4jconfig.parse id
neo4j = neo4jserver.create config, serverName
proxies[id] = proxy.buildFor neo4j, config
storeConfig = ->
config = auth: auth, serverName: serverName, proxies: (key for key of proxies)
fs.writeFileSync "config.json", JSON.stringify(config)
@loadConfig = ->
try
config = JSON.parse(fs.readFileSync "config.json")
serverName = config['serverName']
auth = config['auth']
for instance in config['proxies']
try bringUp instance catch e
console.log "error during startup for " + instance + " " + e
catch error
console.log error
@start = ->
connect(
connect.basicAuth (user, pass) -> auth.user == user && auth.pass == pass
connect.router (app) ->
app.get '/', (request, response) ->
console.log "status"
res = {}
res[instance] = proxy.running() for instance, proxy of proxies
response.end JSON.stringify(res) + "\n"
app.get '/start/:id', (request, response) ->
id = request.params.id
console.log "starting " + id
if (!proxies[id]) then throw new Error "instance " + id + " is not registered"
proxies[id].startServer (msg) ->
response.end request.params.id + " " + msg + "\n"
app.get '/stop/:id', (request, response) ->
id = request.params.id
console.log "stopping " + id
if (!proxies[id]) then throw new Error "instance " + id + " is not registered"
proxies[id].stopServer (msg) ->
response.end request.params.id + " " + msg + "\n"
app.post '/:id', (request, response) ->
id = request.params.id
console.log "adding " + id
if (proxies[id]) then throw new Error "instance " + id + " is already registered"
bringUp id
storeConfig()
response.end "add " + request.params.id + "\n"
app.delete '/:id', (request, response) ->
id = request.params.id
console.log "deleting " + id
if (!proxies[id]) then throw new Error "instance " + id + " is not registered"
proxies[id].stop()
delete proxies[id]
storeConfig()
response.end "delete " + request.params.id + "\n"
).listen(7999)
serverManager = new ServerManager()
serverManager.loadConfig()
serverManager.start()
console.log "neo4j-cloud keeper started"