• R/O
  • HTTP
  • SSH
  • HTTPS

提交

標籤
無標籤

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

docker based ci tool


Commit MetaInfo

修訂b779c83cfac44098eae93c487aa4db2d48a5b98d (tree)
時間2019-06-06 01:17:59
作者hylom <hylom@user...>
Commiterhylom

Log Message

frontend: implement task launcher

Change Summary

差異

--- a/frontend/config.js
+++ b/frontend/config.js
@@ -4,6 +4,9 @@ exports.config = {
44 newslash: {
55 token: "ASEgbfsREAsea",
66 },
7+ test_task: {
8+ token: "test_token",
9+ },
710 },
811 };
912
--- a/frontend/index.js
+++ b/frontend/index.js
@@ -7,6 +7,9 @@ const config = require('./config').config;
77 const bodyParser = require('body-parser');
88 const Busboy = require('busboy');
99
10+const grpc = require('grpc');
11+const protoLoader = require('@grpc/proto-loader');
12+
1013 const app = connect();
1114
1215 // use morgan logger
@@ -15,6 +18,21 @@ app.use(morgan('combined'));
1518 // use bodyParser for Content-Disposition: form-data
1619 //app.use(bodyParser.urlencoded({ extended: false }));
1720 //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+
1836
1937 function _sendJson(res, statusCode, data) {
2038 const json = JSON.stringify(data);
@@ -25,9 +43,19 @@ function _sendJson(res, statusCode, data) {
2543 res.end(json);
2644 };
2745
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;
3159 };
3260
3361 // public routes - webhook receiver
@@ -61,7 +89,7 @@ app.use('/pub', (req, res, next) => {
6189
6290 // check header
6391 const contentType = req.headers['content-type'];
64- if (contentType.match(/^multipart\/form-data;/)) {
92+ if (contentType.match(/^multipart\/form-data;?/)) {
6593 // parse and extract multipart/form-data
6694 const body = {};
6795 bb = new Busboy({ headers: req.headers });
@@ -69,16 +97,35 @@ app.use('/pub', (req, res, next) => {
6997 body[fieldname] = val;
7098 });
7199 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+ });
78111 });
79112 req.pipe(bb);
80113 return;
81114 }
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+ }
82129
83130 _sendJson(res, 400, { error: { code: 500, message: "invalid body" } });
84131 return;
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -9,9 +9,11 @@
99 "author": "hylom <hylom@osdn.me>",
1010 "license": "ISC",
1111 "dependencies": {
12+ "@grpc/proto-loader": "^0.5.1",
1213 "body-parser": "^1.19.0",
1314 "busboy": "^0.3.1",
1415 "connect": "^3.7.0",
16+ "grpc": "^1.21.1",
1517 "morgan": "^1.9.1"
1618 }
1719 }
--- a/protos/dockrun.proto
+++ b/protos/dockrun.proto
@@ -8,6 +8,10 @@ service DockRun {
88
99 message RunTaskRequest {
1010 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;
1115 }
1216
1317 message RunTaskReply {