-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (78 loc) · 2.11 KB
/
index.js
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
require("make-promises-safe");
require("dotenv").config();
// Require Third-party Dependencies
const polka = require("polka");
const send = require("@polka/send-type");
const bodyParser = require("body-parser");
const { pu } = require("@uim/pu");
const {
graphql,
GraphQLSchema,
GraphQLObjectType,
GraphQLString
} = require("graphql");
// Globals
const port = process.env.port || 1337;
// GraphQL
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: "RootQueryType",
fields: {
hello: {
type: GraphQLString,
resolve() {
return "world";
}
}
}
})
});
// Init nimsoft Probe Utilities
const nimsoft = pu({
login: process.env.uim_user || "adminitrator",
password: process.env.uim_password,
path: process.env.uim_path || "/opt/nimsoft/bin/pu"
});
const server = polka();
server.use(bodyParser.json());
server.get("/", (req, res) => {
send(res, 200, { uptime: process.uptime() });
});
server.post("/", async (req, res) => {
const query = req.body.query;
if (typeof query !== "string") {
send(res, 400, "body.query must be a string containing a GraphQL Query");
}
try {
const result = await graphql(schema, query);
const isError = Array.isArray(result.errors);
send(res, isError ? 500 : 200, result);
}
catch (err) {
console.error(err);
send(res, 500, err.message);
}
});
server.post("/pu", async (req, res) => {
const { path, args = [] } = req.body;
if (typeof path !== "string") {
send(res, 400, "body.path must be a string");
}
if (!Array.isArray(args)) {
send(res, 400, "body.args must be an Array of arguments (or undefined)");
}
try {
const result = await nimsoft(path, args);
send(res, 200, result);
}
catch (err) {
console.error(err);
send(res, 500, err.message);
}
});
server.listen(port, (err) => {
if (err) {
throw err;
}
console.log(`Http Server is listening on port <${port}>`);
});