-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
75 lines (65 loc) · 2.31 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { join } from 'node:path'
import type { FastifyInstance } from 'fastify'
import { app as platformaticService, Stackable, buildStackable as serviceBuildStackable } from '@platformatic/service'
import { ConfigManager } from '@platformatic/config'
import type { StackableInterface } from '@platformatic/config'
import fastifyUser from 'fastify-user'
import fastifyStatic from '@fastify/static'
import { schema } from './lib/schema.js'
import { Generator } from './lib/generator.js'
import { AiWarpConfig } from './config.js'
import warpPlugin from './plugins/warp.js'
import authPlugin from './plugins/auth.js'
import apiPlugin from './plugins/api.js'
import rateLimitPlugin from './plugins/rate-limiting.js'
export interface AiWarpMixin {
platformatic: {
configManager: ConfigManager<AiWarpConfig>
config: AiWarpConfig
}
}
type AiGenerator = new () => Generator
async function buildStackable (opts: { config: string }): Promise<StackableInterface> {
// eslint-disable-next-line
return await serviceBuildStackable(opts, stackable)
}
const stackable: Stackable<AiWarpConfig, AiGenerator> = {
async app (app, opts) {
const fastify = app as unknown as FastifyInstance & AiWarpMixin
const { config } = fastify.platformatic
// @ts-expect-error
await fastify.register(fastifyUser as any, config.auth)
await fastify.register(authPlugin, opts)
if (config.showAiWarpHomepage !== undefined && config.showAiWarpHomepage) {
await fastify.register(fastifyStatic, {
root: join(import.meta.dirname, 'static'),
wildcard: false
})
}
await fastify.register(platformaticService, opts)
await fastify.register(warpPlugin, opts) // needs to be registered here for fastify.ai to be decorated
await fastify.register(rateLimitPlugin, opts)
await fastify.register(apiPlugin, opts)
},
configType: 'ai-warp-app',
schema,
Generator,
configManagerConfig: {
schema,
envWhitelist: ['PORT', 'HOSTNAME'],
allowToWatch: ['.env'],
schemaOptions: {
useDefaults: true,
coerceTypes: true,
allErrors: true,
strict: false
},
async transformConfig () {}
},
buildStackable
}
// break Fastify encapsulation
// @ts-expect-error
stackable.app[Symbol.for('skip-override')] = true
export default stackable
export { Generator, schema, buildStackable }