使用Postgrest快速创建数据库的OpenAPI接口

 

下载镜像
 

docker pull postgrest/postgrest


正确运行需要修改postgrest配置文件。可以修改镜像中的配置文件/etc/postgrest.conf,或者修改运行的环境变量(具有“ PGRST_ ”前缀)。使用下面命令可以查看系统的环境变量:
 

docker inspect -f "{{.Config.Env}}" postgrest/postgrest

最简单的postgrest配置文件如下

# 连接URI
db-uri       = "postgres://user:password@192.168.126.131:5432/database"
# database schema to expose to REST clients
db-schema    = "public"
# 如果客户端没有认证,使用的数据库角色
db-anon-role = "user"

运行docker

docker run --rm  -p 3000:3000 \
  -e PGRST_DB_URI="postgres://user:password@192.168.126.131/database" \
  -e PGRST_DB_ANON_ROLE="user" \
  -e PGRST_DB_SCHEMA="public" \
  postgrest/postgrest

访问 http://ip:3000即可看到结果。

为了方便测试,安装一个swagger。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用OpenAPI定义REST接口的示例: ``` openapi: 3.0.0 info: title: 示例API description: 用于演示OpenAPI定义REST接口的示例 version: 1.0.0 servers: - url: https://api.example.com/v1 paths: /users: get: summary: 获取用户列表 operationId: getUsers responses: '200': description: 成功获取用户列表 content: application/json: schema: type: array items: type: object properties: id: type: integer description: 用户ID name: type: string description: 用户名 email: type: string description: 用户邮箱 post: summary: 创建新用户 operationId: createUser requestBody: required: true content: application/json: schema: type: object properties: name: type: string description: 用户名 email: type: string description: 用户邮箱 required: - name - email responses: '201': description: 成功创建新用户 content: application/json: schema: type: object properties: id: type: integer description: 用户ID name: type: string description: 用户名 email: type: string description: 用户邮箱 /users/{userId}: get: summary: 获取指定用户信息 operationId: getUserById parameters: - name: userId in: path description: 用户ID required: true schema: type: integer responses: '200': description: 成功获取指定用户信息 content: application/json: schema: type: object properties: id: type: integer description: 用户ID name: type: string description: 用户名 email: type: string description: 用户邮箱 put: summary: 更新指定用户信息 operationId: updateUserById parameters: - name: userId in: path description: 用户ID required: true schema: type: integer requestBody: required: true content: application/json: schema: type: object properties: name: type: string description: 用户名 email: type: string description: 用户邮箱 required: - name - email responses: '200': description: 成功更新指定用户信息 content: application/json: schema: type: object properties: id: type: integer description: 用户ID name: type: string description: 用户名 email: type: string description: 用户邮箱 delete: summary: 删除指定用户 operationId: deleteUserById parameters: - name: userId in: path description: 用户ID required: true schema: type: integer responses: '204': description: 成功删除指定用户 ``` 以上是一个简单的示例,包含了获取用户列表、创建新用户、获取指定用户信息、更新指定用户信息和删除指定用户等5个接口。其中,每个接口都包含了请求参数、响应数据和状态码等信息,可以通过OpenAPI工具自动生成客户端或服务端代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值