pREST学习笔记

一、安装

Docker

$ docker pull prest/prest:latest

二、运行

Docker

$ docker run --rm -e PREST_HTTP_PORT=3000 \
        -e PREST_PG_HOST=xxx.xxx.xxx.xxx\
        -e PREST_PG_PORT=5432 \
        -e PREST_PG_USER=postgres \
        -e PREST_PG_PASS=mysecretpassword \
        -e PREST_PG_DATABASE=postgres \
        -e PREST_SSL_MODE=disable \
        -p 3000:3000 prest/prest:latest

三、Query Statements

使用 curl 进行操作:

$ curl -i -X GET http://127.0.0.1:3000/postgres/public/items

增删改查

POST

$ curl -i -X POST http://127.0.0.1:3000/postgres/public/items -H "Content-Type: application/json" -d '{"number": "2", "name": "pencil", "price": "5"}'

-H:header,接收方自动识别转为 json

-d:data,post 需要有数据

DELETE

$ curl -i -X DELETE http://127.0.0.1:3000/postgres/public/items?name='notebook'

PATCH/PUT

$ curl -i -X PATCH http://127.0.0.1:3000/postgres/public/items?name='book' -H "Content-Type: application/json" -d '{"price": "30"}'

-d:可以只写需要更新的数据对应的值。

?:是定位符,如果没有,则全量更新,删除也是如此。

GET

filter参数
$ curl -i -X GET http://127.0.0.1:3000/postgres/public/items?name=$eq'book'

官方文档里写的是:GET /DATABASE/SCHEMA/TABLE?FIELD=$eq.VALUE。实际上,. 需要删掉,否则获取不到数据。

问题

  • gt:大于,但是等于也能获取到?( lt 也有一样的问题)

  • ne:不等于,但是等于也能获取到?

  • incurl -i -X GET http://127.0.0.1:3000/postgres/public/items\?price\=$in\{'20.00','30.00'\}

  • nin:查询出来的反而是 in 的结果。

  • like:查询出来的是全部。curl -i -X GET http://127.0.0.1:3000/postgres/public/items\?name\=$like'%book'

query string
$ curl -i -X GET http://127.0.0.1:3000/postgres/public/items\?_select={name}
$ curl -i -X GET http://127.0.0.1:3000/postgres/public/items\?_count={name}
$ curl -i -X GET http://127.0.0.1:3000/postgres/public/items\?_distinct\=true
$ curl -i -X GET http://127.0.0.1:3000/postgres/public/items\?_order={price} # 默认增序

解决方法

-G 表示 GET

gt(gte、lt、lte同理)

$ curl -G -d 'price=$gt.15' http://127.0.0.1:3000/postgres/public/items

in、nin

$ curl -G -d 'price=$in.15,30' http://127.0.0.1:3000/postgres/public/items

具体的数组直接写在 $in. 后面,不需要写 []{}

like、ilike

$ curl -G -d 'name=$like.%25book' http://127.0.0.1:3000/postgres/public/items

如果使用 $like.%book,则会查询全部,不能正确匹配。经过查阅资料,发现应该是没有转义 %,因为 % 经过URL编码的结果是 %25。如果改成 %25 则会成功查询出匹配 name 字段尾部是 book 的所有数据。

遗留问题

为什么 ?price=$gt30 写在链接后面,查询出来的还包括等于的,而把参数写在前面就可以正确查询?

四、报错整理

1. [error] dial tcp 127.0.0.1:5432: connect: connection refused

代码

$ docker run -e PREST_HTTP_PORT=3000 \
        -e PREST_PG_HOST=127.0.0.1 \
        -e PREST_PG_USER=postgres \
        -e PREST_PG_PASS=mysecretpassword \
        -e PREST_PG_DATABASE=prest \
        prest/prest:latest

原因:连接被拒绝一般是没有这个 IP 地址或者端口。查看 docker 容器:

$ docker container ls -a

结果

CONTAINER ID   IMAGE                COMMAND                  CREATED          STATUS                        PORTS                                   NAMES
958000ab2099   postgres:alpine      "docker-entrypoint.s…"   6 days ago       Up 2 minutes                  5432/tcp                                some-postgres
3470bafff001   nginx:alpine         "/docker-entrypoint.…"   6 days ago       Exited (255) 6 hours ago      0.0.0.0:8080->80/tcp, :::8080->80/tcp   vigorous_cerf

