1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-09-17 13:07:08 +03:00

refactor(server): make use of consts for HTTP API matters

This commit is contained in:
louistiti 2022-09-04 14:17:06 +08:00
parent d05d572b70
commit 819e57a2c2
No known key found for this signature in database
GPG Key ID: 7ECA3DD523793FE6
3 changed files with 9 additions and 2 deletions

View File

@ -24,3 +24,7 @@ export const HAS_STT = process.env['LEON_STT'] === 'true'
export const STT_PROVIDER = process.env['LEON_STT_PROVIDER']
export const HAS_TTS = process.env['LEON_TTS'] === 'true'
export const TTS_PROVIDER = process.env['LEON_TTS_PROVIDER']
export const HAS_OVER_HTTP = process.env['LEON_OVER_HTTP'] === 'true'
export const HTTP_API_KEY = process.env['LEON_HTTP_API_KEY']
export const HTTP_API_LANG = process.env['LEON_HTTP_API_LANG']

View File

@ -1,6 +1,8 @@
import { HTTP_API_KEY } from '@/constants'
const keyMidd = async (request, reply) => {
const apiKey = request.headers['x-api-key']
if (!apiKey || apiKey !== process.env.LEON_HTTP_API_KEY) {
if (!apiKey || apiKey !== HTTP_API_KEY) {
reply.statusCode = 401
reply.send({
message: 'Unauthorized, please check the HTTP API key is correct',

View File

@ -6,6 +6,7 @@ import { join } from 'path'
import { version } from '@@/package.json'
import { endpoints } from '@@/core/skills-endpoints.json'
import {
HAS_OVER_HTTP,
HAS_STT,
HAS_TTS,
HOST,
@ -327,7 +328,7 @@ server.bootstrap = async () => {
server.fastify.register(infoPlugin, { apiVersion })
server.fastify.register(downloadsPlugin, { apiVersion })
if (process.env.LEON_OVER_HTTP === 'true') {
if (HAS_OVER_HTTP) {
server.fastify.register((instance, opts, next) => {
instance.addHook('preHandler', keyMidd)