1
1
const express = require ( 'express' ) ;
2
- const session = require ( 'express-session' ) ;
3
2
const connectDb = require ( '../lib/db/connectDb' ) ;
4
3
const indexSync = require ( '../lib/db/indexSync' ) ;
5
4
const path = require ( 'path' ) ;
6
5
const cors = require ( 'cors' ) ;
7
6
const routes = require ( './routes' ) ;
8
7
const errorController = require ( './controllers/ErrorController' ) ;
9
8
const passport = require ( 'passport' ) ;
9
+ const configureSocialLogins = require ( './socialLogins' ) ;
10
+
10
11
const port = process . env . PORT || 3080 ;
11
12
const host = process . env . HOST || 'localhost' ;
12
13
const projectPath = path . join ( __dirname , '..' , '..' , 'client' ) ;
13
- const {
14
- jwtLogin,
15
- passportLogin,
16
- googleLogin,
17
- githubLogin,
18
- discordLogin,
19
- facebookLogin,
20
- setupOpenId,
21
- } = require ( '../strategies' ) ;
22
-
23
- // Init the config and validate it
24
- const config = require ( '../../config/loader' ) ;
25
- config . validate ( ) ; // Validate the config
14
+ const { jwtLogin, passportLogin } = require ( '../strategies' ) ;
26
15
27
- ( async ( ) => {
16
+ const startServer = async ( ) => {
28
17
await connectDb ( ) ;
29
18
console . log ( 'Connected to MongoDB' ) ;
30
19
await indexSync ( ) ;
31
20
32
21
const app = express ( ) ;
22
+
23
+ // Middleware
33
24
app . use ( errorController ) ;
34
25
app . use ( express . json ( { limit : '3mb' } ) ) ;
35
26
app . use ( express . urlencoded ( { extended : true , limit : '3mb' } ) ) ;
36
27
app . use ( express . static ( path . join ( projectPath , 'dist' ) ) ) ;
37
28
app . use ( express . static ( path . join ( projectPath , 'public' ) ) ) ;
38
-
39
29
app . set ( 'trust proxy' , 1 ) ; // trust first proxy
40
30
app . use ( cors ( ) ) ;
41
31
@@ -51,38 +41,11 @@ config.validate(); // Validate the config
51
41
passport . use ( await passportLogin ( ) ) ;
52
42
53
43
if ( process . env . ALLOW_SOCIAL_LOGIN === 'true' ) {
54
- if ( process . env . GOOGLE_CLIENT_ID && process . env . GOOGLE_CLIENT_SECRET ) {
55
- passport . use ( await googleLogin ( ) ) ;
56
- }
57
- if ( process . env . FACEBOOK_CLIENT_ID && process . env . FACEBOOK_CLIENT_SECRET ) {
58
- passport . use ( await facebookLogin ( ) ) ;
59
- }
60
- if ( process . env . GITHUB_CLIENT_ID && process . env . GITHUB_CLIENT_SECRET ) {
61
- passport . use ( await githubLogin ( ) ) ;
62
- }
63
- if ( process . env . DISCORD_CLIENT_ID && process . env . DISCORD_CLIENT_SECRET ) {
64
- passport . use ( await discordLogin ( ) ) ;
65
- }
66
- if (
67
- process . env . OPENID_CLIENT_ID &&
68
- process . env . OPENID_CLIENT_SECRET &&
69
- process . env . OPENID_ISSUER &&
70
- process . env . OPENID_SCOPE &&
71
- process . env . OPENID_SESSION_SECRET
72
- ) {
73
- app . use (
74
- session ( {
75
- secret : process . env . OPENID_SESSION_SECRET ,
76
- resave : false ,
77
- saveUninitialized : false ,
78
- } ) ,
79
- ) ;
80
- app . use ( passport . session ( ) ) ;
81
- await setupOpenId ( ) ;
82
- }
44
+ configureSocialLogins ( app ) ;
83
45
}
46
+
84
47
app . use ( '/oauth' , routes . oauth ) ;
85
- // api endpoint
48
+ // API Endpoints
86
49
app . use ( '/api/auth' , routes . auth ) ;
87
50
app . use ( '/api/user' , routes . user ) ;
88
51
app . use ( '/api/search' , routes . search ) ;
@@ -97,21 +60,23 @@ config.validate(); // Validate the config
97
60
app . use ( '/api/plugins' , routes . plugins ) ;
98
61
app . use ( '/api/config' , routes . config ) ;
99
62
100
- // static files
63
+ // Static files
101
64
app . get ( '/*' , function ( req , res ) {
102
65
res . sendFile ( path . join ( projectPath , 'dist' , 'index.html' ) ) ;
103
66
} ) ;
104
67
105
68
app . listen ( port , host , ( ) => {
106
69
if ( host == '0.0.0.0' ) {
107
70
console . log (
108
- `Server listening on all interface at port ${ port } . Use http://localhost:${ port } to access it` ,
71
+ `Server listening on all interfaces at port ${ port } . Use http://localhost:${ port } to access it` ,
109
72
) ;
110
73
} else {
111
74
console . log ( `Server listening at http://${ host == '0.0.0.0' ? 'localhost' : host } :${ port } ` ) ;
112
75
}
113
76
} ) ;
114
- } ) ( ) ;
77
+ } ;
78
+
79
+ startServer ( ) ;
115
80
116
81
let messageCount = 0 ;
117
82
process . on ( 'uncaughtException' , ( err ) => {
0 commit comments