Skip to content

Commit

Permalink
Merge pull request #74 from qfdk/vue
Browse files Browse the repository at this point in the history
Vue
  • Loading branch information
qfdk authored Oct 4, 2024
2 parents 782c7ce + 59fe8a1 commit a8f5f16
Show file tree
Hide file tree
Showing 17 changed files with 1,811 additions and 196 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "web"]
path = web
url = [email protected]:qfdk/EasyDocker-web.git
38 changes: 9 additions & 29 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,18 @@ const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const app = express();
const session = require('express-session');

const {checkUser} = require('./middlewares/security');

const io = require('socket.io')();
const favicon = require('serve-favicon');
app.io = io;

const index = require('./routes/index');
const api = require('./routes/api');
const overview = require('./routes/overview');
const containers = require('./routes/containers')(io);
const images = require('./routes/images')(io);
const publicRoute = require('./routes/public');
const privateRoute = require('./routes/private');
const {verifyToken} = require('./middlewares/security');
const {logger} = require('./utils/logger');

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
app.engine('html', require('ejs').renderFile);

app.use(session({
saveUninitialized: false,
resave: false,
secret: 'easy-docker-web',
cookie: {
maxAge: 365 * 24 * 60 * 60 * 1000,
expires: false
}
}));

// public files
app.use('/static', express.static(__dirname + '/public'));
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
Expand All @@ -53,16 +36,13 @@ app.all('*', (req, res, next) => {
}
});

app.use(checkUser);
app.use((req, res, next) => {
res.locals.isLogin = req.session.isLogin || false;
logger.info(`[${req.method}] ${req.originalUrl}`);
next();
});
app.use('/', index);
app.use('/api', api);
app.use('/overview', overview);
app.use('/containers', containers);
app.use('/images', images);
app.use(publicRoute);
app.use(verifyToken);
app.use(privateRoute);

// catch 404 and forward to error handler
app.use((req, res, next) => {
Expand All @@ -78,7 +58,7 @@ app.use((err, req, res, next) => {
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
res.json({message: err.message});
});

module.exports = app;
84 changes: 41 additions & 43 deletions bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
* Module dependencies.
*/

var app = require('../app');
var io = app.io;
var http = require('http');

const app = require('../app');
const http = require('http');
const {logger} = require('../utils/logger');
/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
const port = normalizePort(process.env.PORT || '6000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);
const server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
Expand All @@ -28,64 +27,63 @@ var server = http.createServer(app);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
io.attach(server);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);
const port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}
if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}
if (port >= 0) {
// port number
return port;
}

return false;
return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.log(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.log(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
if (error.syscall !== 'listen') {
throw error;
}

const bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
logger.info(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
logger.info(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
console.log('Listening on ' + bind);
const addr = server.address();
const bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
logger.info('Listening on ' + bind);
}
53 changes: 39 additions & 14 deletions middlewares/security.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,45 @@
const checkUser = (req, res, next) => {
res.locals.isLogin = false;
if (req.session.isLogin) {
res.locals.isLogin = true;
next();
} else {
const username = process.env.EDW_USERNAME,
password = process.env.EDW_PASSWORD;
if (req.body.username === username && req.body.password === password) {
req.session.isLogin = true;
res.redirect('/');
} else {
res.render('login');
const jwt = require('jsonwebtoken');

const verifyToken = (req, res, next) => {
const rawToken = req.headers['authorization'];
if (!rawToken) { // 检查是否存在token
return res.status(403).send({message: 'No token provided!'});
}

const tokenParts = rawToken.split('Bearer ');
if (tokenParts.length !== 2) { // 检查token格式是否正确
return res.status(403).send({message: 'Invalid token format!'});
}

const token = tokenParts[1]; // 获取token部分
if (!token || token === 'undefined') { // 检查token是否存在或者为字符串"undefined"
return res.status(403).send({message: 'No token provided!'});
}

jwt.verify(token, process.env.JWT_SECRET, (err, user) => { // 验证token
if (err) {
return res.status(401).send({message: 'Unauthorized!', err});
}
req.user = user;
return next();
});
};

/**
* 权限检查
* @param role
* @returns {(function(*, *, *): (*))|*}
*/
const hasRole = (role) => (req, res, next) => {
if (req.user.roles.includes(role)) {
return next();
} else {
return res.status(401).send({message: 'Unauthorized!'});
}

};

module.exports = {
checkUser
verifyToken,
hasRole
};
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
"version": "1.0.0",
"private": true,
"scripts": {
"start": "nodemon ./bin/www"
"startBackend": "nodemon ./bin/www",
"startFrontend": "cd web;pnpm dev"
},
"dependencies": {
"body-parser": "1.20.1",
"dockerode": "^4.0.2",
"dotenv": "^16.4.5",
"ejs": "3.1.7",
"express": "^4.16.4",
"express-session": "^1.17.1",
"express-status-monitor": "^1.2.8",
"jsonwebtoken": "^9.0.2",
"pino": "^9.1.0",
"pino-pretty": "^11.0.0",
"serve-favicon": "^2.4.5",
"socket.io": "2.4.1",
"dotenv": "^16.4.5"
"socket.io": "2.5.0"
},
"devDependencies": {
"nodemon": "^3.0.3"
Expand Down
Loading

0 comments on commit a8f5f16

Please sign in to comment.