1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-12-24 17:23:23 +03:00

refactor(server): http-server with over http enabled

This commit is contained in:
Divlo 2023-04-03 20:58:02 +02:00
parent 907fb1a129
commit d261967920
No known key found for this signature in database
GPG Key ID: 8F9478F220CE65E9
4 changed files with 79 additions and 61 deletions

View File

@ -128,7 +128,7 @@
"json": "10.0.0",
"lint-staged": "13.0.3",
"nodemon": "2.0.19",
"prettier": "2.7.1",
"prettier": "2.8.7",
"resolve-tspaths": "0.8.8",
"semver": "7.3.5",
"shx": "0.3.3",

View File

@ -10,15 +10,15 @@ import { LogHelper } from '@/helpers/log-helper'
import { StringHelper } from '@/helpers/string-helper'
import type { APIOptions } from '@/core/http-server/http-server'
const querystringSchema = Type.Object({
domain: Type.String(),
skill: Type.String()
})
const getDownloadsSchema = {
querystring: Type.Object({
domain: Type.String(),
skill: Type.String()
})
} satisfies FastifySchema
type QuerystringSchema = Static<typeof querystringSchema>
const schema: FastifySchema = {
querystring: querystringSchema
interface GetDownloadsSchema {
querystring: Static<typeof getDownloadsSchema.querystring>
}
export const getDownloads: FastifyPluginAsync<APIOptions> = async (
@ -26,11 +26,11 @@ export const getDownloads: FastifyPluginAsync<APIOptions> = async (
options
) => {
fastify.route<{
Querystring: QuerystringSchema
Querystring: GetDownloadsSchema['querystring']
}>({
method: 'GET',
url: `/api/${options.apiVersion}/downloads`,
schema,
schema: getDownloadsSchema,
handler: async (request, reply) => {
LogHelper.title('GET /downloads')

View File

@ -16,29 +16,31 @@ export const getInfo: FastifyPluginAsync<APIOptions> = async (
fastify,
options
) => {
fastify.get(`/api/${options.apiVersion}/info`, (_request, reply) => {
LogHelper.title('GET /info')
fastify.route({
method: 'GET',
url: `/api/${options.apiVersion}/info`,
handler: async (_request, reply) => {
LogHelper.title('GET /info')
const message = 'Information pulled.'
LogHelper.success(message)
const message = 'Information pulled.'
LogHelper.success(message)
reply.send({
success: true,
status: 200,
code: 'info_pulled',
message,
after_speech: HAS_AFTER_SPEECH,
logger: HAS_LOGGER,
stt: {
enabled: HAS_STT,
provider: STT_PROVIDER
},
tts: {
enabled: HAS_TTS,
provider: TTS_PROVIDER
},
version
})
reply.send({
success: true,
status: 200,
code: 'info_pulled',
message,
after_speech: HAS_AFTER_SPEECH,
logger: HAS_LOGGER,
stt: {
enabled: HAS_STT,
provider: STT_PROVIDER
},
tts: {
enabled: HAS_TTS,
provider: TTS_PROVIDER
},
version
})
}
})
}

View File

@ -1,17 +1,20 @@
import { join } from 'node:path'
import Fastify from 'fastify'
import Fastify, { FastifySchema } from 'fastify'
import fastifyStatic from '@fastify/static'
import { Type } from '@sinclair/typebox'
import type { Static } from '@sinclair/typebox'
import { version } from '@@/package.json'
import { LEON_NODE_ENV, HAS_LOGGER } from '@/constants'
import { LEON_NODE_ENV, HAS_LOGGER, HAS_OVER_HTTP } from '@/constants'
import { LogHelper } from '@/helpers/log-helper'
import { DateHelper } from '@/helpers/date-helper'
import { corsMidd } from '@/core/http-server/plugins/cors'
import { otherMidd } from '@/core/http-server/plugins/other'
import { infoPlugin } from '@/core/http-server/api/info'
import { downloadsPlugin } from '@/core/http-server/api/downloads'
// import { keyMidd } from '@/core/http-server/plugins/key'
import { keyMidd } from '@/core/http-server/plugins/key'
import { NLU, BRAIN } from '@/core'
const API_VERSION = 'v1'
@ -19,6 +22,16 @@ export interface APIOptions {
apiVersion: string
}
const postQuerySchema = {
body: Type.Object({
utterance: Type.String()
})
} satisfies FastifySchema
interface PostQuerySchema {
body: Static<typeof postQuerySchema.body>
}
export default class HTTPServer {
private static instance: HTTPServer
@ -54,9 +67,6 @@ export default class HTTPServer {
const sLogger = !HAS_LOGGER ? 'disabled' : 'enabled'
LogHelper.info(`Collaborative logger ${sLogger}`)
// TODO
// await addProvider('1')
await this.bootstrap()
}
@ -76,37 +86,43 @@ export default class HTTPServer {
this.fastify.register(infoPlugin, { apiVersion: API_VERSION })
this.fastify.register(downloadsPlugin, { apiVersion: API_VERSION })
// TODO: HTTP API
/*if (HAS_OVER_HTTP) {
server.fastify.register((instance, opts, next) => {
if (HAS_OVER_HTTP) {
this.fastify.register((instance, _opts, next) => {
instance.addHook('preHandler', keyMidd)
instance.post('/api/query', async (request, reply) => {
const { utterance } = request.body
instance.route<{
Body: PostQuerySchema['body']
}>({
method: 'POST',
url: '/api/query',
schema: postQuerySchema,
handler: async (request, reply) => {
const { utterance } = request.body
try {
// TODO
BRAIN.isMuted = true
const data = await mainProvider.nlu.process(utterance)
try {
BRAIN.isMuted = true
const data = await NLU.process(utterance)
reply.send({
...data,
success: true
})
} catch (e) {
reply.statusCode = 500
reply.send({
message: e.message,
success: false
})
reply.send({
...data,
success: true
})
} catch (error) {
const message = error instanceof Error ? error.message : error
reply.statusCode = 500
reply.send({
message,
success: false
})
}
}
})
server.generateSkillsRoutes(instance)
// server.generateSkillsRoutes(instance)
next()
})
}*/
}
try {
await this.listen()