-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathserver.ls
86 lines (69 loc) · 2.59 KB
/
server.ls
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
{bind-p, from-error-value-callback, new-promise, return-p, to-callback} = require \./async-ls
require! \base62
require! \body-parser
{http-port}:config = require \./config
require! \express
{each, fold, map, Str} = require \prelude-ls
# QueryStore
err, query-store <- to-callback do
(require "./query-stores/#{config.query-store.name}") config.query-store[config.query-store.name]
if err
console.log "unable to connect to query store: #{err.to-string!}"
return
else
console.log "successfully connected to query store"
# CacheStore
err, cache-store <- to-callback do
(require "./cache-stores/#{config.cache-store.name}") config.cache-store[config.cache-store.name]
if err
console.log "unable to connect to cache store: #{err.to-string!}"
return
else
console.log "successfully connected to cache store"
# OpsManager
require! \./OpsManager
ops-manager = new OpsManager cache-store
# Spy
pipend-spy = (require \pipend-spy) config?.spy?.storage-details
spy =
| config?.spy?.enabled => pipend-spy
| _ =>
record: (event-object) -> return-p [event-object]
record-req: (req, event-object) -> return-p [event-object]
routes = (require \./routes) query-store, ops-manager, spy
app = express!
..set \views, __dirname + \/
..engine \.html, (require \ejs).__express
..set 'view engine', \ejs
..use (require \cors)!
..use (require \serve-favicon) __dirname + '/public/images/favicon.png'
..use (require \cookie-parser)!
# with-optional-params :: [String] -> [String] -> [String]
with-optional-params = (routes, params) -->
routes |> fold do
(acc, value) ->
new-routes = [0 to params.length]
|> map (i) ->
[0 til i]
|> map -> ":#{params[it]}"
|> Str.join \/
|> map -> "#{value}/#{it}"
acc ++ new-routes
[]
routes |> each ({methods, patterns, optional-params or [], request-handler}?) !->
methods |> each (method) !->
if patterns
(patterns `with-optional-params` optional-params) |> each (pattern) ->
app[method] pattern, request-handler
else
app[method] request-handler
server = app.listen http-port
console.log "listening for connections on port: #{http-port}"
# emit all the running ops to the client
io = (require \socket.io) server
..on \connect, (connection) ->
connection.emit \ops, ops-manager.running-ops!
ops-manager.on \change, -> io.emit \ops, ops-manager.running-ops!
set-interval do
-> io.emit \ops, ops-manager.running-ops!
1000