-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
220 lines (189 loc) · 6.24 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
require('dotenv').config()
//for Server fnction
const session = require('express-session')
const MySQLStore = require('express-mysql-session')(session) //Added for session store in mysql
const flash = require('connect-flash')
const msal = require('@azure/msal-node')
var createError = require('http-errors')
const express = require('express')
const path = require('path')
var cookieParser = require('cookie-parser')
const morgan = require('morgan')
const winston = require('winston')
const DailyRotateFile = require('winston-daily-rotate-file')
const spdy = require('spdy') //for https
//for File processing
const config = require('config')
const fs = require('fs')
const cors = require('cors')
// config constants
const morganFormat = config.get('morganFormat')
const logDirPath = config.get('logDirPath')
const port = config.get('port')
const privkeyPath = config.get('privkeyPath')
const fullchainPath = config.get('fullchainPath')
const htdocsPath = config.get('htdocsPath')
const defaultZ = config.get('defaultZ')
const mbtilesDir = config.get('mbtilesDir')
// logger configuration
const logger = winston.createLogger({
transports: [
new winston.transports.Console(),
new DailyRotateFile({
filename: `${logDirPath}/coesite-%DATE%.log`,
datePattern: 'YYYY-MM-DD'
})
]
})
logger.stream = {
write: (message) => { logger.info(message.trim()) }
}
// logger until here
var authRouter = require('./routes/auth') //before app
const app = express()
// In-memory storage of logged-in users
// For demo purposes only, production apps should store
// this in a reliable storage
app.locals.users = {};
// MSAL config
const msalConfig = {
auth: {
clientId: process.env.OAUTH_CLIENT_ID,
authority: process.env.OAUTH_AUTHORITY,
clientSecret: process.env.OAUTH_CLIENT_SECRET
//Client credential (secret, certificate, or assertion) must not be empty when creating a confidential client.
//An application should at most have one credential
},
system: {
loggerOptions: {
loggerCallback(loglevel, message, containsPii) {
console.log(message);
},
piiLoggingEnabled: false,
logLevel: msal.LogLevel.Verbose,
}
}
};
// Create msal application object
app.locals.msalClient = new msal.ConfidentialClientApplication(msalConfig);
//(before indexRouter) until here
var indexRouter = require('./routes/index')
var usersRouter = require('./routes/users')
var mapRouter = require('./routes/map') //test 0104
var mapLRouter = require('./routes/map-l') //test 0104
var webmapRouter = require('./routes/webmap') //test 0104
var webmapLRouter = require('./routes/webmap-l')
var webmap3DRouter = require('./routes/webmap-3d')
var VTRouter = require('./routes/VT') //test 0308
//var VTRouter = require('./routes/VT-r') //referer test
//var VTORouter = require('./routes/VT-open') //test 0322(only for development env.)
var esriIFRouter = require('./routes/esriIF') //esri interface (tilemap, etc..)
var rgbElevRouter = require('./routes/rgbElev')
/*
// Session middleware
// NOTE: Uses default in-memory session store, which is not
// suitable for production
app.use(session({
secret: process.env.OAUTH_CLIENT_SECRET,
resave: false,
saveUninitialized: false,
unset: 'destroy'
}))
// note: session will be replaceid with mysql
*/
//session with mysql (from here)
const mysqlOptions ={
host: 'localhost',
port: 3306,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE
}
const sessionStore = new MySQLStore(mysqlOptions)
const sess = {
secret: process.env.OAUTH_CLIENT_SECRET,
cookie: {maxAge: 60000000}, //16h40m
store: sessionStore,
resave: true, //make it true if necessary
saveUninitialized: true //make it true if necessary
}
//sess.cookie.secure = true //for production
app.use(session(sess))
//session with mysql (until here)
// Flash middleware
app.use(flash())
// Set up local vars for template layout
app.use(function (req, res, next) {
// Read any flashed errors and save
// in the response locals
res.locals.error = req.flash('error_msg')
// Check for simple error string and
// convert to layout's expected format
var errs = req.flash('error')
for (var i in errs) {
res.locals.error.push({ message: 'An error occurred', debug: errs[i] })
}
// Check for an authenticated user and load
// into response locals
if (req.session.userId) {
res.locals.user = app.locals.users[req.session.userId]
}
next()
})
// view engine setup
app.set('unvt/views', path.join(__dirname, 'views'))
app.set('view engine', 'hbs')
app.use(morgan(morganFormat, {
stream: logger.stream
}))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
/*
// for credential
const corsOption = {
origin: '*',
credentials: true
}
app.options('*', cors(corsOption)) //test
app.use(cors(corsOption))
// for credential (unitil here)
*/
app.use(cors())
//app.use(express.static(path.join(__dirname, htdocsPath)))
app.use('/unvt', express.static('public'))
app.use('/unvt/', indexRouter)
app.use('/unvt/auth', authRouter) //after app.use('/', indexRouter)
app.use('/unvt/users', usersRouter)
app.use('/unvt/map', mapRouter)
app.use('/unvt/map-l', mapLRouter)
app.use('/unvt/webmap', webmapRouter)
app.use('/unvt/webmap-l', webmapLRouter)
app.use('/unvt/webmap-3d', webmap3DRouter)
app.use('/unvt/VT', VTRouter)
//app.use('/unvt/VT-open', VTORouter)
app.use('/unvt/esriIF', esriIFRouter) //esri interface
app.use('/unvt/rgb-elev', rgbElevRouter)
// error handler
app.use((req, res) => {
res.sendStatus(404)
})
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message
res.locals.error = req.app.get('env') === 'development' ? err : {}
// render the error page
res.status(err.status || 500)
res.render('error')
})
//for https
spdy.createServer({
key: fs.readFileSync(privkeyPath),
cert: fs.readFileSync(fullchainPath)
}, app).listen(port)
//for http
//app.listen(port, () => {
// console.log(`Running at Port ${port} ...`)
//app.listen(3000, () => {
//console.log("running at port 3000 ...")
//})