当前位置: 首页 > news >正文

凡科网站设计模板品牌网站分析

凡科网站设计模板,品牌网站分析,wordpress表结构,网站开发费用摊销年限目录 配置使用 验证 token使用 上文部署高可用PostgreSQL14集群后,本文介绍PostgREST,以及如何基于PostgREST实现数据库即服务,PostgREST可以在 PostgreSQL 数据库上通过解析数据库结构(如表、视图、存储过程、权限等&#xff…

目录

配置使用

验证

token使用


上文部署高可用PostgreSQL14集群后,本文介绍PostgREST,以及如何基于PostgREST实现数据库即服务,PostgREST可以在 PostgreSQL 数据库上通过解析数据库结构(如表、视图、存储过程、权限等)自动生成 RESTful API,并直接转换为标准的 HTTP 接口,相当于消除了简单场景下的后端代码和服务,与之前绍过的MagicAPI这种基于SpringBoot的快速api生成工具(magicApi)相比,magicAPI因为复杂度高反而意义不大了(至少在pg数据库中,其他数据库不一定有PostgREST这种工具)。

软件架构逃不开合久必分分久必合的趋势,现在硬件这么能打,玩明白数据库高级服务就可以不用搞那些复杂的后端,等业务足够复杂再去上微服务等分布式架构才是合理的;

配置使用

直接参照官方文档操作PostgREST,复用上文的环境,在100.3.254.211这个主节点上下载二进制文件 PostgREST,创建配置文件,并做成服务启动:

# json解析工具
[root@node2 postgrest]# yum install -y jq
# 配置postgrest
[root@node2 postgrest]# mkdir -p /opt/postgrest
[root@node2 postgrest]# chown postgres:postgres /opt/postgrest/ -R
[root@node2 postgrest]# cd /opt/postgrest
[root@node2 postgrest]# chmod +x postgrest
[root@node2 postgrest]# cat tutorial.conf 
db-uri = "postgres://authenticator:admin123@localhost:5432/postgres"
db-schemas = "api"
db-anon-role = "web_anon"# 配置成服务
[root@node2 postgrest]# cat /etc/systemd/system/postgrest.service
[Unit]
Description=PostgREST API Server
After=network.target[Service]
Type=simple
User=postgres
Group=postgres
ExecStart=/opt/postgrest/postgrest /opt/postgrest/tutorial.conf
Restart=always
RestartSec=5
Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin[Install]
WantedBy=multi-user.target# 启动
[root@node2 postgrest]# systemctl daemon-reload 
[root@node2 postgrest]# systemctl restart postgrest.service 
[root@node2 postgrest]# systemctl status postgrest.service 

在postgresql中创建用户和权限:

postgres=# create schema api;
postgres=# create table api.todos (
postgres(#   id int primary key generated by default as identity,
postgres(#   done boolean not null default false,
postgres(#   task text not null,
postgres(#   due timestamptz
postgres(# );
postgres=# insert into api.todos (task) values ('finish tutorial 0'), ('pat self on back');
postgres=# create role web_anon nologin;
postgres=# grant usage on schema api to web_anon;
postgres=# grant select on api.todos to web_anon;
postgres=# create role authenticator noinherit login password 'admin123';
postgres=# grant web_anon to authenticator;

验证

这就ok啦可以使用curl来测试API接口了,3000是postgrest提供的服务端口,web_anon用户能查到api这个schema下的todos表,并提供查询服务;

[root@node2 ~]# curl http://localhost:3000/todos |jq
[{"id": 1,"done": false,"task": "finish tutorial 0","due": null},{"id": 2,"done": false,"task": "pat self on back","due": null}
]

token使用

postgrest还提供基于jwt的认证机制,官方文档写的很清晰,在jwt.io中拿到手动生成的token,并带入curl请求中:

export LC_CTYPE=C
echo "jwt-secret = \"$(< /dev/urandom tr -dc A-Za-z0-9 | head -c32)\"" >> tutorial.conf[root@node2 postgrest]# cat tutorial.conf 
db-uri = "postgres://authenticator:admin123@localhost:5432/postgres"
db-schemas = "api"
db-anon-role = "web_anon"
jwt-secret = "chthyFjIdcLxEgKGHh780q5ly3yPebW1"# 配置文件中增加了jwt-secret重启服务
[root@node2 postgrest]# systemctl restart postgrest

复制这个jwt-token放入环境变量并重启postgrest服务:

[root@node2 postgrest]# export TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoidG9kb191c2VyIn0.rIRseKU6UQv7GA_sY6tBYsOt4-0nK0zCmP_y1jsIxA0"[root@node2 postgrest]# curl http://localhost:3000/todos -X POST      -H "Authorization: Bearer $TOKEN"        -H "Content-Type: application/json"      -d '{"task": "learn how to auth"}'[root@node2 postgrest]# curl http://localhost:3000/todos |jq
[{"id": 1,"done": false,"task": "finish tutorial 0","due": null},{"id": 2,"done": false,"task": "pat self on back","due": null},{"id": 3,"done": false,"task": "learn how to auth","due": null}
]

以上简单测试实现PostgreSQL的数据库即服务的配置,部署数据库就能提供api,还支持试图、函数等高级使用,而且这个开源工具还比较活跃,是个很好的选项,后续高级功能待研究;

http://www.dtcms.com/a/454150.html

相关文章:

  • 【Spring 2】深入剖析 Spring 的 Singleton 作用域:不仅仅是单例
  • 【密码学实战】openHiTLS kdf命令行:密钥派生工具
  • 手机网站微信网站开发南京米雅途做网站如何
  • AQS(抽象队列同步器)
  • 【深度学习计算机视觉】10:转置卷积——从图像上采样到特征图还原的核心技术
  • 做坑人网站二维码全网热度指数
  • 购物网站设计的目的新绛网站建设
  • ERT中正问题和逆问题的传统数学推导
  • 模电基础:电流源电路
  • 58同城济南网站建设网站建设公司人员配置
  • 山西品牌网站建设什么网站上做效果图可以赚钱
  • 尚庭公寓中Redis的使用
  • 网站建设服务采购方案惠阳网站优化
  • Python 学习(2) ---- Python 数据类型
  • 三更app下载网站衡阳网站设计
  • 语言结构的基本单位:语素、词根与词缀
  • 线程邮箱(1)
  • 【深入理解计算机系统第3版】浮点数舍入和在不同位模式下的变换2.52
  • 有了域名和空间怎么做网站网站怎么做下拉刷新
  • 【完整源码+数据集+部署教程】 水果叶片分割系统: yolov8-seg-dyhead
  • 前端如何做双语网站wordpress缓存插件汉化破解版
  • Python回调函数中携带额外状态的完整指南:从基础到高级实践
  • 郑州的设计公司企业网站建设推荐乐云seo
  • OpenAI 推出 ChatGPT 应用系统,开发者可在平台内构建互动式应用
  • 【Coze】1.Coze 的基本介绍
  • 字符串比较函数strcmp和strncmp以及memcmp详解与对比分析
  • linux练习-2
  • 大连做网站首选领超科技35互联做的网站
  • 国庆科技感祝福:Python 粒子国旗动画
  • 厦门微信网站开发潮州汕头