docker based ci tool
修訂 | b779c83cfac44098eae93c487aa4db2d48a5b98d (tree) |
---|---|
時間 | 2019-06-06 01:17:59 |
作者 | hylom <hylom@user...> |
Commiter | hylom |
frontend: implement task launcher
@@ -4,6 +4,9 @@ exports.config = { | ||
4 | 4 | newslash: { |
5 | 5 | token: "ASEgbfsREAsea", |
6 | 6 | }, |
7 | + test_task: { | |
8 | + token: "test_token", | |
9 | + }, | |
7 | 10 | }, |
8 | 11 | }; |
9 | 12 |
@@ -7,6 +7,9 @@ const config = require('./config').config; | ||
7 | 7 | const bodyParser = require('body-parser'); |
8 | 8 | const Busboy = require('busboy'); |
9 | 9 | |
10 | +const grpc = require('grpc'); | |
11 | +const protoLoader = require('@grpc/proto-loader'); | |
12 | + | |
10 | 13 | const app = connect(); |
11 | 14 | |
12 | 15 | // use morgan logger |
@@ -15,6 +18,21 @@ app.use(morgan('combined')); | ||
15 | 18 | // use bodyParser for Content-Disposition: form-data |
16 | 19 | //app.use(bodyParser.urlencoded({ extended: false })); |
17 | 20 | //app.use(bodyParser.json()); |
21 | +app.use(bodyParser.text({type: ['application/json',]})); | |
22 | + | |
23 | + | |
24 | +// grpc settings | |
25 | +const PROTO_PATH = __dirname + '/../protos/dockrun.proto'; | |
26 | +const packageDefinition = protoLoader.loadSync( | |
27 | + PROTO_PATH, | |
28 | + {keepCase: true, | |
29 | + longs: String, | |
30 | + enums: String, | |
31 | + defaults: true, | |
32 | + oneofs: true | |
33 | + }); | |
34 | +const dockrun_proto = grpc.loadPackageDefinition(packageDefinition).dockrun; | |
35 | + | |
18 | 36 | |
19 | 37 | function _sendJson(res, statusCode, data) { |
20 | 38 | const json = JSON.stringify(data); |
@@ -25,9 +43,19 @@ function _sendJson(res, statusCode, data) { | ||
25 | 43 | res.end(json); |
26 | 44 | }; |
27 | 45 | |
28 | -function _run_task(payload, task) { | |
29 | - console.log(payload); | |
30 | - return true; | |
46 | +function _run_task(params, cb) { | |
47 | + console.log(params); | |
48 | + const client = new dockrun_proto.DockRun('localhost:1234', | |
49 | + grpc.credentials.createInsecure()); | |
50 | + const req = { | |
51 | + task_name: params.task, | |
52 | + client_name: "web", | |
53 | + use_agent: params.userAgent, | |
54 | + parameter: params.requestBody, | |
55 | + paramter_type: params.requestType, | |
56 | + }; | |
57 | + client.RunTask(req, cb); | |
58 | + return; | |
31 | 59 | }; |
32 | 60 | |
33 | 61 | // public routes - webhook receiver |
@@ -61,7 +89,7 @@ app.use('/pub', (req, res, next) => { | ||
61 | 89 | |
62 | 90 | // check header |
63 | 91 | const contentType = req.headers['content-type']; |
64 | - if (contentType.match(/^multipart\/form-data;/)) { | |
92 | + if (contentType.match(/^multipart\/form-data;?/)) { | |
65 | 93 | // parse and extract multipart/form-data |
66 | 94 | const body = {}; |
67 | 95 | bb = new Busboy({ headers: req.headers }); |
@@ -69,16 +97,35 @@ app.use('/pub', (req, res, next) => { | ||
69 | 97 | body[fieldname] = val; |
70 | 98 | }); |
71 | 99 | bb.on('finish', () => { |
72 | - const result = _run_task(body, task); | |
73 | - if (result) { | |
74 | - _sendJson(res, 200, { result: { message: "ok" } }); | |
75 | - } else { | |
76 | - _sendJson(res, 400, { error: { code: 500, message: "execute error" } }); | |
77 | - } | |
100 | + _run_task({ requestBody: body, | |
101 | + requestType: "multipart/form-data", | |
102 | + task: task, | |
103 | + userAgent: req.headers['User-Agent'], | |
104 | + }, (err, resp) => { | |
105 | + if (err) { | |
106 | + _sendJson(res, 400, { result: err }); | |
107 | + return; | |
108 | + } | |
109 | + _sendJson(res, 200, { result: resp }); | |
110 | + }); | |
78 | 111 | }); |
79 | 112 | req.pipe(bb); |
80 | 113 | return; |
81 | 114 | } |
115 | + if (contentType.match(/application\/json;?/)) { | |
116 | + _run_task({ requestBody: {"0": req.body}, | |
117 | + requestType: "aplication/json", | |
118 | + task: task, | |
119 | + userAgent: req.headers['User-Agent'], | |
120 | + }, (err, resp) => { | |
121 | + if (err) { | |
122 | + _sendJson(res, 400, { result: err }); | |
123 | + return; | |
124 | + } | |
125 | + _sendJson(res, 200, { result: resp }); | |
126 | + }); | |
127 | + return; | |
128 | + } | |
82 | 129 | |
83 | 130 | _sendJson(res, 400, { error: { code: 500, message: "invalid body" } }); |
84 | 131 | return; |
@@ -9,9 +9,11 @@ | ||
9 | 9 | "author": "hylom <hylom@osdn.me>", |
10 | 10 | "license": "ISC", |
11 | 11 | "dependencies": { |
12 | + "@grpc/proto-loader": "^0.5.1", | |
12 | 13 | "body-parser": "^1.19.0", |
13 | 14 | "busboy": "^0.3.1", |
14 | 15 | "connect": "^3.7.0", |
16 | + "grpc": "^1.21.1", | |
15 | 17 | "morgan": "^1.9.1" |
16 | 18 | } |
17 | 19 | } |
@@ -8,6 +8,10 @@ service DockRun { | ||
8 | 8 | |
9 | 9 | message RunTaskRequest { |
10 | 10 | string task_name = 1; |
11 | + string client_name = 2; | |
12 | + string user_agent = 3; | |
13 | + map<string, string> parameters = 4; | |
14 | + string parameter_type = 5; | |
11 | 15 | } |
12 | 16 | |
13 | 17 | message RunTaskReply { |