发现 postgres 这个容器没有做 IP 映射,因此 -e PREST_PG_HOST=127.0.0.1 找不到相应 IP 地址。

于是考虑做 IP 映射,使用 -p 在运行 postgres 容器时将端口映射到宿主机的指定端口:

$ docker run --name some-postgres --rm -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres:alpine

再使用 docker ps -a 查看 docker 容器:

CONTAINER ID   IMAGE             COMMAND                  CREATED         STATUS                     PORTS                                       NAMES
44c4253e9fa2   postgres:alpine   "docker-entrypoint.s…"   5 seconds ago   Up 3 seconds               0.0.0.0:5432->5432/tcp, :::5432->5432/tcp   some-postgres
3470bafff001   nginx:alpine      "/docker-entrypoint.…"   6 days ago      Exited (255) 6 hours ago   0.0.0.0:8080->80/tcp, :::8080->80/tcp       vigorous_cerf

发现这时已经有 iPhone 映射。解释一下这里的 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp 分别是什么意思。

0.0.0.0:5432->5432/tcp0.0.0.0 代表本机所有 IPV4 地址,因此本机所有 IPV4 地址 + 端口 :5432 都可以指向 postgres。

:::5432->5432/tcp:: 其实是 0:0:0:0:0:0:0:0 的缩写,代表本机所有 IPV6 地址。

127.0.0.1:所有网络号为 127 的地址都被称之为回环地址。

再执行以下命令:

$ docker run --rm -e PREST_HTTP_PORT=3000 \
        -e PREST_PG_HOST=127.0.0.1 \
        -e PREST_PG_USER=postgres \
        -e PREST_PG_PASS=mysecretpassword \
        -e PREST_PG_DATABASE=prest \
        prest/prest:latest

发现还是报错:[error] dial tcp 127.0.0.1:5432: connect: connection refused

原因:可以把这个 -e PREST_PG_HOST=127.0.0.1 理解成配置文件,所以肯定是从 prest 这个 docker 里进行寻找 postgres 进行连接,因此找不到。而运行的 postgres 容器则是跑在另一个 docker 里的,两者没有连接桥梁,所以根本跑不通。连接方式有两种,一种是将 postgres 映射到本机上,这样就相当于 postgres 在本机上操作写操作,然后 prest 通过这个端口操作读操作,就可以连接上 postgres 容器了。另一种则是使用两个 docker 容器直连,通过 network 配置 DNS,这样 host 只需要写成需要连接的 docker 容器起的名字就行,可以自动识别端口,如果使用 docker compose,可以直接 host 写成需要连接的 docker 容器起的名字,不需要单独设置 DNS。

参考docker容器的网络配置,允许docker可以被宿主机以外的其它主机访问以及局域网内可以直接访问docker容器ip

解决方法

  1. 使用 --network host 使得 prest 容器可以操作本机(不推荐)。
  2. 使用 WiFi 网络作为 HOST 地址。此方法有一个缺点,会走一遍路由器,没有方法一快。

执行以下命令查看 WiFi 地址:

$ ifconfig

找到这个:

en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	options=400<CHANNEL_IO>
	ether 88:66:5a:3b:56:2a
	inet6 fe80::10d9:f421:90f7:60b8%en0 prefixlen 64 secured scopeid 0x6
	inet xxx.xxx.xxx.xxx netmask 0xfffffc00 broadcast 10.8.203.255
	nd6 options=201<PERFORMNUD,DAD>
	media: autoselect
	status: active

xxx.xxx.xxx.xxx 就是 WiFi 的 IP 地址。

然后执行:

$ docker run --rm -e PREST_HTTP_PORT=3000 \
        -e PREST_PG_HOST=xxx.xxx.xxx.xxx \
        -e PREST_PG_PORT=5432 \
        -e PREST_PG_USER=postgres \
        -e PREST_PG_PASS=mysecretpassword \
        -e PREST_PG_DATABASE=prest \
        prest/prest:latest

发现报错:

[error] pq: SSL is not enabled on the server

此时,只需要加一行 -e PREST_SSL_MODE=disable 关闭 SSL 就可以了。

$ docker run --rm -e PREST_HTTP_PORT=3000 \
        -e PREST_PG_HOST=xxx.xxx.xxx.xxx \
        -e PREST_PG_PORT=5432 \
        -e PREST_PG_USER=postgres \
        -e PREST_PG_PASS=mysecretpassword \
        -e PREST_PG_DATABASE=prest \
        -e PREST_SSL_MODE=disable \
        prest/prest:latest

