77var express = require ( 'express' ) ;
88var colors = require ( 'colors' ) ;
99var fs = require ( 'fs' ) ;
10- var deepExtend = require ( 'deep-extend' ) ;
1110var loadOptions = require ( './core/loadOptions' ) ;
1211var commander = require ( 'commander' ) ;
1312var bodyParser = require ( 'body-parser' ) ;
@@ -51,6 +50,19 @@ global.app.use(require('compression')());
5150
5251// Cookies
5352global . app . use ( require ( 'cookie-parser' ) ( ) ) ;
53+ global . app . use ( require ( 'express-session' ) ( {
54+ secret : ( function ( ) {
55+ var d = new Date ( ) . getTime ( ) ;
56+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' . replace ( / [ x y ] / g, function ( c ) {
57+ var r = ( d + Math . random ( ) * 16 ) % 16 | 0 ;
58+ d = Math . floor ( d / 16 ) ;
59+ return ( c === 'x' ? r : ( r & 0x3 | 0x8 ) ) . toString ( 16 ) ;
60+ } ) ;
61+ } ) ( ) ,
62+ resave : false ,
63+ saveUninitialized : true
64+ } ) ) ;
65+
5466app . use ( function ( req , res , next ) {
5567 res . cookie ( 'source-mode' , global . MODE , { maxAge : 3600000 , httpOnly : false } ) ;
5668
@@ -71,48 +83,42 @@ app.use(bodyParser.json());
7183
7284/* Middlewares */
7385
74- // LESS processing
75- if ( global . MODE === 'development' ) {
76- var less = require ( 'less-middleware' ) ;
77-
78- var lessOpts = {
79- src : global . app . get ( 'user' )
80- } ;
81-
82- deepExtend ( lessOpts , global . opts . core . less ) ;
83-
84- global . app . use ( less ( lessOpts ) ) ;
85- }
86-
86+ // Auth initializing
87+ var auth = require ( './core/auth' ) ( app ) ;
88+ app . use ( auth . everyauth . middleware ( ) ) ;
8789
8890// Clarify
8991global . app . use ( require ( './core/middleware/clarify' ) ) ;
9092
91-
9293// File tree module
93- fileTree = require ( './core/file-tree' ) ;
94- global . app . use ( function ( req , res , next ) {
94+ var fileTree = require ( './core/file-tree' ) ;
9595
96- // Updating navigation on each main page visit
97- if ( global . MODE !== 'presentation' && req . url === "/" ) {
98- // Making this async
99- fileTree . scan ( ) ;
100- }
96+ // Run file tree scan on start
97+ fileTree . scan ( ) ;
98+
99+ // Run file tree scan on main page visit
100+ if ( global . opts . core . fileTree . mainPageTrigger && global . MODE !== 'presentation' ) {
101+ global . app . use ( function ( req , res , next ) {
102+
103+ // Updating navigation on each main page visit
104+ if ( req . url === "/" ) fileTree . scan ( ) ;
105+
106+ next ( ) ;
107+ } ) ;
108+ }
101109
102- next ( ) ;
103- } ) ;
104110
105111// Middleware that loads spec content
106112var read = require ( "./core/middleware/read" ) ;
107- global . app . use ( read . handleIndex ) ;
108113global . app . use ( read . process ) ;
109114
115+ // Markdown
116+ global . app . use ( require ( "./core/middleware/md" ) . process ) ;
117+ global . app . use ( require ( "./core/middleware/mdTag" ) . process ) ;
118+
110119// Load user defined middleware, that processes spec content
111120require ( "./core/middleware/userMiddleware" ) ;
112121
113- // Basic markdown support
114- global . app . use ( require ( "./core/middleware/markdown" ) . process ) ;
115-
116122// Middleware that wraps spec with Source template
117123global . app . use ( require ( "./core/middleware/wrap" ) . process ) ;
118124
@@ -142,6 +148,13 @@ try {
142148 // User additional functionality
143149 require ( global . app . get ( 'user' ) + "/core/app.js" ) ;
144150} catch ( e ) { }
151+
152+
153+ // Watchers
154+ if ( global . opts . core . watch . enabled && global . MODE !== 'presentation' ) {
155+ require ( './core/watchNewSpecs' ) ;
156+ }
157+
145158/* /Includes */
146159
147160
@@ -153,10 +166,9 @@ var headerFooter = require('./core/headerFooter');
153166global . app . use ( express . static ( global . app . get ( 'user' ) ) ) ;
154167
155168// Page 404
156- global . app . use ( function ( req , res , next ) {
169+ global . app . use ( function ( req , res ) {
157170
158171 if ( req . accepts ( 'html' ) ) {
159-
160172 var headerFooterHTML = headerFooter . getHeaderAndFooter ( ) ;
161173 res . status ( 404 ) . render ( __dirname + '/core/views/404.ejs' , {
162174 header : headerFooterHTML . header ,
@@ -171,26 +183,23 @@ global.app.use(function(req, res, next){
171183
172184/* Error handling */
173185var logErrors = function ( err , req , res , next ) {
174- log . error ( ( "Error: " + err . stack ) . red ) ;
175- next ( err ) ;
176- } ;
186+ if ( err ) {
187+ var url = req . url || '' ;
177188
178- var clientErrorHandler = function ( err , req , res , next ) {
179- if ( req . xhr ) {
180- res . send ( 500 , { error : 'Something blew up!' } ) ;
189+ log . debug ( req . method , req . headers ) ;
190+ log . error ( url . red , ( 'Error: ' + err . stack ) . red ) ;
191+
192+ if ( req . xhr ) {
193+ res . status ( 500 ) . json ( { msg : 'Server error' } ) ;
194+ } else {
195+ res . status ( 500 ) . send ( 'Server error' ) ;
196+ }
181197 } else {
182- next ( err ) ;
198+ next ( ) ;
183199 }
184200} ;
185201
186- var errorHandler = function ( err , req , res , next ) {
187- res . status ( 500 ) ;
188- res . render ( 'error' , { error : err } ) ;
189- } ;
190-
191202global . app . use ( logErrors ) ;
192- global . app . use ( clientErrorHandler ) ;
193- global . app . use ( errorHandler ) ;
194203/* /Error handling */
195204
196205
@@ -202,5 +211,5 @@ if (!module.parent) {
202211 global . app . listen ( port ) ;
203212 var portString = port . toString ( ) ;
204213
205- log . info ( '[SOURCEJS] launched on http://localhost :' . blue + portString . red + ' in ' . blue + MODE . blue + ' mode...' . blue ) ;
214+ log . info ( '[SOURCEJS] launched on http://127.0.0.1 :' . blue + portString . red + ' in ' . blue + MODE . blue + ' mode...' . blue ) ;
206215}
0 commit comments