Skip to content

Commit

Permalink
Merge pull request #13 from gnehs/dev
Browse files Browse the repository at this point in the history
準備發布 0.5.4
  • Loading branch information
gnehs authored Aug 9, 2018
2 parents 507b883 + ddd35b7 commit b73613a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 28 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ RUN npm install --production
ENV NODE_ENV=production

EXPOSE 3000
EXPOSE 3001

CMD forever -c "npm start" ./

3 changes: 2 additions & 1 deletion config-simple.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"password": "CUSTOM_PASSWORD_HERE",
"passwordSwitch": true,
"sessionSecret": "hello!!",
"instantUpgradeProcess": false
"instantUpgradeProcess": false,
"debug": false
},
"DSM": {
"protocol": "https",
Expand Down
70 changes: 47 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@ const schedule = require('node-schedule'); // 很會計時ㄉ朋友
const base64 = require('base-64');
//express
const express = require('express');
const session = require('express-session');
const FileStore = require('session-file-store')(require('express-session')); // session
const session = require('express-session')({
store: new FileStore(),
secret: config.PokaPlayer.sessionSecret,
resave: false,
saveUninitialized: true
});
const helmet = require('helmet'); // 防範您的應用程式出現已知的 Web 漏洞
const bodyParser = require('body-parser'); // 讀入 post 請求
const app = express(); // Node.js Web 架構
const FileStore = require('session-file-store')(session); // session
const git = require('simple-git/promise')(__dirname);
const server = require('http').createServer(app),
io = require('socket.io').listen(server)
io = require('socket.io').listen(server),
sharedsession = require("express-socket.io-session")
app.set('views', __dirname + '/views');
app.set('view engine', 'pug')
app.use(bodyParser.urlencoded({ extended: true }));
app.use(helmet.hidePoweredBy({ setTo: 'PHP/7.1.20' }));
app.use(session({
store: new FileStore(),
secret: config.PokaPlayer.sessionSecret,
resave: false,
saveUninitialized: true
app.use(session);
io.use(sharedsession(session, {
autoSave: true
}));
// 時間處理
const moment = require('moment-timezone');
Expand Down Expand Up @@ -78,7 +82,37 @@ app.get('/', (req, res) => {
function pp_decode(str) {
return base64.decode(decodeURIComponent(str))
}
io.on('connection', socket => {
socket.emit('hello')
// Accept a login event with user's data
socket.on("login", function(userdata) {
socket.handshake.session.userdata = userdata;
socket.handshake.session.save();
});
socket.on("logout", function(userdata) {
if (socket.handshake.session.userdata) {
delete socket.handshake.session.userdata;
socket.handshake.session.save();
}
});
socket.on('update', (userdata) => {
if (socket.handshake.session.pass == config.PokaPlayer.password) {
socket.emit('init')
git
.fetch(["--all"])
.then(() => socket.emit('git', 'fetch'))
.then(() => git.reset(["--hard", "origin/" + config.PokaPlayer.debug ? 'dev' : 'master']))
.then(() => socket.emit('git', 'reset'))
.then(() => socket.emit('restart'))
.then(() => process.exit())
.catch(err => {
console.error('failed: ', err)
socket.emit('err', err.toString())
})
} else { socket.emit('Permission Denied Desu') }
})

});
// 更新

app.get('/upgrade', (req, res) => {
Expand All @@ -88,7 +122,7 @@ app.get('/upgrade', (req, res) => {
if (!config.PokaPlayer.instantUpgradeProcess) {
git
.fetch(["--all"])
.then(() => git.reset(["--hard", "origin/master"]))
.then(() => git.reset(["--hard", "origin/" + config.PokaPlayer.debug ? 'dev' : 'master']))
.then(() => res.send('upgrade'))
.then(() => process.exit())
.catch(err => {
Expand All @@ -97,20 +131,6 @@ app.get('/upgrade', (req, res) => {
});
} else {
res.send('socket')
io.on('connection', socket => {
socket.emit('init')
socket.on('restart', () => process.exit())
git
.fetch(["--all"])
.then(() => socket.emit('git', 'fetch'))
.then(() => git.reset(["--hard", "origin/master"]))
.then(() => socket.emit('git', 'reset'))
.then(() => socket.emit('restart'))
.catch(err => {
console.error('failed: ', err)
socket.emit('err', err.toString())
})
})
}
}
})
Expand All @@ -124,6 +144,10 @@ app.get('/info', (req, res) => {
}
})

if (config.PokaPlayer.debug) app.get('/debug', (req, res) => {
res.send('true')
})

// get song
app.get('/song/:res/:id', async(req, res) => {
if (req.session.pass != config.PokaPlayer.password && config.PokaPlayer.passwordSwitch)
Expand Down
16 changes: 15 additions & 1 deletion js/script.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// 宣告全域變數
songList = [];
const lrc = new Lyrics(`[00:00.000]`);
const socket = io();
socket.on("hello", function() {
console.log('hello')
socket.emit('login')
});
// 初始化播放器
const ap = new APlayer({
container: document.getElementById('aplayer'),
Expand Down Expand Up @@ -957,32 +962,41 @@ async function show_settings() {
{
text: '對啦',
onClick: async function(inst) {
mdui.snackbar('正在更新...');
mdui.snackbar('正在更新...', { position: getSnackbarPosition() });
let update = await axios.get('/upgrade/')
if (update.data == "upgrade") {
mdui.snackbar('伺服器重新啟動', {
buttonText: '重新連接',
onButtonClick: () => window.location.reload(),
})
} else if (update.data == "socket") {
socket.emit('update')
socket.on('Permission Denied Desu', () => mdui.snackbar('Permission Denied', {
timeout: 3000,
position: getSnackbarPosition()
}))
socket.on('init', () => mdui.snackbar('正在初始化...', {
timeout: 3000,
position: getSnackbarPosition()
}))
socket.on('git', data => mdui.snackbar({
fetch: '初始化完成',
reset: '更新檔下載完成'
}[data], {
timeout: 3000,
position: getSnackbarPosition()
}))
socket.on('restart', () => {
socket.emit('restart')
mdui.snackbar('伺服器正在重新啟動...', {
buttonText: '重新連接',
onButtonClick: () => window.location.reload(),
position: getSnackbarPosition()
})
})
socket.on('err', data => mdui.snackbar('錯誤: ' + data, {
timeout: 8000,
position: getSnackbarPosition()
}))
}
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pokaplayer",
"version": "0.5.3",
"version": "0.5.4",
"description": "",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -32,4 +32,4 @@
"simple-git": "^1.96.0",
"socket.io": "^2.1.1"
}
}
}
1 change: 1 addition & 0 deletions views/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ html
script(src='//cdnjs.cloudflare.com/ajax/libs/aplayer/1.10.1/APlayer.min.js')
script(src='//cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.min.js')
script(src='//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js')
script(src='//cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js')
script(src='/js/lyrics.min.js')
script(src='/js/base64.js')
script(src='/js/template.js')
Expand Down

0 comments on commit b73613a

Please sign in to comment.