Skip to content

Commit

Permalink
feat: change socket define place
Browse files Browse the repository at this point in the history
  • Loading branch information
NikFranki committed Dec 17, 2023
1 parent ed15f93 commit 431432d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 38 deletions.
22 changes: 17 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ const logger = require('morgan');
const cors = require('cors');
const session = require('express-session');

const app = express();

const server = http.createServer(app);

const socketIO = require('socket.io');
const socketAllowedOrigins = ['http://localhost:3366', 'https://todo-fe-six.vercel.app'];
const io = socketIO(
server,
{
cors: {
origin: socketAllowedOrigins,
}
}
);
global.io = io;

dotenv.config();

// const rmUnusedImages = require('./lib/middleware/rm_unused_images');
Expand All @@ -25,8 +41,6 @@ const userRouter = require('./routes/user');
const emailRouter = require('./routes/email');
const listRouter = require('./routes/list');

const app = express();

app.set('serverPath', process.env.NODE_ENV === 'PROD' ? 'https://todo-be-psi.vercel.app/' : 'http://localhost:8000/');
app.set('imagesPath', path.join(__dirname, '/public/images/'));
app.set('todoAttachmentFilePath', path.join(__dirname, '/public/todo-attachment/'));
Expand Down Expand Up @@ -91,6 +105,4 @@ app.use(function(err, req, res, next) {
});

const PORT = process.env.PORT || 8000;
app.listen(PORT, () => console.log(`Server is running in port ${PORT}`));


server.listen(PORT, () => console.log(`Server is running in port ${PORT}`));
29 changes: 12 additions & 17 deletions lib/todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const storage = multer.diskStorage({
if (!file.originalname) {
return;
}
callback(null, req.app.get('xlsxsPath'));
callback(null, req.app.get('xlsxsPath'));
},
filename: (req, file, callback) => {
if (!file.originalname) {
Expand All @@ -33,13 +33,13 @@ const todoAttachmentsStorage = multer.diskStorage({
if (!file.originalname) {
return;
}

const dir = `${req.app.get('todoAttachmentFilePath')}${req.body.id}`;

if (!fs.existsSync(dir)){
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}

callback(null, dir);
},
filename: (req, file, callback) => {
Expand All @@ -58,10 +58,10 @@ class Todo {
constructor() {
this.reminderTodos = [];
this.getReminderTodo();

const rule = new schedule.RecurrenceRule();
rule.second = [0, 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]; // 每隔1秒执行一次

const job = schedule.scheduleJob(rule, () => {
const currentDateTime = dayjs().format('YYYY-MM-DD HH:mm:ss');
const dayTimeOnlyTime = dayjs().format('HH:mm:ss');
Expand All @@ -73,12 +73,7 @@ class Todo {
noticedTodo.push(item);
}
});
// avoid circular dependencies
setTimeout(() => {
const io = require('../socket');

io.emit('todo-message', noticedTodo);
}, 0);
global.io.emit('todo-message', noticedTodo);
});

// job.cancel();
Expand All @@ -104,7 +99,7 @@ class Todo {
delete todoItem.due_date_alias;
delete todoItem.create_time_alias;
delete todoItem.update_time_alias;

todoItem.subtask = await conn.queryPromise('SELECT * FROM subtask WHERE todo_id=?', [id]);
res.send({
code: 200,
Expand Down Expand Up @@ -141,7 +136,7 @@ class Todo {
if (content) {
values.push(content);
}

try {
const result = await conn.queryPromise(sqlStatus, values);

Expand Down Expand Up @@ -220,7 +215,7 @@ class Todo {
}

async updateTodo(req, res, next) {
const {
const {
content,
list_id,
added_my_day = 0,
Expand All @@ -243,14 +238,14 @@ class Todo {

if (removed_file) {
const dir = `${req.app.get('todoAttachmentFilePath')}${id}`;
if (fs.existsSync(dir)){
if (fs.existsSync(dir)) {
fs.unlinkSync(`${dir}/${removed_file}`);
}
}

const map = new Map([
['content=?',
content],
content],
['user_id=?', res.locals.username],
['list_id=?', list_id],
['added_my_day=?', added_my_day],
Expand Down
2 changes: 1 addition & 1 deletion routes/todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const router = express.Router();

const { Todo, upload, todoAttachmentsUpload } = require('../lib/todo');

const todo = new Todo({});
const todo = new Todo();

router.post('/list', todo.getList.bind(todo));

Expand Down
15 changes: 0 additions & 15 deletions socket.js

This file was deleted.

1 comment on commit 431432d

@vercel
Copy link

@vercel vercel bot commented on 431432d Dec 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.