Skip to content

RESTHeart 心休息

介绍

官方文档:https://restheart.org/

这个名字取得挺好,开发搞久了,心脏也要跟着休息一下。

Virtual Threads Powered API Framework

特性:

  • ✅ 适用于 MongoDB 的即时 REST、GraphQL 和 Websockets API。
  • ✅ 声明式安全性,无需代码。
  • ✅ 在几分钟内实施您的后端。

安装

yaml
version: '3.9'

services:

  mongodb:
    image: 'bitnami/mongodb:7.0.14'
    container_name: mongodb
    ports:
      - 40101:27017
    restart: always
    volumes:
      - mongodb_data:/bitnami/mongodb
    environment:
      - MONGODB_ROOT_USER=admin
      - MONGODB_ROOT_PASSWORD=tlMsTf08gLiev7eY

  restheart:
    image: 'softinstigate/restheart:8.0.12'
    container_name: restheart
    ports:
      - 30102:8080
    restart: always
    environment:
      RHO: >
          /mclient/connection-string->"mongodb://admin:tlMsTf08gLiev7eY@mongodb:27017";
          /http-listener/host->"0.0.0.0";

volumes:
  mongodb_data:

networks:
  default:
    external: true
    name: open-net

常用操作

一下操作基于httpie,请先安装

bash
yum install -y httpie

修改密码

bash
http -a admin:secret PATCH :30102/users/admin password="Wd7zy3D23IPajUnM2p7OKI3SXQz3ZAQS"

创建集合

bash
http -a admin:secret PUT :30102/demos

新增用户

bash
http -a admin:secret POST :30102/users _id=alex password=7vrfyh37GUJ7FAE9 roles:='["user"]'

访问新集合

bash
# 401 密码错误
http -a alice:wrong GET :30102/demos

# 403 权限不够
http -a alice:secret GET :30102/demos

分配角色

bash
http -a admin:secret POST :30102/acl _id=userCanAccessOwnSecret roles:='["user"]' priority:=100 predicate="method(GET) and path('/demos')" mongo.readFilter:='{ "author": "@user._id" }'
bash
http -a admin:secret POST :30102/acl _id=userCanCreateOwnSecret roles:='["user"]' priority:=100 predicate="method(POST) and path('/demos')" mongo.mergeRequest:='{ "author": "@user._id" }'
bash
http -a admin:secret POST :30102/acl _id=userCanModifyOwnSecret roles:='["user"]' priority:=100 predicate="method(PATCH) and path-template('/demos/{id}')" mongo.writeFilter:='{ "author": "@user._id" }'
bash
http -a admin:secret POST :30102/acl _id=userCanDeleteOwnSecret roles:='["user"]' priority:=100 predicate="method(DELETE) and path('/demos')" mongo.mergeRequest:='{ "author": "@user._id" }'

查询

bash

# 查询所有文档
http -a alex:7vrfyh37GUJ7FAE9 GET :30102/demos

# 查询指定文档
http -a alex:7vrfyh37GUJ7FAE9 GET ':30102/demos?filter={"name": "Helen"}'

人生感悟