forked from blynkkk/blynk-sketch-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
124 lines (94 loc) · 3.01 KB
/
server.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'use strict';
//http://localhost:8080/generate?b=Arduino%20Uno&s=Simple%20Ethernet&e=Widgets/RTC
var host = 'examples.blynk.cc';
var httpPort = 8080;
var httpsPort = 8443;
var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var serveStatic = require('serve-static');
var favicon = require('serve-favicon');
var hljs = require('highlight.js');
var http = require('http');
var https = require('https');
var fs = require('fs');
var sslPath = '/etc/letsencrypt/live/' + host +'/';
var gen = require("./index.js");
var app = express();
var publicPath = path.resolve('./public/');
app.use(favicon(__dirname + '/public/favicon.png'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(serveStatic(publicPath, {'index': ['index.html', 'index.htm']}));
app.get('/listBoards', function(req, res) {
res.send(JSON.stringify(gen.listBoards()));
});
app.get('/listExamples', function(req, res) {
res.send(JSON.stringify(gen.listExamples()));
});
app.get('/getBoardShields', function(req, res) {
var board = req.query.b;
res.send(JSON.stringify(gen.getBoardShields(board)));
});
app.get('/generate', function(req, res) {
var board = req.query.b;
var shield = req.query.s;
var example = req.query.e;
var auth = req.query.a;
var tmpl = req.query.t;
var devname = req.query.n;
res.setHeader('Access-Control-Allow-Origin', '*');
res.send(gen.generate(board, shield, example, auth, tmpl, devname));
});
app.get('/generate.html', function(req, res) {
var board = req.query.b;
var shield = req.query.s;
var example = req.query.e;
var auth = req.query.a;
var tmpl = req.query.t;
var devname = req.query.n;
var code = gen.generate(board, shield, example, auth, tmpl, devname);
var html = hljs.highlight("arduino", code).value;
html =
`
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/styles/default.min.css">
<link href='https://fonts.googleapis.com/css?family=Ubuntu%20Mono' rel='stylesheet'>
<style>
.hljs {
font-family: "Ubuntu Mono", "Lucida Console", Monaco, monospace;
font-size: 14px;
background-color: #FAFAFA;
}
a:link, a:visited, a:active {
color: #23C890;
}
a:hover {
color: blue;
}
</style>
</head>
<body>
<pre><code class="hljs sketch">`
+ html +
` </code></pre>
</body>
</html>
`;
res.setHeader('Access-Control-Allow-Origin', '*');
res.send(html);
});
http.createServer(function(req, res) {
res.writeHead(301, {"Location": "https://" + host + req.url});
res.end();})
.listen(httpPort);
https.createServer({
key: fs.readFileSync(sslPath + 'privkey.pem'),
cert: fs.readFileSync(sslPath + 'fullchain.pem')
}, app)
.listen(httpsPort);
console.log("Server running at", "http://" + host + ":" + httpPort);
console.log("Server running at", "https://" + host + ":" + httpsPort);