-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
45 lines (39 loc) · 2 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
var port = Number(process.env.PORT || 8080)
var global_count = 0
var parseurl = require('parseurl')
var logger = require('morgan')
var express = require('express')
var app = express()
var fs = require('fs')
var path = require('path')
var showdown = require('showdown')
app.use(logger())
app.get('/', function(req, res) {
const converter = new showdown.Converter();
const markdown = String(fs.readFileSync(path.join(__dirname, './README.md')));
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(converter.makeHtml(markdown).replace('', ` This instance has been used for ${global_count} times since last reboot.`));
})
app.get('/*', function(req, res) {
let args = parseurl(req).pathname.replace('/', '').split('+')
let size = args[0].split('x')
let width = size[0]
let height = size[1]
let font_size = Math.round(Math.max(12, Math.min(Math.min(width, height) * 0.75, 0.75 * Math.max(width, height) / 12)))
let rectStyle = ''
let elements = ''
if (args.length > 1) {
if (args.indexOf('border') > 0) {
rectStyle += 'stroke-width: 3px; stroke: rgba(0,0,0,.1);'
}
if (args.indexOf('cross') > 0) {
elements += `<line xmlns="http://www.w3.org/2000/svg" x1="0" y1="0" x2="${width}" y2="${height}" stroke-width="1" stroke="#ddd" /><line xmlns="http://www.w3.org/2000/svg" x1="${width}" y1="0" x2="0" y2="${height}" stroke-width="1" stroke="#ddd" />`
}
}
res.writeHead(200, {'Content-Type': 'image/svg+xml'})
res.end(`<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" preserveAspectRatio="none"><rect width="${width}" height="${height}" style="${rectStyle}" fill="#eee"/>${elements}<text text-anchor="middle" x="${width/2}" y="${height/2}" style="fill:#aaa;font-weight:bold;font-size:${font_size}px;font-family:Arial,Helvetica,sans-serif;dominant-baseline:central">${width}x${height}</text></svg>`)
global_count ++
})
var server = app.listen(port, function() {
console.log('Listening on port %d', server.address().port)
})