溫馨提示×

Swagger在Ubuntu上如何配置

小樊
53
2025-05-05 13:46:06
欄目: 智能運維

在Ubuntu上配置Swagger通常涉及以下幾個步驟:

安裝Swagger工具

  • 基于Debian的系統(如Ubuntu)

    1. 更新包列表:
      sudo apt-get update
      
    2. 安裝Swagger工具:
      sudo apt-get install swagger-jsdoc swagger-ui-express
      
  • 基于Red Hat的系統(如Fedora)

    1. 更新包列表:
      sudo dnf update
      
    2. 安裝Swagger工具:
      sudo dnf install swagger-jsdoc swagger-ui-express
      
  • 使用Node.js環境

    1. 安裝Node.js和npm(如果尚未安裝):
      sudo apt-get install nodejs npm
      
    2. 使用npm安裝Swagger工具:
      npm install swagger-jsdoc swagger-ui-express --save-dev
      

配置Swagger

  1. 創建一個Swagger配置文件,通常命名為swagger.jsonswagger.yaml。這個文件定義了API的元數據,包括API的路徑、操作、參數、模型等。

    以下是一個簡單的swagger.json示例:

    {
      "swagger": "2.0",
      "info": {
        "description": "Sample API",
        "version": "1.0.0"
      },
      "basePath": "/api",
      "paths": {
        "/users": {
          "get": {
            "summary": "List all users",
            "responses": {
              "200": {
                "description": "An array of users",
                "schema": {
                  "type": "array",
                  "items": {
                    "ref": "#/definitions/User"
                  }
                }
              }
            }
          }
        }
      },
      "definitions": {
        "User": {
          "type": "object",
          "properties": {
            "id": {
              "type": "integer"
            },
            "name": {
              "type": "string"
            }
          }
        }
      }
    }
    

集成Swagger到你的應用

如果你使用的是Express框架,可以按照以下方式集成Swagger UI:

const express = require('express');
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
const app = express();

app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));

// ... 其他中間件和路由

const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

運行你的應用

運行你的Node.js應用,然后訪問http://localhost:3000/api-docs(或者你設置的相應端口),你應該能看到Swagger UI界面,其中包含了你的API文檔。

使用Swagger Editor和Swagger UI進行API監控和測試

  1. 安裝Node.js和npm(如果尚未安裝):

    sudo apt update
    sudo apt install nodejs npm
    
  2. 安裝Swagger Editor

    • 方法一:從官網下載并解壓Swagger Editor:
      wget https://github.com/swagger-api/swagger-editor/archive/refs/tags/v3.16.1.tar.gztar -xvf v3.16.1.tar.gz
      cd swagger-editor-3.16.1
      npm install
      npm install -g http-server
      http-server -p 8080
      
    • 方法二:使用npm全局安裝Swagger Editor:
      npm install -g swagger-editor
      
  3. 安裝Swagger UI

    • 方法一:從官網下載并解壓Swagger UI:
      wget https://github.com/swagger-api/swagger-ui/archive/refs/tags/v3.48.0.tar.gztar -xvf v3.48.0.tar.gz
      cd swagger-ui-3.48.0
      npm install
      npm install -g express node index.js
      
    • 方法二:使用npm全局安裝Swagger UI:
      npm install -g swagger-ui
      
  4. 配置Swagger Editor和Swagger UI

    • Swagger Editor:啟動后訪問http://localhost:8080,使用你保存的Swagger YAML文件進行API文檔的編輯和查看。
    • Swagger UI:啟動后訪問http://localhost:3000,在頁面中輸入你的Swagger YAML文件的URL,然后點擊“Explore”按鈕查看和測試API。

通過以上步驟,你可以在Ubuntu上成功安裝和配置Swagger,從而進行API的監控和管理。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女