优化项目结构,优化构建脚本,优化README.MD
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Qumolama.d
2022-05-04 21:04:44 +08:00
parent 99d304899d
commit bd8b866f07
6 changed files with 113 additions and 69 deletions

View File

@ -1,23 +1,38 @@
const defaultPrehandler = async function (req, rep) { }
export const config = {
database: {
database: { // MONGODB IS THE BEST DATABASE
url: 'mongodb://localhost:27017/yggdrasil?readPreference=primary&appname=MongoDB%20Compass&directConnection=true&ssl=false',
},
server: {
port: 3000,
url: '',
port: 3000
},
signing: {
signing: { // 签名材质信息使用
public: '/path/to/public.pem',
private: '/path/to/private.key'
},
custom: {
overridePrehandler: (url) => {
return defaultPrehandler
override: {
preHandler: {
/*
"/example/route": async function(req, rep) {}
*/
},
handler: {
/*
"/example/route": async function(req, rep) {}
*/
}
},
preHooks: (fastify) => {},
preRouting: (fastify) => {},
postRouting: (fastify) => {},
preHooks: (fastify) => {}, // 在这里添加自定义hook
preRouting: (fastify) => {}, // 在这里添加自定义路由
postRouting: (fastify) => {}, // IDK what u want to do at here
}
}
export function getOverrideHandler(route) {
return config.custom.override.handler[route]
}
export function getOverridePreHandler(route) {
return config.custom.override.preHandler[route]
}

View File

@ -3,6 +3,21 @@ import { mongoose } from 'mongoose'
import { registerModels } from './models/index.js';
import * as Hooks from './hooks.js'
import * as AuthenticateRoutings from './routes/authenticate.js'
import { config } from './config.js'
console.log(`
================================================================
__ _____ ______ __ __ _ __
/ / / ___// __ \\ \\/ /___ _____ _____/ /________ ______(_) /
/ / \\__ \\/ /_/ /\\ / __ \`/ __ \`/ __ / ___/ __ \`/ ___/ / /
/ /______/ / ____/ / / /_/ / /_/ / /_/ / / / /_/ (__ ) / /
/_____/____/_/ /_/\\__, /\\__, /\\__,_/_/ \\__,_/____/_/_/
/____//____/
================================================================\n`)
if(typeof PROGRAM_PRODUCTION === 'undefined') {
console.warn("⚠ 警告: 您运行的不是正式版本,可能会不稳定,仅限开发环境运行!\n")
}
export const server = fastify({
logger: {
@ -14,10 +29,6 @@ export const server = fastify({
//export const logger = server.log
;(async () => {
const { config } = await import('./config.js')
server.decorate('conf', config)
const mongooseClient = await mongoose.connect(config.database.url)
const models = registerModels(mongooseClient)
@ -28,11 +39,14 @@ export const server = fastify({
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)
config.custom.postRouting(server)
/*

View File

@ -1,6 +1,7 @@
import * as PlayerModel from '../models/player.js'
import { createHash } from 'crypto'
import { generateToken, uuidToNoSymboUUID } from '../generator.js'
import { getOverrideHandler, getOverridePreHandler } from '../config.js'
export const authenticate = {
method: 'POST',
@ -46,7 +47,7 @@ export const authenticate = {
},
"availableProfiles": {
"type": "array",
"items": [{...PlayerModel.PlayerSeriliazationSchema}]
"items": [PlayerModel.PlayerSeriliazationSchema]
},
"selectedProfile": PlayerModel.PlayerSeriliazationSchema,
"user": PlayerModel.PlayerAccountSerializationSchema
@ -54,10 +55,8 @@ export const authenticate = {
}
}
},
preHandler: async function(req, rep) {
this.conf.custom.overridePrehandler('/authserver/authenticate', req, rep)
},
handler: async function (req, rep) {
preHandler: getOverridePreHandler("/authserver/authenticate"),
handler: getOverrideHandler("/authserver/authenticate") ?? async function (req, rep) {
let { username, password, clientToken, requestUser, agent } = req.body
if(!username || !password || !agent || agent.name.toLowerCase() !== 'minecraft') {
@ -185,10 +184,8 @@ export const refresh = {
}
}
},
preHandler: async function(req, rep) {
this.conf.custom.overridePrehandler('/authserver/refresh', req, rep)
},
handler: async function (req, rep) {
preHandler: getOverridePreHandler("/authserver/refresh"),
handler: getOverrideHandler("/authserver/authenticate") ?? async function (req, rep) {
const { accessToken, clientToken, requestUser, selectedProfile } = req.body
const query = {
@ -320,10 +317,8 @@ export const validate = {
}
}
},
preHandler: async function(req, rep) {
this.conf.custom.overridePrehandler('/authserver/validate', req, rep)
},
handler: async function (req, rep) {
preHandler: getOverridePreHandler("/authserver/validate"),
handler: getOverrideHandler("/authserver/validate") ?? async function (req, rep) {
const { accessToken, clientToken } = req.body
const query = {
@ -380,10 +375,8 @@ export const invalidate = {
}
}
,
preHandler: async function(req, rep) {
this.conf.custom.overridePrehandler('/authserver/invalidate', req, rep)
},
handler: async function (req, rep) {
preHandler: getOverridePreHandler("/authserver/invalidate"),
handler: getOverrideHandler("/authserver/authenticate") ?? async function (req, rep) {
const { accessToken } = req.body
const { modifiedCount } = await this.models.Token.updateOne({
@ -428,10 +421,8 @@ export const signout = {
}
}
},
preHandler: async function(req, rep) {
this.conf.custom.overridePrehandler('/authserver/logout', req, rep)
},
handler: async function (req, rep) {
preHandler: getOverridePreHandler("/authserver/signout"),
handler: getOverrideHandler("/authserver/signout") ?? async function (req, rep) {
const { username, password } = req.body
const player = await this.models.Player.findOne({ email: username, password: createHash('sha256').update(password).digest().toString('hex').toLowerCase() })