1. 程式人生 > >sqler sql 轉rest api redis 介面使用

sqler sql 轉rest api redis 介面使用

sqler 支援redis 協議,我們可以用過redis client 連線sqler,他會將巨集住轉換為redis command
實現上看原始碼我們發現是基於一個開源的redis 協議的golang 實現,同時sqler 內建了一些方便
的command : list、 info、 echo、select、ping。
目前關於redis 的使用文件基本沒有,但是我們通過閱讀原始碼可以看出,就是解析引數,第一個為
command, 第二個為資料(json 序列化之後的,下邊會有使用的說明

環境準備

docker 映象,可以參考我的文章,裡面有映象的製作同時我也上傳dockerhub 了

  • docker-compose 檔案
 
version: "3"
services:
  sqler:
    image: dalongrong/sqler:1.6
    volumes:
    - "./config/config.example.hcl:/app/config.example.hcl"
    environment:
    - "DSN=root:[email protected](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
``
* 執行的配置檔案
```code
_boot {
    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
}
allusers {
    methods = ["GET"]
    exec = <<SQL
        SELECT * FROM users;
    SQL
}
adduser {
    methods = ["POST"]
    rules {
        user_name = ["required"]
        user_email = ["required", "email"]
        user_password = ["required", "stringlength: 5,50"]
    }
    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
}
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
  • 新增資料
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":"[email protected]",
        "user_password":"dalongdemo"
}'
   

返回結果

{"data":[{"ID":1,"email":"[email protected]","name":"dalong","password":"$2a$10$9gB.kOC8sn5f3jGPo4qzl.tULFkDRXj0i3EMyAr7VvkvBxBijbqKu","time":1547169926}],"success":true}% 
 
  • 查詢資料
curl -i http://localhost:8025/allusers

返回結果

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=UTF-8
Vary: Origin
Vary: Accept-Encoding
Date: Fri, 11 Jan 2019 01:26:20 GMT
Content-Length: 170
{"data":[{"ID":1,"email":"[email protected]","name":"dalong","password":"$2a$10$9gB.kOC8sn5f3jGPo4qzl.tULFkDRXj0i3EMyAr7VvkvBxBijbqKu","time":1547169926}],"success":true}%
   

redis 整合使用

為了測試,我使用的是cli,沒有編寫程式碼

  • 連線
    預設埠是3678,可以通過環境變數修改
 
redis-cli -p 3678
  • 檢視巨集資訊
list
 

輸出如下:

1) "adduser"
2) "databases"
3) "_boot"
4) "allusers"
 
  • 通過redis-cli 新增資料
    呼叫adduser 巨集
 
adduser "{\"user_name\":\"dalong\",\"user_email\":\"[email protected]\",\"user_password\":\"dalongdemo\"}"
 

效果
實際上從這個也可以看出,會有一些bug,具體待確定

 
adduser "{\"user_name\":\"dalong\",\"user_email\":\"[email protected]\",\"user_password\":\"dalongdemo\"}"
(error) not found
127.0.0.1:3678> adduser "{\"user_name\":\"dalong\",\"user_email\":\"[email protected]\",\"user_password\":\"dalongdemo\"}"
、1) (integer) 1
2) "[{\"ID\":2,\"email\":\"[email protected]\",\"name\":\"dalong\",\"password\":\"$2a$10$995GTjNXCNAElXHafDoxkOsum5juPBjpu5dlIt0GJi.TsJfQ3uRIK\",\"time\":1547170172}]"
 
  • 通過redis-cli 查詢資料
    呼叫allusers 巨集
 
allusers
1) (integer) 1
2) "[{\"ID\":1,\"email\":\"[email protected]\",\"name\":\"dalong\",\"password\":\"$2a$10$9gB.kOC8sn5f3jGPo4qzl.tULFkDRXj0i3EMyAr7VvkvBxBijbqKu\",\"time\":1547169926},{\"ID\":2,\"email\":\"[email protected]\",\"name\":\"dalong\",\"password\":\"$2a$10$995GTjNXCNAElXHafDoxkOsum5juPBjpu5dlIt0GJi.TsJfQ3uRIK\",\"time\":1547170172}]"
 
  • 列出資料
    呼叫databases 巨集
 
databases
1) (integer) 1
2) "[\"information_schema\",\"mysql\",\"performance_schema\",\"sys\",\"test\"]"

說明

不太確定是sqler redis 協議的相容問題還是,目前使用mac 的cli,會有資料時有時無的問題,待確定原因,同時對於添加了安全
認證與redis 怎麼通訊還有待研究。

參考資料

https://github.com/alash3al/sqler/blob/master/server_resp.go
https://github.com/rongfengliang/sqler-docker-compose
https://github.com/alash3al/sqler