102 lines
3.5 KiB
JavaScript
102 lines
3.5 KiB
JavaScript
import { fastify } from 'fastify'
|
|
import { mongoose } from 'mongoose'
|
|
import { registerModels } from './models/index.js';
|
|
import * as Hooks from './hooks.js'
|
|
import * as AuthenticateRoutings from './routes/authenticate.js'
|
|
import * as SessionServerRoutings from './routes/session.js'
|
|
import * as AdvancedRoutings from './routes/advanced.js'
|
|
import { config } from './config.js'
|
|
import { readFileSync } from 'fs'
|
|
|
|
export const server = fastify({
|
|
logger: {
|
|
prettyPrint: true,
|
|
// level: 'error'
|
|
}
|
|
})
|
|
|
|
|
|
//export const logger = server.log
|
|
|
|
export const setup = async () => {
|
|
const mongooseClient = await mongoose.connect(config.database.url)
|
|
const models = registerModels(mongooseClient)
|
|
const publicKey = readFileSync(config.signing.public).toString()
|
|
const privateKey = readFileSync(config.signing.private).toString()
|
|
|
|
server.decorate('mongoose', mongooseClient)
|
|
server.decorate('models', models)
|
|
server.decorate('keys', { publicKey, privateKey })
|
|
|
|
config.custom.preHooks(server)
|
|
server.addHook('preHandler', Hooks.headerValidation)
|
|
|
|
config.custom.preRouting(server)
|
|
|
|
// Authserver routings
|
|
server.route(AuthenticateRoutings.authenticate)
|
|
server.route(AuthenticateRoutings.refresh)
|
|
server.route(AuthenticateRoutings.validate)
|
|
server.route(AuthenticateRoutings.invalidate)
|
|
server.route(AuthenticateRoutings.signout)
|
|
|
|
server.route(SessionServerRoutings.join)
|
|
server.route(SessionServerRoutings.hasJoined)
|
|
server.route(SessionServerRoutings.profile)
|
|
|
|
server.route(AdvancedRoutings.meta)
|
|
server.route(AdvancedRoutings.status)
|
|
|
|
config.custom.postRouting(server)
|
|
|
|
if(process.env["UNIT_TEST"] || process.env["DEVEL_FIRST_RUN"]) {
|
|
// Create a test player
|
|
await new models.Player({
|
|
username: 'test',
|
|
password: '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92',
|
|
email: 'i@lama.icu',
|
|
uuid: '098f6bcd-4621-3373-8ade-4e832627b4f6',
|
|
texture: {
|
|
skin: 'assets.lama.icu/textures/skin/steve.png',
|
|
cape: 'assets.lama.icu/textures/cape/default.png'
|
|
},
|
|
registerDate: Date.now(),
|
|
permissions: [{ node: 'login', allowed: true, duration: 0, eternal: true, startDate: Date.now(), highPriority: false }]
|
|
}).save()
|
|
}
|
|
}
|
|
|
|
const launch = async () => {
|
|
process.on('SIGINT', () => {
|
|
new Promise(shutdown)
|
|
})
|
|
|
|
await server.listen(config.server.port, config.server.url)
|
|
|
|
server.log.info("老色批世界树 > 基于 fastify 的高性能 HTTP 服务器已启动")
|
|
}
|
|
|
|
export const shutdown = async () => {
|
|
server.close()
|
|
mongoose.disconnect()
|
|
}
|
|
|
|
(async () => {
|
|
if(!process.env["UNIT_TEST"]) {
|
|
console.log(`
|
|
================================================================
|
|
__ _____ ______ __ __ _ __
|
|
/ / / ___// __ \\ \\/ /___ _____ _____/ /________ ______(_) /
|
|
/ / \\__ \\/ /_/ /\\ / __ \`/ __ \`/ __ / ___/ __ \`/ ___/ / /
|
|
/ /______/ / ____/ / / /_/ / /_/ / /_/ / / / /_/ (__ ) / /
|
|
/_____/____/_/ /_/\\__, /\\__, /\\__,_/_/ \\__,_/____/_/_/
|
|
/____//____/
|
|
================================================================\n`)
|
|
|
|
if(typeof PROGRAM_PRODUCTION === 'undefined') {
|
|
console.warn("⚠ 警告: 您运行的不是正式版本,可能会不稳定,仅限开发环境运行!\n")
|
|
}
|
|
await setup()
|
|
await launch()
|
|
}
|
|
})() |