mirror of
https://github.com/paricafe/misskey.git
synced 2024-11-29 14:46:43 -06:00
96bc17aa10
Co-authored-by: syuilo <syuilotan@yahoo.co.jp>
156 lines
3.4 KiB
TypeScript
156 lines
3.4 KiB
TypeScript
/**
|
|
* Core Server
|
|
*/
|
|
|
|
import * as fs from 'fs';
|
|
import * as http from 'http';
|
|
import * as http2 from 'http2';
|
|
import * as https from 'https';
|
|
import * as zlib from 'zlib';
|
|
import * as Koa from 'koa';
|
|
import * as Router from 'koa-router';
|
|
import * as mount from 'koa-mount';
|
|
import * as compress from 'koa-compress';
|
|
import * as koaLogger from 'koa-logger';
|
|
import * as requestStats from 'request-stats';
|
|
import * as slow from 'koa-slow';
|
|
|
|
import activityPub from './activitypub';
|
|
import nodeinfo from './nodeinfo';
|
|
import wellKnown from './well-known';
|
|
import config from '../config';
|
|
import networkChart from '../chart/network';
|
|
import apiServer from './api';
|
|
import { sum } from '../prelude/array';
|
|
import User from '../models/user';
|
|
import Logger from '../misc/logger';
|
|
import { program } from '../argv';
|
|
|
|
export const serverLogger = new Logger('server', 'gray');
|
|
|
|
// Init app
|
|
const app = new Koa();
|
|
app.proxy = true;
|
|
|
|
if (!['production', 'test'].includes(process.env.NODE_ENV)) {
|
|
// Logger
|
|
app.use(koaLogger(str => {
|
|
serverLogger.info(str);
|
|
}));
|
|
|
|
// Delay
|
|
if (program.slow) {
|
|
app.use(slow({
|
|
delay: 3000
|
|
}));
|
|
}
|
|
}
|
|
|
|
// Compress response
|
|
app.use(compress({
|
|
flush: zlib.constants.Z_SYNC_FLUSH
|
|
}));
|
|
|
|
// HSTS
|
|
// 6months (15552000sec)
|
|
if (config.url.startsWith('https') && !config.disableHsts) {
|
|
app.use(async (ctx, next) => {
|
|
ctx.set('strict-transport-security', 'max-age=15552000; preload');
|
|
await next();
|
|
});
|
|
}
|
|
|
|
app.use(mount('/api', apiServer));
|
|
app.use(mount('/files', require('./file')));
|
|
app.use(mount('/proxy', require('./proxy')));
|
|
|
|
// Init router
|
|
const router = new Router();
|
|
|
|
// Routing
|
|
router.use(activityPub.routes());
|
|
router.use(nodeinfo.routes());
|
|
router.use(wellKnown.routes());
|
|
|
|
router.get('/verify-email/:code', async ctx => {
|
|
const user = await User.findOne({ emailVerifyCode: ctx.params.code });
|
|
|
|
if (user != null) {
|
|
ctx.body = 'Verify succeeded!';
|
|
ctx.status = 200;
|
|
|
|
User.update({ _id: user._id }, {
|
|
$set: {
|
|
emailVerified: true,
|
|
emailVerifyCode: null
|
|
}
|
|
});
|
|
} else {
|
|
ctx.status = 404;
|
|
}
|
|
});
|
|
|
|
// Register router
|
|
app.use(router.routes());
|
|
|
|
app.use(mount(require('./web')));
|
|
|
|
function createServer() {
|
|
if (config.https.isJust()) {
|
|
const opts = {
|
|
key: fs.readFileSync(config.https.get().key),
|
|
cert: fs.readFileSync(config.https.get().cert),
|
|
...config.https.get().ca.map<any>(path => ({ ca: fs.readFileSync(path) })).getOrElse({}),
|
|
allowHTTP1: true
|
|
};
|
|
return http2.createSecureServer(opts, app.callback()) as https.Server;
|
|
} else {
|
|
return http.createServer(app.callback());
|
|
}
|
|
}
|
|
|
|
// For testing
|
|
export const startServer = () => {
|
|
const server = createServer();
|
|
|
|
// Init stream server
|
|
require('./api/streaming')(server);
|
|
|
|
// Listen
|
|
server.listen(config.port);
|
|
|
|
return server;
|
|
};
|
|
|
|
export default () => new Promise(resolve => {
|
|
const server = createServer();
|
|
|
|
// Init stream server
|
|
require('./api/streaming')(server);
|
|
|
|
// Listen
|
|
server.listen(config.port, resolve);
|
|
|
|
//#region Network stats
|
|
let queue: any[] = [];
|
|
|
|
requestStats(server, (stats: any) => {
|
|
if (stats.ok) {
|
|
queue.push(stats);
|
|
}
|
|
});
|
|
|
|
// Bulk write
|
|
setInterval(() => {
|
|
if (queue.length == 0) return;
|
|
|
|
const requests = queue.length;
|
|
const time = sum(queue.map(x => x.time));
|
|
const incomingBytes = sum(queue.map(x => x.req.byets));
|
|
const outgoingBytes = sum(queue.map(x => x.res.byets));
|
|
queue = [];
|
|
|
|
networkChart.update(requests, time, incomingBytes, outgoingBytes);
|
|
}, 5000);
|
|
//#endregion
|
|
});
|