Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
382 changed files
with
93,200 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
var express = require('express'); | ||
var http = require('http'); | ||
var path = require('path'); | ||
var config = require('config'); | ||
var log = require('lib/log')(module); | ||
var mongoose = require('lib/mongoose'); | ||
var HttpError = require('error').HttpError; | ||
|
||
var app = express(); | ||
|
||
app.engine('ejs', require('ejs-locals')); | ||
app.set('views', __dirname + '/template'); | ||
app.set('view engine', 'ejs'); | ||
|
||
app.use(express.favicon()); | ||
|
||
if (app.get('env') == 'development') { | ||
app.use(express.logger('dev')); | ||
} else { | ||
app.use(express.logger('default')); | ||
} | ||
|
||
app.use(express.bodyParser()); | ||
|
||
app.use(express.cookieParser()); | ||
|
||
var sessionStore = require('lib/sessionStore'); | ||
|
||
app.use(express.session({ | ||
secret: config.get('session:secret'), | ||
key: config.get('session:key'), | ||
cookie: config.get('session:cookie'), | ||
store: sessionStore | ||
})); | ||
|
||
app.use(require('middleware/sendHttpError')); | ||
app.use(require('middleware/loadUser')); | ||
|
||
app.use(app.router); | ||
|
||
require('routes')(app); | ||
|
||
app.use(express.static(path.join(__dirname, 'public'))); | ||
|
||
|
||
app.use(function(err, req, res, next) { | ||
if (typeof err == 'number') { | ||
err = new HttpError(err); | ||
} | ||
|
||
if (err instanceof HttpError) { | ||
res.sendHttpError(err); | ||
} else { | ||
if (app.get('env') == 'development') { | ||
express.errorHandler()(err, req, res, next); | ||
} else { | ||
log.error(err); | ||
err = new HttpError(500); | ||
res.sendHttpError(err); | ||
} | ||
} | ||
}); | ||
|
||
|
||
var server = http.createServer(app); | ||
server.listen(config.get('port'), function(){ | ||
log.info('Express server listening on port ' + config.get('port')); | ||
}); | ||
|
||
var io = require('./socket')(server); | ||
app.set('io', io); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"port": 3000, | ||
"mongoose": { | ||
"uri": "mongodb://localhost/chat", | ||
"options": { | ||
"server": { | ||
"socketOptions": { | ||
"keepAlive": 1 | ||
} | ||
} | ||
} | ||
}, | ||
"session": { | ||
"secret": "KillerIsJim", | ||
"key": "sid", | ||
"cookie": { | ||
"path": "/", | ||
"httpOnly": true, | ||
"maxAge": null | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var nconf = require('nconf'); | ||
var path = require('path'); | ||
|
||
nconf.argv() | ||
.env() | ||
.file({ file: path.join(__dirname, 'config.json') }); | ||
|
||
module.exports = nconf; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
var mongoose = require('lib/mongoose'); | ||
var async = require('async'); | ||
|
||
async.series([ | ||
open, | ||
dropDatabase, | ||
requireModels, | ||
createUsers | ||
], function(err) { | ||
console.log(arguments); | ||
mongoose.disconnect(); | ||
process.exit(err ? 255 : 0); | ||
}); | ||
|
||
function open(callback) { | ||
mongoose.connection.on('open', callback); | ||
} | ||
|
||
function dropDatabase(callback) { | ||
var db = mongoose.connection.db; | ||
db.dropDatabase(callback); | ||
} | ||
|
||
function requireModels(callback) { | ||
require('models/user'); | ||
|
||
async.each(Object.keys(mongoose.models), function(modelName, callback) { | ||
mongoose.models[modelName].ensureIndexes(callback); | ||
}, callback); | ||
} | ||
|
||
function createUsers(callback) { | ||
|
||
var users = [ | ||
{username: 'Вася', password: 'supervasya'}, | ||
{username: 'Петя', password: '123'}, | ||
{username: 'admin', password: 'thetruehero'} | ||
]; | ||
|
||
async.each(users, function(userData, callback) { | ||
var user = new mongoose.models.User(userData); | ||
user.save(callback); | ||
}, callback); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
var path = require('path'); | ||
var util = require('util'); | ||
var http = require('http'); | ||
|
||
// ошибки для выдачи посетителю | ||
function HttpError(status, message) { | ||
Error.apply(this, arguments); | ||
Error.captureStackTrace(this, HttpError); | ||
|
||
this.status = status; | ||
this.message = message || http.STATUS_CODES[status] || "Error"; | ||
} | ||
|
||
util.inherits(HttpError, Error); | ||
|
||
HttpError.prototype.name = 'HttpError'; | ||
|
||
exports.HttpError = HttpError; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
var winston = require('winston'); | ||
var ENV = process.env.NODE_ENV; | ||
|
||
// can be much more flexible than that O_o | ||
function getLogger(module) { | ||
|
||
var path = module.filename.split('/').slice(-2).join('/'); | ||
|
||
return new winston.Logger({ | ||
transports: [ | ||
new winston.transports.Console({ | ||
colorize: true, | ||
level: (ENV == 'development') ? 'debug' : 'error', | ||
label: path | ||
}) | ||
] | ||
}); | ||
} | ||
|
||
module.exports = getLogger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
var mongoose = require('mongoose'); | ||
var config = require('config'); | ||
|
||
mongoose.connect(config.get('mongoose:uri'), config.get('mongoose:options')); | ||
|
||
module.exports = mongoose; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
var mongoose = require('mongoose'); | ||
var express = require('express'); | ||
var MongoStore = require('connect-mongo')(express); | ||
|
||
var sessionStore = new MongoStore({mongoose_connection: mongoose.connection}); | ||
|
||
module.exports = sessionStore; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
var HttpError = require('error').HttpError; | ||
|
||
module.exports = function(req, res, next) { | ||
if (!req.session.user) { | ||
return next(new HttpError(401, "Вы не авторизованы")); | ||
} | ||
|
||
next(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var User = require('models/user').User; | ||
|
||
module.exports = function(req, res, next) { | ||
req.user = res.locals.user = null; | ||
|
||
if (!req.session.user) return next(); | ||
|
||
User.findById(req.session.user, function(err, user) { | ||
if (err) return next(err); | ||
|
||
req.user = res.locals.user = user; | ||
next(); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module.exports = function(req, res, next) { | ||
|
||
res.sendHttpError = function(error) { | ||
|
||
res.status(error.status); | ||
if (res.req.headers['x-requested-with'] == 'XMLHttpRequest') { | ||
res.json(error); | ||
} else { | ||
res.render("error", {error: error}); | ||
} | ||
}; | ||
|
||
next(); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
var crypto = require('crypto'); | ||
var async = require('async'); | ||
var util = require('util'); | ||
|
||
var mongoose = require('lib/mongoose'), | ||
Schema = mongoose.Schema; | ||
|
||
var schema = new Schema({ | ||
username: { | ||
type: String, | ||
unique: true, | ||
required: true | ||
}, | ||
hashedPassword: { | ||
type: String, | ||
required: true | ||
}, | ||
salt: { | ||
type: String, | ||
required: true | ||
}, | ||
created: { | ||
type: Date, | ||
default: Date.now | ||
} | ||
}); | ||
|
||
schema.methods.encryptPassword = function(password) { | ||
return crypto.createHmac('sha1', this.salt).update(password).digest('hex'); | ||
}; | ||
|
||
schema.virtual('password') | ||
.set(function(password) { | ||
this._plainPassword = password; | ||
this.salt = Math.random() + ''; | ||
this.hashedPassword = this.encryptPassword(password); | ||
}) | ||
.get(function() { return this._plainPassword; }); | ||
|
||
|
||
schema.methods.checkPassword = function(password) { | ||
return this.encryptPassword(password) === this.hashedPassword; | ||
}; | ||
|
||
schema.statics.authorize = function(username, password, callback) { | ||
var User = this; | ||
|
||
async.waterfall([ | ||
function(callback) { | ||
User.findOne({username: username}, callback); | ||
}, | ||
function(user, callback) { | ||
if (user) { | ||
if (user.checkPassword(password)) { | ||
callback(null, user); | ||
} else { | ||
callback(new AuthError("Пароль неверен")); | ||
} | ||
} else { | ||
var user = new User({username: username, password: password}); | ||
user.save(function(err) { | ||
if (err) return callback(err); | ||
callback(null, user); | ||
}); | ||
} | ||
} | ||
], callback); | ||
}; | ||
|
||
exports.User = mongoose.model('User', schema); | ||
|
||
|
||
function AuthError(message) { | ||
Error.apply(this, arguments); | ||
Error.captureStackTrace(this, AuthError); | ||
|
||
this.message = message; | ||
} | ||
|
||
util.inherits(AuthError, Error); | ||
|
||
AuthError.prototype.name = 'AuthError'; | ||
|
||
exports.AuthError = AuthError; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "chat", | ||
"version": "0.0.1", | ||
"private": true, | ||
"scripts": { | ||
"start": "node app.js" | ||
}, | ||
"dependencies": { | ||
"express": "3.3.8", | ||
"ejs": "*" | ||
} | ||
} |
Empty file.
Oops, something went wrong.