-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
92 lines (76 loc) · 2.38 KB
/
server.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
// Setup basic express server
const express = require('express')
const app = express()
const server = require('http').createServer(app)
const io = require('socket.io')(server)
const port = process.env.PORT || 3000
// Other requires
const path = require('path')
const localip = require('internal-ip').v4()
const config = require('config')
const server_mode = config.get('mode')
/// Module initialization
// Init dispatcher
const Dispatcher = require('./lib/dispatcher')
var dispatcher = new Dispatcher(io)
// Init playlist
const playlist_config = config.get('playlist')
const Playlist = require('./lib/playlist')
var playlist = new Playlist(dispatcher, playlist_config, server_mode)
// Init webcontroller
const WebController = require('./lib/webcontroller')
var webController = new WebController(playlist, io)
// Init API router
const ApiRouter = require('./route/api')
var apiRouter = new ApiRouter(playlist)
// Init Search router
const SearchRouter = require('./route/search')
var searchRouter = new SearchRouter(playlist_config)
// Init Discord Bot
const discord_config = config.get('discord')
if(discord_config.enabled) {
const DiscordBot = require('./lib/discordbot')
const discordBot = new DiscordBot(playlist, discord_config, server_mode)
discordBot.login()
}
/// Express setting
// Init EJS
app.set('views', path.join(__dirname, 'view'))
app.engine('html', require('ejs').renderFile)
app.set('view engine', 'html')
// Static folder
app.use(express.static(path.join(__dirname, '/public')))
// Routing
app.get('/', function (req, res) {
// in ipv6 localhost look like `::ffff:127.0.0.1`
if (/127.0.0.1|::1/.test(req.ip)) {
res.render('index', {
serverip: localip,
info: "新增歌曲: http://ccns-radio:3000"
})
} else if(server_mode == 'station') {
res.redirect('/control')
} else if(server_mode == 'service') {
res.redirect('/client')
}
})
app.get('/control', function (req, res) {
res.render('index', {
serverip: localip,
info: "控制器模式"
})
})
app.get('/client', function (req, res) {
res.render('index', {
serverip: localip,
info: "歡迎光臨"
})
})
app.use('/api', apiRouter.getRouter())
app.use('/search', searchRouter.getRouter())
/// Start Server
server.listen(port, function () {
console.log('Server listening at port %d', port)
})
/// Socket.io
io.on('connection', webController.connectionHandler())