sqler sql 转rest api 的工具试用
sqler 从开源很快就获取了1k的star,使用起来很方便,而且也很灵活,支持的数据库也比较多。
支持的功能
- 无需依赖,可独立使用;
- 支持多种数据可类型,包括:SQL Server, MYSQL, SQLITE, PostgreSQL, Cockroachdb 等;
- 内置 RESTful 服务器;
- 内置 RESP Redis 协议,可以使用任何 redis 客户端连接到 SQLer;
- 内置 Javascript 解释器,可轻松转换结果;
- 内置验证器;
- 自动使用预备语句;
- 使用(HCL)配置语言;
- 可基于 unix glob 模式加载多个配置文件;
- 每条 SQL 查询可被命名为宏;
- 在每个宏内可使用 Go text/template;
- 每个宏都有自己的 Context(查询参数 + 正文参数)作为.Input(map [string] interface{}),而.Utils是辅助函数列表,目前它只包含 SQLEscape;
- 可自定义授权程序,授权程序只是一个简单的 webhook,sqler 使用这个 webhook 验证是否应该完成某请求
测试环境准备
为了方便测试,我制作了一个比较简单的docker 镜像dalongrong/sqler:1.5,对于运行的参数可以通过环境
变量指定
- docker-compose 文件
version: "3"
services:
sqler:
image: dalongrong/sqler:1.5
environment:
- "DSN=root:dalongrong@tcp(mysqldb:3306)/test?multiStatements=true"
ports:
- "3678:3678"
- "8025:8025"
mysqldb:
image: mysql:5.7.16
ports:
- 3306:3306
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
environment:
MYSQL_ROOT_PASSWORD: dalongrong
MYSQL_DATABASE: test
MYSQL_USER: test
MYSQL_PASSWORD: test
TZ: Asia/Shanghai
- 说明
镜像dalongrong/sqler:1.5 包含一个默认的配置,也可以通过环境指定,配置内容来自官方
内容如下:
// create a macro/endpoint called "_boot",
// this macro is private "used within other macros"
// because it starts with "_".
_boot {
// the query we want to execute
exec = <<SQL
CREATE TABLE IF NOT EXISTS `users` (
`ID` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(30) DEFAULT "@anonymous",
`email` VARCHAR(30) DEFAULT "@anonymous",
`password` VARCHAR(200) DEFAULT "",
`time` INT UNSIGNED
);
SQL
}
// adduser macro/endpoint, just hit `/adduser` with
// a `?user_name=&user_email=` or json `POST` request
// with the same fields.
adduser {
// what request method will this macro be called
// default: ["ANY"]
methods = ["POST"]
// authorizers,
// sqler will attempt to send the incoming authorization header
// to the provided endpoint(s) as `Authorization`,
// each endpoint MUST return `200 OK` so sqler can continue, other wise,
// sqler will break the request and return back the client with the error occured.
// each authorizer has a method and a url, if you ignored the method
// it will be automatically set to `GET`.
// authorizers = ["GET http://web.hook/api/authorize", "GET http://web.hook/api/allowed?roles=admin,root,super_admin"]
// the validation rules
// you can specifiy seprated rules for each request method!
rules {
user_name = ["required"]
user_email = ["required", "email"]
user_password = ["required", "stringlength: 5,50"]
}
// the query to be executed
exec = <<SQL
{{ template "_boot" }}
/* let's bind a vars to be used within our internal prepared statment */
{{ .BindVar "name" .Input.user_name }}
{{ .BindVar "email" .Input.user_email }}
{{ .BindVar "emailx" .Input.user_email }}
INSERT INTO users(name, email, password, time) VALUES(
/* we added it above */
:name,
/* we added it above */
:email,
/* it will be secured anyway because it is encoded */
'{{ .Input.user_password | .Hash "bcrypt" }}',
/* generate a unix timestamp "seconds" */
{{ .UnixTime }}
);
SELECT * FROM users WHERE id = LAST_INSERT_ID();
SQL
}
// list all databases, and run a transformer function
databases {
exec = "SHOW DATABASES"
transformer = <<JS
// there is a global variable called `$result`,
// `$result` holds the result of the sql execution.
(function(){
newResult = []
for ( i in $result ) {
newResult.push($result[i].Database)
}
return newResult
})()
JS
}
运行&&测试
- 运行
docker-compose up -d
- 添加数据
rest 接口地址为8025
添加数据curl 命令
curl -X POST \
http://localhost:8025/adduser \
-H 'Content-Type: application/json' \
-H 'Postman-Token: a7784ea1-9f50-46ee-92ac-1d850334f3f1' \
-H 'cache-control: no-cache' \
-d '{
"user_name":"dalong",
"user_email":"1141591465@qq.com",
"user_password":"dalongdemo"
}'
返回结果
{
"data": [
{
"ID": 1,
"email": "1141591465@qq.com",
"name": "dalong",
"password": "$2a$10$nfPllaq3AqYDwu4SQTskWeN0BphHCoBzwmb4rj6Q0OB21voBHCZke",
"time": 1547127497
}
],
"success": true
}
数据库数据 
说明
sqler 的设计很方便,我们通过简单的配置就可以创建灵活的rest api 了,很强大,而且内置的认证处理,数据校验。。。
参考资料
https://www.infoq.cn/article/LZxqJd-ZcNiUKcG1APDz
https://github.com/alash3al/sqler
https://github.com/rongfengliang/sqler-docker-compose
sqler sql 转rest api 的工具试用的更多相关文章
- sqler sql 转rest api 2.0 试用
sqler 的迭代还是很快的,已经2.0 了,2.0 有好多新功能的添加,同时也有好多不兼容的修改 说明: 测试使用docker-compose,同时我已经push 了docker 镜像 dalong ...
- sqler sql 转rest api 源码解析(一)应用的启动入口
sqler sql 转rest api 的源码还是比较简单的,没有比较复杂的设计,大部分都是基于开源 模块实现的. 说明: 当前的版本为2.0,代码使用go mod 进行包管理,如果本地运行注意gol ...
- sqler sql 转rest api javascript 试用
sqler 内嵌了一个js 引擎的实现(基于goja,当我们配置了exec的配置之后 调用宏(redis 接口)或者rest api 的时候会有一个全局变量$result ,保存了执行的结果,我们可以 ...
- sqler sql 转rest api 数据聚合操作
sqler 2.0 提供了一个新的指令aggregate,注意这个和sql 的聚合函数不是一个概念,这个只是为了 方便api数据的拼接 参考格式 databases { exec = &qu ...
- sqler sql 转rest api redis 接口使用
sqler 支持redis 协议,我们可以用过redis client 连接sqler,他会将宏住转换为redis command 实现上看源码我们发现是基于一个开源的redis 协议的golang ...
- sqler sql 转rest api 源码解析(四)macro 的执行
macro 说明 macro 是sqler 的核心,当前的处理流程为授权处理,数据校验,依赖执行(include),聚合处理,数据转换 处理,sql 执行以及sql 参数绑定 授权处理 这个是通过go ...
- sqler sql 转rest api 数据校验的处理
早期版本(2.0 之前)使用rules 进行数据校验处理,2.0 之后进行了修改使用 validators,这样更加明确 参考格式 addpost { // if any rule retu ...
- sqler sql 转rest api 防止sql 注入
sqler 对于sql Sanitization 的处理,我们可以使用bind 指令 说明: 这个是2.0 的功能,注意版本的使用 参考格式 addpost { // $input is a ...
- sqler sql 转rest api 授权处理
我们可以使用内置的authorizer 以及js 脚本,方便的进行api 接口的授权处理 说明: 这个是2.0 的功能,注意版本的使用 参考格式 addpost { authorizer = & ...
随机推荐
- Android : 获取声卡信息的测试代码
完整的编译包(android平台): 链接:http://pan.baidu.com/s/1qXMTT7I 密码:2bow /* * ALSA parameter test program * * C ...
- 3.7 C++派生类构造函数调用规则
参考:http://www.weixueyuan.net/view/6364.html 总结: 派生类构造函数可以自动调用基类的默认构造函数而无需显式调用. 生类构造函数可以自动调用基类的默认构造函数 ...
- session和cokkie的区别与作用
session在计算机中,尤其是在网络应用中,称为“会话机制”,Session对象存储特定用户会话所需的属性及配置信息,这样,当用户在应用程序的web页之间跳转时,存储在session对象中的变量将不 ...
- Spring学习三
Spring注解来注入bean 在classpath中扫描组件 组件扫描,即componetscanning 利用注解来扫描的组件有 @Component :基本注解,表示一个受Spring管理的 ...
- 性能测试-9.Controller功能介绍
Controller功能介绍 设计场景 配置场景 场景监控 一.设计场景 二.配置场景 规划场景(场景名.调度方式.运行方式.Initialize方式.Start Vusers方式.Duration方 ...
- 【git学习笔记】
一.查看git的配置文件 1.在项目下,有一个.git的隐藏文件 2.config为git的配置文件 3.查看config :branch表示分支,此配置文件表示当前有两个分支NNU和master,一 ...
- SQL注入之Sqli-labs系列第二十四关(二阶注入)
开始挑战第二十四关(Second Degree Injections) 0x1 前言 SQL注入一般分为两类:一阶SQL注入(普通SQL注入),二阶SQL注入 .二次注入不是注入两次的意思,请不要混淆 ...
- 安卓 dex 通用脱壳技术研究(一)
注:以下4篇博文中,部分图片引用自DexHunter作者zyqqyz在slide.pptx中的图片,版本归原作者所有: 0x01 背景介绍 安卓 APP 的保护一般分为下列几个方面: JAVA/C代码 ...
- Oracle查询行对应block_id,file_id
select id,rowid, dbms_rowid.rowid_object(rowid) object#, dbms_rowid.rowid_relative_fno(rowid) file#, ...
- DG备库缺失归档文件GAP日志
问题现象: XXXsdgebus-dg GAP手工注册归档 #出现GAP idle>select * from v$archive_gap; THREAD# LOW_SEQUENCE# HIGH ...