Skip to content

Commit 4f7ae8e

Browse files
committed
a
1 parent 2c440bf commit 4f7ae8e

11 files changed

+181
-54
lines changed

config.sample.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module.exports = {
22
db_url: 'mongodb://localhost/bbs',
33
db_name: 'bbs',
44
ui_path: '.uibuild',
5+
// ip_header:'x-forwarded-for', Proxy IP header
56
config: {
67
PORT: 10001, // The port the app will listen on.
78
/* Fill and uncomment the folling line to enable mail service.

development.js

+25-7
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,42 @@ process.on('restart', async () => {
1919
});
2020

2121
async function run() {
22-
const hydro = require('hydro').app;
22+
const hydro = require('hydro-framework');
2323
let config;
2424
try {
2525
config = require('./config.js');
2626
config.lib = ['@'];
27-
config.handler = ['trace', '@', 'base', 'message', 'user', 'main'];
27+
config.middleware = [
28+
hydro.handler.trace,
29+
'@',
30+
hydro.handler.base,
31+
hydro.handler.user
32+
];
33+
config.hosts = {
34+
'osu.sh': [
35+
require('./handler/main/main.js')
36+
],
37+
'bbs.osu.sh': [
38+
require('./handler/bbs/message.js'),
39+
require('./handler/bbs/main.js')
40+
]
41+
};
42+
config.perm = require('./permission.js');
2843
config.deploy = require('./scripts/deploy.js');
29-
config.app_path = __dirname;
44+
config.constants = {
45+
MODE_NAME: ['osu!', 'osu!taiko', 'osu!catch', 'osu!mania']
46+
};
3047
} catch (e) {
31-
console.error('Error: Cannot load config');
48+
console.error('Error loading application:');
49+
console.error(e);
3250
process.exit(1);
3351
}
34-
global.Hydro = new hydro(config);
35-
await Hydro.load().catch(e => {
52+
global.Hydro = new hydro.app(config);
53+
await global.Hydro.load().catch(e => {
3654
console.error('Error loading application:');
3755
console.error(e);
3856
});
39-
await Hydro.listen().catch(e => {
57+
await global.Hydro.listen().catch(e => {
4058
console.error(e);
4159
});
4260
}

handler/main.js handler/bbs/main.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const
22
Router = require('koa-router'),
33
{ ObjectID } = require('bson'),
4-
{ PermissionError } = require('hydro').errors,
5-
{ PERM_THREAD_CREATE, PERM_THREAD_REPLY, PERM_THREAD_DELETE, PERM_REPLY_DELETE } = require('../constants.js');
4+
{ PermissionError } = require('hydro-framework').errors,
5+
{ PERM_THREAD_CREATE, PERM_THREAD_REPLY, PERM_THREAD_DELETE, PERM_REPLY_DELETE } = require('../../permission.js');
66

7-
module.exports = class HANDLER_MAIN {
7+
exports.handler = class {
88
constructor(i) {
99
this.db = i.db;
1010
this.lib = i.lib;
@@ -25,7 +25,7 @@ module.exports = class HANDLER_MAIN {
2525
this.router
2626
.get('/', async ctx => {
2727
let categories = await this.lib.conf.get('categories');
28-
await ctx.render('index', { categories });
28+
await ctx.render('bbs_index', { categories });
2929
})
3030
.get('/c/:category', async (ctx, next) => {
3131
let page = ctx.query.page || 1, category;
@@ -39,7 +39,7 @@ module.exports = class HANDLER_MAIN {
3939
else {
4040
let threads = await this.db.collection('thread').find({ category: category.id })
4141
.skip((page - 1) * 50).limit(50).sort({ time: -1 }).toArray();
42-
await ctx.render('list', { category, threads });
42+
await ctx.render('bbs_list', { category, threads });
4343
}
4444
})
4545
.get('/c/:category/create', async (ctx, next) => {
@@ -50,7 +50,7 @@ module.exports = class HANDLER_MAIN {
5050
break;
5151
}
5252
if (!category) await next();
53-
else await ctx.render('create', { category });
53+
else await ctx.render('bbs_create', { category });
5454
})
5555
.post('/c/:category/create', async (ctx, next) => {
5656
if (!ctx.state.user.hasPerm(PERM_THREAD_CREATE)) throw new PermissionError();
@@ -92,7 +92,7 @@ module.exports = class HANDLER_MAIN {
9292
for (let i in comments) queue.push(await this.formatComment(comments[i]));
9393
await Promise.all(queue);
9494
thread.author = author;
95-
await ctx.render('thread', { thread, comments, category });
95+
await ctx.render('bbs_thread', { thread, comments, category });
9696
}
9797
})
9898
.all('/t/:id/delete', async (ctx, next) => {

handler/message.js handler/bbs/message.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const
22
Router = require('koa-router'),
33
{ ObjectID } = require('bson'),
4-
{ PermissionError, NotFoundError } = require('hydro').errors,
5-
{ PERM_THREAD_CREATE, PERM_THREAD_REPLY, PERM_THREAD_DELETE, PERM_REPLY_DELETE } = require('../constants.js');
4+
{ PermissionError, NotFoundError } = require('hydro-framework').errors,
5+
{ PERM_THREAD_CREATE, PERM_THREAD_REPLY, PERM_THREAD_DELETE, PERM_REPLY_DELETE } = require('../../permission.js');
66

7-
module.exports = class HANDLER_MAIN {
7+
exports.handler = class {
88
constructor(i) {
99
this.db = i.db;
1010
this.lib = i.lib;

handler/file/main.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const
2+
Router = require('koa-router'),
3+
{ ObjectID } = require('bson'),
4+
{ PermissionError } = require('hydro-framework').errors;
5+
6+
exports.handler = class {
7+
constructor(i) {
8+
this.db = i.db;
9+
this.gfs = i.gfs;
10+
this.lib = i.lib;
11+
this.router = new Router();
12+
}
13+
async getUser(uid) {
14+
let _id = new ObjectID(uid);
15+
return new this.lib.user.user(await this.db.collection('user').findOne({ _id }));
16+
}
17+
async init() {
18+
let secret = await this.lib.conf.get('file.secret');
19+
this.router
20+
.get('/:id/:token', async ctx => {
21+
if (this.lib.crypto.decrypt(token, id));
22+
await ctx.render('home_index', { maps });
23+
});
24+
return this.router;
25+
}
26+
};

handler/main/api.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const
2+
Router = require('koa-router'),
3+
{ ObjectID } = require('bson'),
4+
{ requirePerm } = require('hydro-framework').handler,
5+
{ PermissionError } = require('hydro-framework').errors;
6+
7+
exports.handler = class {
8+
constructor(i) {
9+
this.db = i.db;
10+
this.lib = i.lib;
11+
this.router = new Router({ prefix: '/api' });
12+
this.maps = this.db.collection('maps');
13+
}
14+
async getUser(uid) {
15+
let _id = new ObjectID(uid);
16+
return new this.lib.user.user(await this.db.collection('user').findOne({ _id }));
17+
}
18+
async init() {
19+
this.router
20+
.get('/update/:id', requirePerm(), async ctx => {
21+
await this.maps.insertOne({
22+
id: ctx.params.id,
23+
lastUpdateAt: new Date()
24+
});
25+
ctx.body = '200 OK';
26+
});
27+
return this.router;
28+
}
29+
};

handler/main/main.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const
2+
Router = require('koa-router'),
3+
{ ObjectID } = require('bson'),
4+
axios = require('axios'),
5+
{ merge } = require('hydro-framework').handler,
6+
{ PermissionError } = require('hydro-framework').errors;
7+
8+
exports.handler = class {
9+
constructor(i) {
10+
this.db = i.db;
11+
this.lib = i.lib;
12+
this.router = new Router();
13+
this.maps = this.db.collection('maps');
14+
}
15+
async getUser(uid) {
16+
let _id = new ObjectID(uid);
17+
return new this.lib.user.user(await this.db.collection('user').findOne({ _id }));
18+
}
19+
async init() {
20+
this.router
21+
.get('/', async ctx => {
22+
let maps = await this.maps.find().limit(50).toArray();
23+
await ctx.render('home_index', { maps });
24+
})
25+
.get('/download/:id', async ctx => {
26+
let map = await this.maps.findOne({ _id: new ObjectID(ctx.params.id) });
27+
if (map) ctx.redirect(map.link);
28+
})
29+
.all('/add', merge, async ctx => {
30+
let info = await axios.get(`https://api.sayobot.cn/v2/beatmapinfo?0=${ctx.request.body.url}`);
31+
ctx.body = info.data;
32+
info = info.data;
33+
if (info.status) throw new Error(info.status);
34+
else info = info.data;
35+
info.modes = [];
36+
let modes = {};
37+
for (let b of info.bid_data) modes[b.mode] = true;
38+
for (let mode in modes) info.modes.push(mode);
39+
await this.maps.deleteMany({ sid: info.sid });
40+
await this.maps.insertOne(Object.assign({
41+
link: `https://txy1.sayobot.cn/download/osz/${info.sid}`,
42+
lastUpdate: new Date()
43+
}, info));
44+
ctx.redirect('/');
45+
});
46+
return this.router;
47+
}
48+
};

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
},
2323
"homepage": "https://github.com/sayobot-dev/bbs#readme",
2424
"dependencies": {
25-
"hydro-framework": "^0.0.0"
25+
"axios": "^0.19.0",
26+
"hydro-framework": "^0.0.3"
2627
},
2728
"devDependencies": {
2829
"eslint": "^6.5.1"

constants.js permission.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
const { ObjectID } = require('bson');
21
module.exports = {
3-
TYPE_THREAD: 1,
4-
UID_GUEST: new ObjectID('000000000000000000000000'),
52
PERM_THREAD_CREATE: 1,
63
PERM_THREAD_REPLY: 2,
74
PERM_THREAD_DELETE: 4,

scripts/deploy.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const
2-
log = require('hydro').log.get('Deploy'),
3-
{ UID_GUEST } = require('../constants.js'),
2+
log = require('hydro-framework').log.get('Deploy'),
3+
{ UID_GUEST } = require('hydro-framework'),
44
expects = {
55
udoc: {
66
_id: UID_GUEST,

0 commit comments

Comments
 (0)