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   荣锋亮  阅读(416)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2018-09-02 vulcanjs 包类型
2018-09-02 vulcanjs schemas&& collections
2018-09-02 vulcanjs 核心架构概念
2018-09-02 vulcanjs 开源工具方便快速开发react graphql meteor 应用
2014-09-02 转微软最新的Web服务器Katana发布了版本3

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示