Skip to content

Commit 4bb962d

Browse files
committed
Initial Commit
1 parent e56727c commit 4bb962d

File tree

4 files changed

+2428
-0
lines changed

4 files changed

+2428
-0
lines changed

.gitignore

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
9+
# Diagnostic reports (https://nodejs.org/api/report.html)
10+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11+
12+
# Runtime data
13+
pids
14+
*.pid
15+
*.seed
16+
*.pid.lock
17+
18+
# Directory for instrumented libs generated by jscoverage/JSCover
19+
lib-cov
20+
21+
# Coverage directory used by tools like istanbul
22+
coverage
23+
*.lcov
24+
25+
# nyc test coverage
26+
.nyc_output
27+
28+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29+
.grunt
30+
31+
# Bower dependency directory (https://bower.io/)
32+
bower_components
33+
34+
# node-waf configuration
35+
.lock-wscript
36+
37+
# Compiled binary addons (https://nodejs.org/api/addons.html)
38+
build/Release
39+
40+
# Dependency directories
41+
node_modules/
42+
jspm_packages/
43+
44+
# Snowpack dependency directory (https://snowpack.dev/)
45+
web_modules/
46+
47+
# TypeScript cache
48+
*.tsbuildinfo
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Microbundle cache
57+
.rpt2_cache/
58+
.rts2_cache_cjs/
59+
.rts2_cache_es/
60+
.rts2_cache_umd/
61+
62+
# Optional REPL history
63+
.node_repl_history
64+
65+
# Output of 'npm pack'
66+
*.tgz
67+
68+
# Yarn Integrity file
69+
.yarn-integrity
70+
71+
# dotenv environment variables file
72+
.env
73+
.env.test
74+
75+
# parcel-bundler cache (https://parceljs.org/)
76+
.cache
77+
78+
# Next.js build output
79+
.next
80+
81+
# Nuxt.js build / generate output
82+
.nuxt
83+
dist
84+
85+
# Gatsby files
86+
.cache/
87+
# Comment in the public line in if your project uses Gatsby and not Next.js
88+
# https://nextjs.org/blog/next-9-1#public-directory-support
89+
# public
90+
91+
# vuepress build output
92+
.vuepress/dist
93+
94+
# Serverless directories
95+
.serverless/
96+
97+
# FuseBox cache
98+
.fusebox/
99+
100+
# DynamoDB Local files
101+
.dynamodb/
102+
103+
# TernJS port file
104+
.tern-port
105+
106+
# Stores VSCode versions used for testing VSCode extensions
107+
.vscode-test

index.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
const amqplib = require("amqplib");
2+
3+
require("dotenv").config();
4+
5+
let channel = null;
6+
7+
const QUEUE = "maxfield-task";
8+
9+
const express = require("express");
10+
const bodyParser = require("body-parser");
11+
const fileUpload = require("express-fileupload");
12+
13+
const app = express();
14+
15+
const { check, validationResult } = require("express-validator");
16+
17+
app.all("*", function(req, res, next) {
18+
res.header("Access-Control-Allow-Origin", process.env.ORIGIN);
19+
res.header("Access-Control-Allow-Credentials", "true");
20+
res.header(
21+
"Access-Control-Allow-Headers",
22+
"Content-Type, Content-Length, Authorization, Accept, X-Requested-With"
23+
);
24+
res.header("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS");
25+
if (req.method == "OPTIONS") {
26+
res.send(200);
27+
} else {
28+
next();
29+
}
30+
});
31+
32+
app.use(bodyParser());
33+
app.use(fileUpload());
34+
35+
app.get("/", (req, res) => res.send("It works!"));
36+
37+
app.get("/queue", [], (req, res) => {
38+
channel.assertQueue(QUEUE).then(info => {
39+
res.send({
40+
error: false,
41+
inqueue: info.messageCount,
42+
worker: info.consumerCount
43+
});
44+
});
45+
});
46+
47+
app.post("/submit", [], (req, res) => {
48+
const errors = validationResult(req);
49+
if (!errors.isEmpty()) {
50+
return res.status(422).json({ error: true, errors: errors.array() });
51+
}
52+
53+
var id = randomid();
54+
channel.assertQueue(QUEUE).then(info => {
55+
channel.sendToQueue(
56+
QUEUE,
57+
Buffer.from(
58+
JSON.stringify({
59+
agents: req.body.agents,
60+
googlemap: req.body.googlemap,
61+
portal: req.body.portals,
62+
faction: req.body.faction
63+
})
64+
),
65+
{
66+
correlationId: id.uuid,
67+
replyTo: "amq.rabbitmq.reply-to"
68+
}
69+
);
70+
res.send({ error: false, submitid: id.taskid, inqueue: info.messageCount });
71+
});
72+
});
73+
74+
function init() {
75+
return require("amqplib")
76+
.connect(process.env.AMQPURL)
77+
.then(conn => conn.createChannel())
78+
.then(ch => {
79+
channel = ch;
80+
ch.consume(
81+
"amq.rabbitmq.reply-to",
82+
msg => eventEmitter.emit(msg.properties.correlationId, msg.content),
83+
{ noAck: true }
84+
);
85+
});
86+
}
87+
88+
function randomid() {
89+
var ret1 = new Date().getTime();
90+
var ret2 = Math.random();
91+
var ret3 = Math.random();
92+
return {
93+
uuid: ret1.toString() + ret2.toString() + ret3.toString(),
94+
taskid: ret1.toString(16) + ret2.toString(16) + ret3.toString(16)
95+
};
96+
}
97+
98+
init()
99+
.then(() =>
100+
app.listen(process.env.PORT, () =>
101+
console.log("MaxField API running on port " + process.env.PORT + " !")
102+
)
103+
)
104+
.catch(err => console.error(err));

package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "maxfield-api",
3+
"version": "1.0.0",
4+
"description": "MaxField NIA-CN API",
5+
"main": "index.js",
6+
"author": "Yukari Chiba",
7+
"license": "GPLv3",
8+
"dependencies": {
9+
"amqplib": "^0.5.5",
10+
"body-parser": "^1.19.0",
11+
"dotenv": "^8.2.0",
12+
"express": "^4.17.1",
13+
"express-fileupload": "^1.1.6",
14+
"express-validator": "^6.4.0",
15+
"imagemin": "^7.0.1",
16+
"imagemin-pngquant": "^8.0.0"
17+
}
18+
}

0 commit comments

Comments
 (0)