refactor(server): 整理文件路由和处理逻辑

重构文件服务相关的路由和控制器。移除了未使用的路由和控制器文件,简化了文件服务逻辑。现在,路由更加清晰且控制器逻辑更加集中。此外,更新了.gitignore文件以排除不必要的文件和文件夹,并调整了默认环境变量以适应新的文件路径结构。start.py脚本中自动化构建和启动流程已被移除,以适应手动构建和启动服务器的新流程。
This commit is contained in:
ZZY 2024-08-25 11:10:19 +08:00
parent a891912817
commit 510a33489f
10 changed files with 34 additions and 128 deletions

4
.gitignore vendored
View File

@ -1,9 +1,9 @@
.*
!.gitignore
*.log
dist
node_modules
__pycache__
package-lock.json
vue-project
ws-server
*-*

View File

@ -1,8 +0,0 @@
const router = require('express').Router();
// router.use('/users', require('../users/api.js'));
// router.use('/files', require('../files/api.js'));
router.use('/files', require('../files/api.js'))
module.exports = router;

View File

@ -1,39 +1,11 @@
const router = require('express').Router();
const { getJsonFile } = require('./json');
const {join} = require('path');
const fs = require('fs');
require('dotenv').config();
const env = process.env;
const root = join(__dirname, '../../');
const getFilePath = require('./index');
// router.get('/json/:filename', getJsonFile);
router.get('/cards/:filename', (req, res) => {
const filePath = join(root, `${env.FILE_PATH}/cards/${req.params.filename}.${req.query.type}`);
fs.readFile(filePath, (err, data) => {
if (err) {
console.error(err);
return res.status(500).send('Error reading file');
}
res.setHeader('Content-Type', 'application/json');
res.send(data);
});
})
router.get('/notes/:filename', (req, res) => {
const filePath = join(root, `${env.FILE_PATH}/notes/${req.params.filename}.${req.query.type}`);
fs.readFile(filePath, (err, data) => {
if (err) {
console.error(err);
return res.status(500).send('Error reading file');
}
res.setHeader('Content-Type', 'text/html');
res.send(data);
});
router.get('/:type/*', (req, res) => {
// console.log(req.params);
const path = getFilePath(`${req.params.type}/${req.params[0]}.${req.query.type}`);
res.sendFile(path);
})
module.exports = router;

View File

@ -0,0 +1,24 @@
const {join} = require('path');
const fs = require('fs');
require('dotenv').config();
const env = process.env;
const root = join(__dirname, '../../');
function getFilePath(path) {
return join(root, `${env.FILE_PATH}`, path);
}
// function getFile(path, callback) {
// const filePath = join(root, `${env.FILE_PATH}`, path);
// fs.readFile(filePath, (err, data) => {
// if (err) {
// console.error(err);
// // return res.status(500).send('Error reading file');
// }
// callback(data);
// });
// }
module.exports = getFilePath;

View File

@ -6,4 +6,4 @@ ADMIN_LOGIN_PASSWORD=admin
MOD=
STATIC_PATH=./static
HOME_PATH=./home
FILE_PATH=./home

View File

@ -2,7 +2,7 @@ const express = require('express');
const router = express.Router();
router.use(require('../controllers/api/index'));
router.use('/files', require('../controllers/files/api.js'));
// router.use('/home')
module.exports = router;

View File

@ -1,11 +0,0 @@
const utils = require('../utils.js');
const express = require('express');
const router = express.Router();
router.get('/', function(req, res, next) {
// res.sendFile(utils.path.join(utils.DATA_FILE_PATH, 'index.html'));
res.send('files');
});
module.exports = router;

View File

@ -1,39 +0,0 @@
const router = require('express').Router();
const { getRoot, getAdmin, getLogin } = require('../controllers/users/users.js');
/* GET users listing. */
router.get('/', getRoot);
router.get('/admin', getAdmin);
router.get('/login', getLogin);
// WILL BE FILLING
router.get('/register', function(req, res, next) {
res.send('respond with a resource');
})
router.get('/logout', function(req, res, next) {
res.send('respond with a resource');
})
router.get('/profile', function(req, res, next) {
res.send('respond with a resource');
})
router.get('/profile/edit', function(req, res, next) {
res.send('respond with a resource');
})
router.get('/profile/edit/password', function(req, res, next) {
res.send('respond with a resource');
})
router.get('/profile/edit/username', function(req, res, next) {
res.send('respond with a resource');
})
router.get('/profile/edit/email', function(req, res, next) {
res.send('respond with a resource');
})
router.get('/profile/edit/phone', function(req, res, next) {
res.send('respond with a resource');
})
router.get('/profile/edit/website', function(req, res, next) {
res.send('respond with a resource');
})
module.exports = router;

View File

@ -9,7 +9,7 @@ app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(require('cookie-parser')());
dist = path.join(__dirname, 'vue-project', 'dist')
dist = path.join(__dirname, env.STATIC_PATH ?? 'static')
// 静态资源服务假设你的dist目录包含了编译后的Vue应用
app.use(express.static(dist));

View File

@ -1,32 +0,0 @@
import subprocess
def run_command(command):
"""Run a shell command."""
try:
subprocess.run(command, check=True, shell=True)
except subprocess.CalledProcessError as e:
print(f"Error executing command: {command}")
print(e)
exit(1)
def main():
# Define the commands you want to run
commands = [
# 'git clone https://github.com/yourusername/yourproject.git',
'cd vue-project && git pull',
'cd vue-project && npm install',
'cd vue-project && npm run build',
'git pull',
'npm install',
'cp -r vue-project/dist/* ./dist'
'npm start'
]
# Run each command in sequence
for cmd in commands:
print(f"Executing: {cmd}")
run_command(cmd)
if __name__ == '__main__':
main()