haproxy 2.0 dataplaneapi rest api 轉爲graphql

haproxy 2.0 dataplaneapi rest api 是比較全的,如下是一個簡單的集成graphql,經過swagger-to-graphql
轉換爲graphql api 方便使用node

環境準備

  • 項目準備
yarn init -y
yarn add express express-graphql graphql swagger-to-graphql
  • 獲取haproxy 2.0 dataplaneapi rest api swagger 定義
curl -s -X GET --user admin:dalong -H "Content-Type: application/json" http://localhost:5555/v1/specification > api.json
  • 代碼
const express = require('express');
const app = express();
const graphqlHTTP = require('express-graphql');
const graphQLSchema = require('swagger-to-graphql');
# 使用basic auth 輸入配置好的用戶以及密碼
const proxyUrl = 'http://admin:dalong@localhost:5555/v1/';
const pathToSwaggerSchema = './api.json';
const customHeaders = {
    // Authorization: 'Basic YWRkOmJhc2ljQXV0aA=='
};
graphQLSchema(pathToSwaggerSchema, proxyUrl, customHeaders)
  .then(schema => {
    app.use(
      '/graphql',
      graphqlHTTP(() => {
        return {
          schema,
          graphiql: true,
        };
      }),
    );
    app.listen(3009, 'localhost', () => {
      console.info('http://localhost:3009/graphql');
    });
  })
  .catch(e => {
    console.log(e);
  });
  • package.json 文件
{
  "name": "haproxy-dataplaneapi2graphql",
  "version": "1.0.0",
  "main": "app.js",
  "license": "MIT",
  "dependencies": {
    "express": "^4.17.1",
    "express-graphql": "^0.9.0",
    "graphql": "^14.5.4",
    "swagger-to-graphql": "^2.1.0"
  },
  "scripts": {
    "app": "node app.js"
  }
}
 

啓動&&效果

 
yarn app
  • 效果

 

 

  • 查詢效果

 

 

說明

以上是一個簡單的集成,經過graphql 的查詢api,咱們能夠方便的操做haproxygit

參考資料

https://github.com/yarax/swagger-to-graphql
https://www.npmjs.com/package/swagger-to-graphql
https://github.com/rongfengliang/haproxy2.0-prometheus
https://www.haproxy.com/documentation/hapee/1-9r1/configuration/dataplaneapi/
https://github.com/rongfengliang/haproxy-dataplaneapi2graphql/tree/mastergithub

相關文章
相關標籤/搜索