forked from Siteglide/siteglide-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsiteglide-cli-server.js
More file actions
executable file
·85 lines (73 loc) · 2.33 KB
/
siteglide-cli-server.js
File metadata and controls
executable file
·85 lines (73 loc) · 2.33 KB
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
#!/usr/bin/env node
const express = require('express'),
compression = require('compression'),
bodyParser = require('body-parser'),
Gateway = require('./lib/proxy'),
logger = require('./lib/logger'),
path = require('path');
const start = (env,command) => {
const port = env.PORT || 3333;
const app = express();
const gateway = new Gateway({
url: env.SITEGLIDE_URL,
token: env.SITEGLIDE_TOKEN,
email: env.SITEGLIDE_EMAIL
});
const graphqlRouting = (req, res) => {
gateway
.graph(req.body)
.then(body => res.send(body))
.catch(error => res.send(error));
};
var liquidRouting;
if(command==='gui'){
liquidRouting = (req, res) => {
gateway
.liquid(req.body)
.then(body => res.send(body))
.catch(error => res.send(error));
};
}
app.use(bodyParser.json());
app.use(compression());
app.use('/gui/graphql', express.static(path.resolve(__dirname, 'gui', 'graphql', 'public')));
if(command==='gui'){
app.use('/gui/liquid', express.static(path.resolve(__dirname, 'gui', 'liquid', 'public')));
}
// INFO
const info = (req, res) => {
return res.send(JSON.stringify({ SG_URL: env.SITEGLIDE_URL }));
};
app.get('/info', info);
app.post('/graphql', graphqlRouting);
app.post('/api/graph', graphqlRouting);
if(command==='gui'){
app.post('/api/liquid', liquidRouting);
app.get('/api/liquid', liquidRouting);
}
gateway.ping().then(async () => {
app.listen(port, function() {
logger.Debug(`Server is listening on ${port}`);
logger.Success(`Connected to ${env.SITEGLIDE_URL}`);
logger.Success(`GraphiQL Editor: http://localhost:${port}/gui/graphql`);
if(command==='gui'){
logger.Success(`Liquid Evaluator: http://localhost:${port}/gui/liquid`);
}else{
logger.Warn('The graphql command is now deprecated and will be removed in a future update. Please switch to the new gui command to use the GraphiQL Editor and Liquid Evaluator.')
}
})
.on('error', err => {
if (err.errno === 'EADDRINUSE') {
logger.Error(`Port ${port} is already in use.`, { exit: false });
logger.Print('\n');
logger.Warn('Please use -p <port> to run server on a different port.\n');
logger.Warn('Example: siteglide-cli graphql <env> -p 31337');
} else {
logger.Error(`Something wrong happened when trying to run Express server: ${err}`);
}
});
})
};
module.exports = {
start: start
};