haproxy 2.0 dataplaneapi rest api 转为graphql

haproxy 2.0 dataplaneapi rest api 是比较全的,以下是一个简单的集成graphql,通过swagger-to-graphql
转换为graphql api 方便使用

环境准备

  • 项目准备
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,我们可以方便的操作haproxy

参考资料

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/master

posted on 2019-09-02 10:14  荣锋亮  阅读(413)  评论(0编辑  收藏  举报

导航