添加 /api 接口,优化config中的注释,更新README.MD
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
具体有多快呢?登录处理从数据包发出到接收到服务端响应仅需要 __***6ms***__(根据机器不同可能会有浮动,以实际情况为准)!
|
具体有多快呢?登录处理从数据包发出到接收到服务端响应仅需要 __***6ms***__(根据机器不同可能会有浮动,以实际情况为准)!
|
||||||
|
|
||||||
**暂时别管这个 build 是不是 failed。因为某些原因 ci 跑完了一直都是超时,每次提交之前我会在本地进行测试直到修复**
|
**暂时别管这个 build 是不是 failed。因为某些原因 ci 跑不了,每次提交之前我会在本地进行测试直到修复**
|
||||||
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
@ -23,7 +23,7 @@ export const config = {
|
|||||||
private: 'private.key'
|
private: 'private.key'
|
||||||
},
|
},
|
||||||
custom: {
|
custom: {
|
||||||
override: {
|
override: { // 使用这个来覆写一个路径的处理器,使用function而不是箭头函数可以用this访问fastify server
|
||||||
preHandler: {
|
preHandler: {
|
||||||
/*
|
/*
|
||||||
"/example/route": async function(req, rep) {}
|
"/example/route": async function(req, rep) {}
|
||||||
@ -38,7 +38,7 @@ export const config = {
|
|||||||
},
|
},
|
||||||
preHooks: (fastify) => {}, // 在这里添加自定义hook
|
preHooks: (fastify) => {}, // 在这里添加自定义hook
|
||||||
preRouting: (fastify) => {}, // 在这里添加自定义路由
|
preRouting: (fastify) => {}, // 在这里添加自定义路由
|
||||||
postRouting: (fastify) => {}, // IDK what u want to do at here
|
postRouting: (fastify) => {}, // 我也不知道你在这里写了有啥用...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
30
src/index.js
30
src/index.js
@ -5,6 +5,7 @@ import * as Hooks from './hooks.js'
|
|||||||
import * as AuthenticateRoutings from './routes/authenticate.js'
|
import * as AuthenticateRoutings from './routes/authenticate.js'
|
||||||
import * as SessionServerRoutings from './routes/session.js'
|
import * as SessionServerRoutings from './routes/session.js'
|
||||||
import * as AdvancedRoutings from './routes/advanced.js'
|
import * as AdvancedRoutings from './routes/advanced.js'
|
||||||
|
import * as APIRoutings from './routes/api.js'
|
||||||
import { config } from './config.js'
|
import { config } from './config.js'
|
||||||
import { readFileSync } from 'fs'
|
import { readFileSync } from 'fs'
|
||||||
|
|
||||||
@ -15,9 +16,6 @@ export const server = fastify({
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
//export const logger = server.log
|
|
||||||
|
|
||||||
export const setup = async () => {
|
export const setup = async () => {
|
||||||
const mongooseClient = await mongoose.connect(config.database.url)
|
const mongooseClient = await mongoose.connect(config.database.url)
|
||||||
const models = registerModels(mongooseClient)
|
const models = registerModels(mongooseClient)
|
||||||
@ -47,22 +45,24 @@ export const setup = async () => {
|
|||||||
server.route(AdvancedRoutings.meta)
|
server.route(AdvancedRoutings.meta)
|
||||||
server.route(AdvancedRoutings.status)
|
server.route(AdvancedRoutings.status)
|
||||||
|
|
||||||
|
server.route(APIRoutings.profiles)
|
||||||
|
|
||||||
config.custom.postRouting(server)
|
config.custom.postRouting(server)
|
||||||
|
|
||||||
if(process.env["UNIT_TEST"] || process.env["DEVEL_FIRST_RUN"]) {
|
if(process.env["UNIT_TEST"] || process.env["DEVEL_FIRST_RUN"]) {
|
||||||
// Create a test player
|
// Create a test player
|
||||||
await new models.Player({
|
await new models.Player({
|
||||||
username: 'test',
|
username: 'test',
|
||||||
password: '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92',
|
password: '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92',
|
||||||
email: 'i@lama.icu',
|
email: 'i@lama.icu',
|
||||||
uuid: '098f6bcd-4621-3373-8ade-4e832627b4f6',
|
uuid: '098f6bcd-4621-3373-8ade-4e832627b4f6',
|
||||||
texture: {
|
texture: {
|
||||||
skin: 'assets.lama.icu/textures/skin/steve.png',
|
skin: 'assets.lama.icu/textures/skin/steve.png',
|
||||||
cape: 'assets.lama.icu/textures/cape/default.png'
|
cape: 'assets.lama.icu/textures/cape/default.png'
|
||||||
},
|
},
|
||||||
registerDate: Date.now(),
|
registerDate: Date.now(),
|
||||||
permissions: [{ node: 'login', allowed: true, duration: 0, eternal: true, startDate: Date.now(), highPriority: false }]
|
permissions: [{ node: 'login', allowed: true, duration: 0, eternal: true, startDate: Date.now(), highPriority: false }]
|
||||||
}).save()
|
}).save()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
48
src/routes/api.js
Normal file
48
src/routes/api.js
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
import { getOverrideHandler, getOverridePreHandler } from "../config.js"
|
||||||
|
import { uuidToNoSymboUUID } from "../generator.js"
|
||||||
|
|
||||||
|
export const profiles = {
|
||||||
|
method: 'POST',
|
||||||
|
url: '/api/profiles/minecraft',
|
||||||
|
schema: {
|
||||||
|
body: {
|
||||||
|
type: 'array',
|
||||||
|
items: {
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
response: {
|
||||||
|
200: {
|
||||||
|
type: 'array',
|
||||||
|
items: {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
id: {
|
||||||
|
type: 'string'
|
||||||
|
},
|
||||||
|
name: {
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
preHandler: getOverridePreHandler('/api/profiles/minecraft') ?? async function(req, rep) {
|
||||||
|
if(req.body.length >= 3) {
|
||||||
|
await rep.code(400).send({
|
||||||
|
error: "IllegalArgumentException",
|
||||||
|
errorMessage: "请求内容过长, 最大请求玩家数: 2",
|
||||||
|
cause: "请求内容过长, 最大请求玩家数: 2"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
handler: getOverrideHandler('/api/profiles/minecraft') ?? async function (req, rep) {
|
||||||
|
const { body } = req
|
||||||
|
const profiles = await this.models.Player.find({ username: { $in: body } })
|
||||||
|
return await rep.code(200).send(profiles.map(profile => ({
|
||||||
|
id: uuidToNoSymboUUID(profile.uuid),
|
||||||
|
name: profile.username
|
||||||
|
})))
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user