-
Notifications
You must be signed in to change notification settings - Fork 0
/
aispace.coffee
178 lines (126 loc) · 4.09 KB
/
aispace.coffee
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'use strict'
http = require 'http'
express = require 'express'
path = require 'path'
favicon = require 'serve-favicon'
logger = require 'morgan'
cookieParser = require 'cookie-parser'
bodyParser = require 'body-parser'
session = require 'express-session'
flash = require 'express-flash'
multiparty = require 'multiparty' #F7
errorlog = require 'express-errorlog'
nconf = require "nconf"
nconf.argv().env().file {
file: "config.json"
}
app = do express
app.config = nconf
app.db = require( "redis" ).createClient()
RedisStore = require( "connect-redis" )(session)
# view engine setup
app.set 'views', (path.join __dirname, 'views')
app.set 'view engine', 'jade'
# uncomment after placing your favicon in /public
#app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(require('less-middleware')(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
app.use(session {
secret: app.config.get 'redis:secret'
key: 'aid'
resave: no
saveUninitialized: no
store: new RedisStore {
host: 'localhost'
port: 6379
client: app.db
}
});
app.use flash()
models = require './models'
models.init(app)
app.use (req, res, next) ->
req.db = app.db
req.config = app.config
req.models = app.models
req.user = { # fixme login
id: 1
name: "Дмитрий Сидоров"
email: "[email protected]"
}
req.log = (data) ->
console.log "ADD LOG MESSAGE", data
app.models.Log.create data
res.error = (message) ->
next new Error(message)
if req.method == "POST"
#console.log "POST BODY", req.method, req.body
# Привет F7!
form = new multiparty.Form()
form.parse req, (err, fields, files) ->
#console.log "form fields", fields
#console.log "form files", files
for key, val of fields
if val && val.length==1
req.body[key] = val[0]
else
req.body[key] = val
req.files = files
do next
else
do next
####(require "./auth.coffee").init app
#app.use(auth.passport.initialize());
#app.use(auth.passport.session());
#app.use('/shop', require './routes/shop_route.coffee');
#app.use('/user', require './routes/bak/user_route.coffee');
##app.use('/user', require './routes/users.coffee');
#app.use('/shops', require './routes/shops.coffee');
app.use('/controller', require './routes/controllers.coffee');
app.use('/stand', require './routes/stands.coffee');
app.use('/cell', require './routes/cells.coffee');
app.use('/db', require './routes/db.coffee');
app.use('/', require './routes/index.coffee');
# catch 404 and forward to error handler
app.use (req, res, next) ->
err = new Error('Not Found');
err.status = 404;
next err
app.use errorlog
app.use (err, req, res, next) ->
res.json err
# Create HTTP server.
server = http.createServer app
# Listen on provided port, on all network interfaces.
server.listen (app.config.get "express:port"), app.config.get("express:address")
server.on 'listening', ->
address = do server.address
console.log "Listening on ", address.address, " port ", address.port
mqttServer = require "./mqtt_broker.coffee"
#mqttServer.
console.log("mosca", require.resolve("mosca"));
#console.log("Models: ", app.models)
#require './test.coffee'
# Event listener for HTTP server "error" event.
server.on 'error', (error) ->
throw error if error.syscall != 'listen'
bind = if typeof port == 'string' then 'Pipe ' + port else 'Port ' + port
# handle specific listen errors with friendly messages
switch error.code
when 'EACCES'
console.error bind + ' requires elevated privileges'
process.exit 1
when 'EADDRINUSE'
console.error bind + ' is already in use'
process.exit 1
else
throw error
zeroify = (val) ->
val = "0" + val if val < 10
val
Date.prototype.prettyDateTime = ->
zeroify(@getDate()) + "." + zeroify((@getMonth()+1)) + "." + (@getFullYear()) + " " + zeroify(@getHours()) + ":" + zeroify(@getMinutes()) + ":" + zeroify(@getSeconds())