• 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

修訂a011f1c95d48cfb8aeebeb2ee7e72908dd5900ef (tree)
時間2019-06-03 23:16:40
作者hylom <hylom@user...>
Commiterhylom

Log Message

add frontend server basic codes

Change Summary

差異

--- /dev/null
+++ b/frontend/config.js
@@ -0,0 +1,9 @@
1+exports.config = {
2+ port: 5678,
3+ tasks: {
4+ newslash: {
5+ token: "ASEgbfsREAsea",
6+ },
7+ },
8+};
9+
--- /dev/null
+++ b/frontend/index.js
@@ -0,0 +1,96 @@
1+const http = require('http');
2+const url = require('url');
3+
4+const connect = require('connect');
5+const morgan = require('morgan');
6+const config = require('./config').config;
7+const bodyParser = require('body-parser');
8+const Busboy = require('busboy');
9+
10+const app = connect();
11+
12+// use morgan logger
13+app.use(morgan('combined'));
14+
15+// use bodyParser for Content-Disposition: form-data
16+//app.use(bodyParser.urlencoded({ extended: false }));
17+//app.use(bodyParser.json());
18+
19+function _sendJson(res, statusCode, data) {
20+ const json = JSON.stringify(data);
21+ res.writeHead(statusCode, {
22+ 'Content-Type': 'application/json',
23+ 'Content-Length': json.length,
24+ });
25+ res.end(json);
26+};
27+
28+function _run_task(payload, task) {
29+ console.log(payload);
30+ return true;
31+};
32+
33+// public routes - webhook receiver
34+app.use('/pub', (req, res, next) => {
35+ if (req.method != 'POST') {
36+ _sendJson(res, 400, { error: { code: 400, message: "bad request" } });
37+ return;
38+ }
39+
40+ const req_url = url.parse(req.url, true);
41+ m = req_url.pathname.match(/^\/([^/]*)\/([^/]*)/);
42+ if (!m) {
43+ next();
44+ return;
45+ }
46+ const task = m[1];
47+ const action = m[2];
48+
49+ const task_config = config.tasks[task];
50+ if (!task_config) {
51+ next();
52+ return;
53+ }
54+
55+ if (action == 'run') {
56+ // check token
57+ if (!task_config.token || task_config.token != req_url.query.token) {
58+ _sendJson(res, 400, { error: { code: 400, message: "invalid token" } });
59+ return;
60+ }
61+
62+ // check header
63+ const contentType = req.headers['content-type'];
64+ if (contentType.match(/^multipart\/form-data;/)) {
65+ // parse and extract multipart/form-data
66+ const body = {};
67+ bb = new Busboy({ headers: req.headers });
68+ bb.on('field', (fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) => {
69+ body[fieldname] = val;
70+ });
71+ 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+ }
78+ });
79+ req.pipe(bb);
80+ return;
81+ }
82+
83+ _sendJson(res, 400, { error: { code: 500, message: "invalid body" } });
84+ return;
85+ }
86+
87+ next();
88+});
89+
90+// 404: Not Found
91+app.use((req, res) => {
92+ _sendJson(res, 404, { error: { code: 404, message: "not found" } });
93+});
94+
95+//create node.js http server and listen on port
96+http.createServer(app).listen(config.port);
--- /dev/null
+++ b/frontend/package.json
@@ -0,0 +1,17 @@
1+{
2+ "name": "dogrun-frontend",
3+ "version": "1.0.0",
4+ "description": "dogrun-ci web frontend",
5+ "main": "index.js",
6+ "scripts": {
7+ "test": "echo \"Error: no test specified\" && exit 1"
8+ },
9+ "author": "hylom <hylom@osdn.me>",
10+ "license": "ISC",
11+ "dependencies": {
12+ "body-parser": "^1.19.0",
13+ "busboy": "^0.3.1",
14+ "connect": "^3.7.0",
15+ "morgan": "^1.9.1"
16+ }
17+}