jan/server/index.ts
Louis b70c21490e
feat: add swagger /docs to localhost:1337 (#1268)
## Description
As a user, I would like to see all supported APIs and test them out. Local swagger hosted at /docs would be helpful.
2023-12-31 20:09:45 +07:00

61 lines
1.4 KiB
TypeScript

import fastify from "fastify";
import dotenv from "dotenv";
import { v1Router } from "@janhq/core/node";
import path from "path";
dotenv.config();
const JAN_API_HOST = process.env.JAN_API_HOST || "0.0.0.0";
const JAN_API_PORT = Number.parseInt(process.env.JAN_API_PORT || "1337");
const server = fastify();
server.register(require("@fastify/cors"), {});
server.register(require("@fastify/swagger"), {
mode: "static",
specification: {
path: "./../docs/openapi/jan.yaml",
baseDir: "./../docs/openapi",
},
});
server.register(require("@fastify/swagger-ui"), {
routePrefix: "/docs",
baseDir: path.join(__dirname, "../..", "./docs/openapi"),
uiConfig: {
docExpansion: "full",
deepLinking: false,
},
staticCSP: true,
transformSpecificationClone: true,
});
server.register(
(childContext, _, done) => {
childContext.register(require("@fastify/static"), {
root:
process.env.EXTENSION_ROOT ||
path.join(require("os").homedir(), "jan", "extensions"),
wildcard: false,
});
done();
},
{ prefix: "extensions" }
);
server.register(v1Router, { prefix: "/v1" });
export const startServer = () => {
server
.listen({
port: JAN_API_PORT,
host: JAN_API_HOST,
})
.then(() => {
console.log(
`JAN API listening at: http://${JAN_API_HOST}:${JAN_API_PORT}`
);
});
};
export const stopServer = () => {
server.close();
};