This repository was archived by the owner on Oct 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_bus_server.coffee
More file actions
106 lines (85 loc) · 3.04 KB
/
command_bus_server.coffee
File metadata and controls
106 lines (85 loc) · 3.04 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
http = require "http"
url = require "url"
formidable = require "formidable"
class CommandBusServer
JSON_MEDIA_TYPE_REGEXP = /^application\/(.+\+)?json$/i
OCTET_STREAM_MEDIA_TYPE_REGEXP = /^application\/octet-stream/i
constructor: ({@commandBus, @logger}) ->
throw new Error "Missing command bus" unless @commandBus?
throw new Error "Missing logger" unless @logger?
@server = http.createServer (req, res) =>
path = url.parse(req.url).path
method = req.method
if path is "/commands"
if method is "POST"
@_handleCommand req, res
else
code 405, res
else if path is "/uids"
if method is "POST"
@_handleCreateNewUid req, res
else
code 405, res
else
code 404, res
listen: (port) ->
@server.listen port
@logger.info "CommandBusServer", "listening on port #{port}..."
close: (callback) ->
@server.close callback
_handleCreateNewUid: (req, res) ->
@commandBus.createNewUid (err, uid) ->
if err?
djump res, 500, error: err
else
djump res, 200, uid: uid
_handleCommand: (req, res) ->
logger = @logger
commandName = null
payload = {}
form = new formidable.IncomingForm();
form.onPart = (part) ->
self = this
chunks = []
chunksLength = 0
part.on "data", (chunk) ->
self.pause()
chunks.push new Buffer chunk
chunksLength += chunk.length
self.resume()
part.on "end", ->
contentType = part.headers["content-type"]
data = Buffer.concat chunks, chunksLength
if contentType && contentType.match JSON_MEDIA_TYPE_REGEXP
data = JSON.parse data.toString()
else if not contentType
data = data.toString()
if part.name is "name" # command name
commandName = data
else if part.name.indexOf("payload.") is 0
propertyName = part.name.substring 8
payload[propertyName] = data
form.parse req, (err, fields, files) =>
if err?
logger.warning "CommandBusServer", "received command failed (#{err})"
return djump res, 400, error: err
unless commandName?
logger.warning "CommandBusServer", "missing command name"
return djump res, 400, error: new Error "Missing command name"
logger.log "CommandBusServer", "deserialize command \"#{commandName}\""
@commandBus.deserializeCommand commandName, payload, (err, command) =>
@commandBus.executeCommand command, (err) ->
if err?
logger.alert "CommandBusServer", "error while executing command (#{err})"
djump res, 500, error: err
else
logger.log "CommandBusServer", "command \"#{commandName}\" started successfully"
djump res, 202
djump = (res, code, obj) ->
res.statusCode = code
if obj?
res.setHeader "Content-Type", "application/vnd.djump-command-bus+json"
res.end JSON.stringify obj
else
res.end()
module.exports = CommandBusServer