From b70c21490e59e35da22a281e676abc897f58ea56 Mon Sep 17 00:00:00 2001 From: Louis Date: Sun, 31 Dec 2023 20:09:45 +0700 Subject: [PATCH] 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. --- docs/docs/guides/05-using-server/01-server.md | 2 +- docs/openapi/jan.yaml | 14 +++++++------- server/index.ts | 17 +++++++++++++++++ server/package.json | 2 ++ 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/docs/docs/guides/05-using-server/01-server.md b/docs/docs/guides/05-using-server/01-server.md index ad53455332..aa5bf8d3a1 100644 --- a/docs/docs/guides/05-using-server/01-server.md +++ b/docs/docs/guides/05-using-server/01-server.md @@ -27,6 +27,6 @@ Jan runs on port `1337` by default, but this can be changed in Settings. Check out the [API Reference](/api-reference) for more information on the API endpoints. ``` -curl https://localhost:1337/v1/chat/completions +curl http://localhost:1337/v1/chat/completions ``` diff --git a/docs/openapi/jan.yaml b/docs/openapi/jan.yaml index 6a59647e51..b7b6bd3c17 100644 --- a/docs/openapi/jan.yaml +++ b/docs/openapi/jan.yaml @@ -14,7 +14,7 @@ license: name: AGPLv3 url: "https://github.com/janhq/nitro/blob/main/LICENSE" servers: - - url: "https://localhost:1337/v1/" + - url: "http://localhost:1337/v1/" tags: - name: Models description: List and describe the various models available in the API. @@ -100,7 +100,7 @@ paths: x-codeSamples: - lang: cURL source: | - curl https://localhost:1337/v1/models + curl http://localhost:1337/v1/models post: operationId: downloadModel tags: @@ -118,7 +118,7 @@ paths: x-codeSamples: - lang: cURL source: | - curl -X POST https://localhost:1337/v1/models + curl -X POST http://localhost:1337/v1/models "/models/{model_id}": get: operationId: retrieveModel @@ -149,7 +149,7 @@ paths: x-codeSamples: - lang: cURL source: | - curl https://localhost:1337/v1/models/{model_id} + curl http://localhost:1337/v1/models/{model_id} delete: operationId: deleteModel tags: @@ -178,7 +178,7 @@ paths: x-codeSamples: - lang: cURL source: | - curl -X DELETE https://localhost:1337/v1/models/{model_id} + curl -X DELETE http://localhost:1337/v1/models/{model_id} "/models/{model_id}/start": put: operationId: startModel @@ -206,7 +206,7 @@ paths: x-codeSamples: - lang: cURL source: | - curl -X PUT https://localhost:1337/v1/models/{model_id}/start + curl -X PUT http://localhost:1337/v1/models/{model_id}/start "/models/{model_id}/stop": put: operationId: stopModel @@ -233,7 +233,7 @@ paths: x-codeSamples: - lang: cURL source: | - curl -X PUT https://localhost:1337/v1/models/{model_id}/stop + curl -X PUT http://localhost:1337/v1/models/{model_id}/stop /threads: post: operationId: createThread diff --git a/server/index.ts b/server/index.ts index a5fbfe0e6a..bc031305e4 100644 --- a/server/index.ts +++ b/server/index.ts @@ -10,6 +10,23 @@ 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"), { diff --git a/server/package.json b/server/package.json index 2d350a3178..36fdc124f0 100644 --- a/server/package.json +++ b/server/package.json @@ -19,6 +19,8 @@ "dependencies": { "@fastify/cors": "^8.4.2", "@fastify/static": "^6.12.0", + "@fastify/swagger": "^8.13.0", + "@fastify/swagger-ui": "^2.0.1", "@janhq/core": "link:./core", "dotenv": "^16.3.1", "fastify": "^4.24.3",