This repository was archived by the owner on Mar 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.js
149 lines (121 loc) · 4.19 KB
/
app.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'use strict'
GLOBAL.app = require('express')()
var compression = require('compression')
var exphbs = require('express-handlebars')
var express = require('express')
var favicon = require('serve-favicon')
var Io = require('socket.io')
var http = require('http')
var loki = require('lokijs')
var moment = require('moment')
var common = require('./lib/common')
var Events = require('./lib/events')
var server = http.Server(app)
var settings = require('./settings.json')
var io = Io(server)
/**
* Rebuild the canvas from Loki.
*/
function getCanvas() {
var _canvas = fabric.createCanvasForNode(1920, 1080)
var collection = app.db.addCollection('paths', {indices: ['uuid']})
var fabricObjects = collection.find({day: parseInt(moment().format('e'), 10)}).reverse()
if(fabricObjects) {
fabricObjects.forEach(function(obj) {
// Revive the objects property for a group.
if(obj.type === 'group') {
obj.objects = obj.__objects
}
})
}
fabric.util.enlivenObjects(fabricObjects, function(objects) {
objects.forEach(function(o) {
// Please note that a deserialize custom brush(stroke property) is
// an anonymous function coming from the client. Potential recipe
// for security issues(!).
_canvas.add(o)
})
})
return _canvas
}
// Reload the page when the next day occurs.
setInterval(function() {
var currentDay = parseInt(moment().format('e'), 10)
if(currentDay !== app.dayOftheWeek) {
app.canvas = getCanvas()
}
}, 3000)
io.on('connection', function(socket) {new Events(socket)})
app.use(express.static('public'))
app.use(favicon(__dirname + '/public/img/dolphin.png'))
app.use(compression())
app.get('/', function(req, res) {
var serializedCanvas = JSON.parse(JSON.stringify(app.canvas))
serializedCanvas.objects.forEach(function(rawObject, i) {
if(rawObject.type === 'group') {
// Somehow this is set to black during group serialization...
delete serializedCanvas.objects[i].fill
}
})
res.render('home', {canvasState: JSON.stringify(serializedCanvas)})
})
/**
* Present the droodle data as a javascript file that can be
* retrieved as GZIP script.
*/
app.get('/js/state.js', function(req, res) {
var serializedCanvas = JSON.parse(JSON.stringify(app.canvas))
serializedCanvas.objects.forEach(function(obj, i) {
if(obj.type === 'group') {
// Somehow this is set to black during group serialization...
delete serializedCanvas.objects[i].fill
delete serializedCanvas.objects[i].stroke
}
})
res.send('window.initialState = ' + JSON.stringify(serializedCanvas) + ';')
})
/**
* Retrieve the current DOTD droodle as a PNG.
*/
app.get('/dotd.png', function(req, res) {
res.writeHead(200, { 'Content-Type': 'image/png' })
var stream = canvas.createPNGStream()
stream.on('data', function(chunk) {res.write(chunk)})
stream.on('end', function() {res.end()})
})
/**
* Retrieve the current DOTD droodle as a SVG.
*/
app.get('/dotd.svg', function(req, res) {
res.send(canvas.toSVG())
})
/**
* Retrieve the current DOTD droodle as a serialized Fabric.js canvas.
*/
app.get('/dotd.json', function(req, res) {
res.send(JSON.parse(JSON.stringify(app.canvas)))
})
server.listen(settings.port, function() {
moment.locale(settings.language)
app.engine('handlebars', exphbs({defaultLayout: 'main'}))
app.set('view engine', 'handlebars')
app.dayOftheWeek = parseInt(moment().format('e'), 10)
app.fabric = require('fabric').fabric
GLOBAL.fabric = app.fabric
common.init(app)
app.db = new loki('db.json', {
autosave: true,
autosaveInterval: 3000,
autoload: true,
autoloadCallback: function() {
// if database did not exist it will be empty so I will intitialize here
var collection = app.db.getCollection('paths')
if (!collection) {
collection = app.db.addCollection('paths', {indices: ['uuid']})
collection.ensureUniqueIndex('uuid')
}
app.canvas = getCanvas()
},
env: 'NODEJS',
})
})