-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.js
124 lines (92 loc) · 3.39 KB
/
application.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
module.exports = function( servers, module ) {
'use strict';
var path = require( 'path' )
, application = module.application
, module_name = application.name || path.dirname( module.path ).split( path.sep ).slice( -1 )[ 0 ]
, www_base = [ __dirname, application.www || 'www' ]
, rs = servers.create_namespace( module_name, true )
, RS = rs.RS
, log = RS.log.bind( null, module_name )
;
log( 'starting application:', application );
servers.set_namespace( rs ); // set namespace to servers' child namespace
require( './database.js' )( rs );
/* --------------------------------------------------------------------------
Authentication with passport
*/
var session_options = require( './passport.js' )( servers, rs );
var login_strategies = rs
.passport_strategies()
.map( function( strategy ) {
var name = strategy.name;
return {
id : name,
flow : 'login_strategies',
order : strategy.order || name,
name : name,
href : '/passport/' + name,
display_name: strategy.display_name || name,
icon : strategy.icon || ''
};
} )
.optimize()
.set()
;
/* --------------------------------------------------------------------------
Sessions
*/
var sessions = rs
.session_store()
//.trace( 'session store content' )
.passport_user_sessions()
;
// Listen when toubkal min is ready
servers.http_listen( rs.toubkal_min() );
/* --------------------------------------------------------------------------
Serve all assets to servers
*/
rs
.union(
[ rs.www_files( www_base ),
rs.toubkal_min(),
rs.source_map_support_min(),
rs.build_bundles( __dirname, www_base )
],
{ key: [ "path"] }
)
.serve( servers, { session_options: session_options } )
;
/* --------------------------------------------------------------------------
Serve dataflows to socket.io clients
*/
var client = {};
var client_module = rs
.set( [ { path: '' } ] )
.directory_entries( __dirname )
.filter( [ { type: 'file', base: 'client.js', depth: 1 } ] )
.path_join( __dirname )
.alter( function( _ ) { _.client = client } )
//.trace( 'client module' )
;
rs.dispatch( client_module, function( source, options ) {
source.require_pipeline( this, options );
} );
var clients = servers
// ToDo: set remove_timeout to a higher production value
.socket_io_clients( { remove_timeout: 10, session_options: session_options } )
.trace( 'clients', { pick: { id: '.id', connected: '.connected' } } ) // client.socket has circular references, don't show it
// Ignore updates in connected state, i.e. when client temporarily disconnects
.pick( [ 'id', 'socket' ] ).optimize()
;
rs
.database()
.union( [ login_strategies, sessions ] )
//.trace( 'all dataflows to clients' )
.dispatch( clients, function( source, options ) {
return client.handler( source, this, options );
}, { no_remove_fetch: true } )
.pass_through() // prevents all clients outputs to connect directly to all inputs of the union bellow
//.trace( 'clients to database' )
.database()
;
}; // module.exports