-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
47 lines (40 loc) · 1.67 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
/* Self-Validating SSL GunDB */
/* using LetsEncrypt Certs */
var pem_email = process.env.PEM_EMAIL
var pem_domain = process.env.PEM_DOMAIN
var debug = process.env.PRODUCTION ? false : true;
if (!pem_email||!pem_domain) { console.log('Missing key parameters! Exiting...'); process.exit(0); }
if (debug) console.log('Staging Mode! No real certificated issued!');
var createServer = require("auto-sni");
const Gun = require("gun");
var app = function(req, res){
if(Gun.serve(req, res)){ return; } // filters gun requests!
require('fs').createReadStream(require('path').join(__dirname, req.url)).on('error',function(){ // static files!
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(require('fs')
.readFileSync(require('path')
.join(__dirname, 'index.html') // or default to index
));
}).pipe(res); // stream
};
var server = createServer({
email: pem_email, // Emailed when certificates expire.
agreeTos: true, // Required for letsencrypt.
debug: debug, // Add console messages and uses staging LetsEncrypt server. (Disable in production)
domains: [ pem_domain ], // List of accepted domain names. (You can use nested arrays to register bundles with LE).
dir: "/etc/letsencrypt", // Directory for storing certificates. Defaults to "~/letsencrypt/etc" if not present.
ports: {
http: 80, // Optionally override the default http port.
https: 443 // // Optionally override the default https port.
}
}, app);
// Server is a "https.createServer" instance.
server.once("listening", ()=> {
var gun = Gun({
file: 'data.json',
web: server
});
// Sync everything
gun.on('out', {get: {'#': {'*': ''}}});
console.log("Gun is ready to go.");
});