-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp.js
121 lines (109 loc) · 3.41 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
/**
* Module dependencies.
*/
var express = require('express'),
http = require('http'),
path = require('path'),
github_client = require("github"),
i18n = require("i18n"),
fs = require('fs'),
mongoose = require('mongoose'),
MongoStore = require('connect-mongo')(express);
var github = new github_client({
version: "3.0.0"
});
//localization config
i18n.configure({
locales:['en'],
register: global
});
//Load all models
var models_path = __dirname + '/models';
fs.readdirSync(models_path).forEach(function (file) {
require(models_path + '/' + file);
});
//Loading routes
var indexRoute = require('./routes/index'),
userRoute = require('./routes/user'),
githubRoute = require('./routes/github'),
documentRoute = require('./routes/document'),
repositoryRoute = require('./routes/repository'),
app = express();
//Global variables
app.locals({
__: i18n.__,
__n: i18n.__n,
version: "0.1.0"
});
//set an global variable to hold all SourceDoc configs and use it in all parts of application
GLOBAL.systemConfig = require(__dirname + '/config.js').systemConfigs;
//DB connection
mongoose.connect(systemConfig.mongoDbConnection);
var conf = {
db: {
db: 'sourcedoc',
host: '127.0.0.1',
port: 27017,
collection: 'sessions'
},
secret: '076ee61d63aa10a125ea872411e433b9'
};
app.configure(function() {
//enable reverse proxy
app.enable('trust proxy');
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon(__dirname + '/public/img/favicon.ico'));
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
//Save sessions in DB
app.use(express.session({
secret: conf.secret,
maxAge: new Date(Date.now() + 3600000),
store: new MongoStore(conf.db)
}));
app.use(i18n.init);
app.use(express.static(path.join(__dirname, 'public')));
//User authentication and more
app.use(require('./middlewares/authentication.js').authCheck);
//this middleware is used to pass current URL to the views and
//active correct menu in navigation bar. I don't know if there's
//a better way for doing it
app.use(function(req, res, next) {
res.locals.url = req.url;
next();
});
app.use(app.router);
});
app.configure('development', function() {
app.use(express.errorHandler());
});
//Homepage
app.get('/', indexRoute.index);
//Start Github authentication
app.get('/github_auth', githubRoute.auth);
//Github authentication callback
app.get('/github_auth_callback', githubRoute.authCallback);
//Sync repositories with Github
app.get('/github_sync', userRoute.githubSync);
//Users panel
app.get('/panel', userRoute.panel);
//Users panel with login parameter
app.get('/panel/:login?', userRoute.panel);
//Logout from account
app.get('/logout', userRoute.logout);
//Active/de-active SourceDoc for repository
app.post('/active_sourcedoc', repositoryRoute.activeSourceDoc);
//Receive and process Github Post-Receive hooks
app.post('/github_hook', githubRoute.githubHook);
//All repository revisions
app.get('/:username/:repo/all', repositoryRoute.getAllRevisions);
//Documents route
app.get('/:username/:repo/:revision?/*', documentRoute.getDocument);
//Start the SourceDoc
http.createServer(app).listen(app.get('port'), function() {
console.log("SourceDoc server listening on port " + app.get('port'));
});