Skip to content

Commit

Permalink
Update to Platformatic v2 (#116)
Browse files Browse the repository at this point in the history
* Update to Platformatic v2

Signed-off-by: Matteo Collina <[email protected]>

* fixup

Signed-off-by: Matteo Collina <[email protected]>

* fixup

Signed-off-by: Matteo Collina <[email protected]>

---------

Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina authored Sep 24, 2024
1 parent 7bc8772 commit 5dfa466
Show file tree
Hide file tree
Showing 7 changed files with 2,155 additions and 947 deletions.
38 changes: 19 additions & 19 deletions config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ export interface AiWarpConfig {
logger?:
| boolean
| {
level?: string;
level: (
| ("fatal" | "error" | "warn" | "info" | "debug" | "trace" | "silent")
| {
[k: string]: unknown;
}
) &
string;
transport?:
| {
target?: string;
Expand Down Expand Up @@ -61,6 +67,9 @@ export interface AiWarpConfig {
};
[k: string]: unknown;
};
loggerInstance?: {
[k: string]: unknown;
};
serializerOpts?: {
schema?: {
[k: string]: unknown;
Expand All @@ -80,7 +89,9 @@ export interface AiWarpConfig {
requestIdLogLabel?: string;
jsonShorthand?: boolean;
trustProxy?: boolean | string | string[] | number;
http2?: boolean;
https?: {
allowHTTP1?: boolean;
key:
| string
| {
Expand Down Expand Up @@ -149,11 +160,13 @@ export interface AiWarpConfig {
defaultMetrics?: {
enabled: boolean;
};
prefix?: string;
auth?: {
username: string;
password: string;
};
labels?: {
[k: string]: string;
};
};
telemetry?: OpenTelemetry;
watch?:
Expand All @@ -168,6 +181,7 @@ export interface AiWarpConfig {
| boolean
| string;
$schema?: string;
module?: string;
service?: {
openapi?:
| {
Expand Down Expand Up @@ -205,24 +219,10 @@ export interface AiWarpConfig {
path?: string;
schema?: string;
url?: string;
fullResponse?: boolean;
fullRequest?: boolean;
validateResponse?: boolean;
}[];
versions?: {
/**
* The path to the directory containing the versions mappers
*/
dir: string;
configs: {
version: string;
openapi?: {
prefix?: string;
path?: string;
};
plugins?: {
[k: string]: unknown;
};
}[];
};
module?: string;
showAiWarpHomepage?: boolean;
aiProvider:
| {
Expand Down
83 changes: 51 additions & 32 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { join } from 'node:path'
import { platformaticService, Stackable } from '@platformatic/service'
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 fastifyPlugin from 'fastify-plugin'
import fastifyStatic from '@fastify/static'
import { schema } from './lib/schema.js'
import { Generator } from './lib/generator.js'
Expand All @@ -11,44 +13,61 @@ import authPlugin from './plugins/auth.js'
import apiPlugin from './plugins/api.js'
import rateLimitPlugin from './plugins/rate-limiting.js'

const stackable: Stackable<AiWarpConfig> = async function (fastify, opts) {
const { config } = fastify.platformatic
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')
})
export interface AiWarpMixin {
platformatic: {
configManager: ConfigManager<AiWarpConfig>
config: AiWarpConfig
}
}

await fastify.register(platformaticService, opts)
type AiGenerator = new () => Generator

await fastify.register(warpPlugin, opts) // needs to be registered here for fastify.ai to be decorated
const stackable: Stackable<AiWarpConfig, AiGenerator> = {
async app (app, opts) {
const fastify = app as unknown as FastifyInstance & AiWarpMixin
const { config } = fastify.platformatic
await fastify.register(fastifyUser as any, config.auth)
await fastify.register(authPlugin, opts)

await fastify.register(rateLimitPlugin, opts)
await fastify.register(apiPlugin, opts)
}
if (config.showAiWarpHomepage !== undefined && config.showAiWarpHomepage) {
await fastify.register(fastifyStatic, {
root: join(import.meta.dirname, 'static'),
wildcard: false
})
}

stackable.configType = 'ai-warp-app'
stackable.schema = schema
stackable.Generator = Generator
stackable.configManagerConfig = {
schema,
envWhitelist: ['PORT', 'HOSTNAME'],
allowToWatch: ['.env'],
schemaOptions: {
useDefaults: true,
coerceTypes: true,
allErrors: true,
strict: 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)
},
async transformConfig () {}
configType: 'ai-warp-app',
schema,
Generator,
configManagerConfig: {
schema,
envWhitelist: ['PORT', 'HOSTNAME'],
allowToWatch: ['.env'],
schemaOptions: {
useDefaults: true,
coerceTypes: true,
allErrors: true,
strict: false
},
async transformConfig () {}
}
}

// break Fastify encapsulation
// @ts-expect-error
stackable[Symbol.for('skip-override')] = true
stackable.app[Symbol.for('skip-override')] = true

async function buildStackable (opts: { config: string }): Promise<StackableInterface> {
// eslint-disable-next-line
return await serviceBuildStackable(opts, stackable.app)
}

export default fastifyPlugin(stackable)
export { Generator, schema }
export default stackable
export { Generator, schema, buildStackable }
6 changes: 3 additions & 3 deletions lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ if (import.meta.url.endsWith('.js')) {
pkgJsonPath = join(dirname(fileURLToPath(import.meta.url)), '..', 'package.json')
}

const pkgJson: any = JSON.parse(readFileSync(pkgJsonPath, 'utf8'))
const pkgJson: { version: string } = JSON.parse(readFileSync(pkgJsonPath, 'utf8'))

const aiWarpSchema = {
...schema.schema,
$id: 'ai-warp',
$id: `https://schemas.platformatic.com/@platformatic/ai-warp/${pkgJson.version}.json`,
title: 'Ai Warp Config',
version: pkgJson.version,
properties: {
Expand Down Expand Up @@ -317,7 +317,7 @@ const aiWarpSchema = {
required: [
'aiProvider'
]
} as any
}

export { aiWarpSchema as schema }

Expand Down
Loading

0 comments on commit 5dfa466

Please sign in to comment.