发现又报错:

[error] pq: database "prest" does not exist

发现是自己写错了数据库名字,连接进入 postgres 里使用 \l 命令查看数据库名字:

postgres=# \l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges
-----------+----------+----------+------------+------------+-----------------------
 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres

然后 exit 退出 postgres 数据库,再执行:

$ docker run --rm -e PREST_HTTP_PORT=3000 \
        -e PREST_PG_HOST=xxx.xxx.xxx.xxx \
        -e PREST_PG_PORT=5432 \
        -e PREST_PG_USER=postgres \
        -e PREST_PG_PASS=mysecretpassword \
        -e PREST_PG_DATABASE=postgres \
        -e PREST_SSL_MODE=disable \
        prest/prest:latest

就可以成功运行 prest 连接 postgres 了:

[prestd] Waiting for port xxx.xxx.xxx.xxx:5432 to become available...
[prestd] Ready hosting xxx.xxx.xxx.xxx to port 5432 !
2021/07/20 08:39:38 [warning] adapter is not set. Using the default (postgres)
2021/07/20 08:39:38 [warning] You are running pREST in public mode.
[prest] listening on 0.0.0.0:3000 and serving on /

2. HTTP/1.1 404 Not Found

执行命令:

$ curl -i -X GET http://127.0.0.1:3000/auth --user "username:password"

报错

HTTP/1.1 404 Not Found
Content-Type: application/json
X-Content-Type-Options: nosniff
Date: Thu, 22 Jul 2021 02:51:30 GMT
Content-Length: 34

{
	"error": "404 page not found"
}%

原因:没有 auth 这个路径,端口后面跟着的应该是 /DATABASE/SCHEMA/TABLE

解决方法

$ curl -i -X GET http://127.0.0.1:3000/postgres/public/items
全部评论

相关推荐

秋招结束已经一段时间了&nbsp;一直在忙着毕业的事情&nbsp;浅浅总结一下自己的秋招经历吧~本人BG双非硕&nbsp;后端选手&nbsp;有一段小厂+腾讯暑期实习腾讯暑期转正loser秋招结束已经结束了有一段时间了总结一下秋招历程最大的感受就是秋招比起暑期更加卡学历秋招总共投了60多家吧一直面&nbsp;一直挂也投了两家银行科技岗&nbsp;都走到终面体检了都拒了(总体感觉本地的银行还是挺容易过的)可能本人更想去私企&nbsp;并且银行也挺卷听说一直到11月就只有一家小厂的offer并签约当保底然后也突然被WXG捞了&nbsp;本来都不对腾讯抱有希望了可能经过一整个秋招的面试积累吧&nbsp;以及本人有ACM经历&nbsp;WXG整体面试以做题偏多(一二面做了5道题&nbsp;4道hard)&nbsp;比较合自己胃口&nbsp;差不多半个月就把五轮面试过了进入录用评估&nbsp;但也一直没有结果到后面也陆陆续续有几家中厂也终面过泡池子一直到12月初华子给开了base杭州&nbsp;14a因为华子公积金的原因&nbsp;和小厂薪资上差距不大&nbsp;所以也一直犹豫是否毁约签华子&nbsp;但是内心也还对WXG抱有一丝幻想(虽然一直没有保温也没有任何消息)然后一直到12月中下旬&nbsp;华子要求去现场签约了&nbsp;但是WXG还是没有消息&nbsp;然后就连续发邮件和打电话催了好多次&nbsp;还是回复耐心等待直到华子签约那天&nbsp;经过内心挣扎已经决定毁约签华子了&nbsp;可能还是想平台更大一点吧&nbsp;然后最戏剧性的一幕来了&nbsp;就在我发毁约邮件没有5秒&nbsp;WXG打电话开奖了&nbsp;并且开奖也十分有诚意&nbsp;最终还是没有签约成功华子&nbsp;研究生期间也打了很多次华子的比赛还是对华子有感情的555整个秋招都是伴随着焦虑的&nbsp;我认为自己也是秋招大部分人的画像&nbsp;屡屡碰壁后不断怀疑自己&nbsp;但是可能自己也比较幸运吧&nbsp;但是也感谢自己在一次次陷入迷茫都没有放弃自己&nbsp;还是一直努力背八股&nbsp;刷题也祝各位牛友们共勉&nbsp;就算暂时没有好的offer&nbsp;不放弃一定会有好的结果的!!
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务