1. 程式人生 > >sqler sql 轉rest api 的docker 映象構建(續)使用原始碼編譯

sqler sql 轉rest api 的docker 映象構建(續)使用原始碼編譯

sqler 在社群的響應還是很不錯的,已經添加了好多資料庫的連線,就在早上專案的包管理還沒有寫明確,
下午就已經有go mod 構建的支援了,同時也調整下docker 映象的構建,直接使用git clone + go mod

備註: go mod 是新的包管理方案,需要新版本的golang,使用容器就不存在這個問題了,同時對於國內
還有一個牆的問題,同時我push了1.7tag 的映象,就是使用go mod 構建的。

dockerfile

 
FROM golang:alpine as build
ENV VERSION=v1.7
WORKDIR /app
RUN apk update && apk add wget unzip build-base git bzr mercurial gcc 
RUN git clone https://github.com/alash3al/sqler.git
RUN cd sqler && go build
FROM alpine:latest
ENV APPVERSION=1.7
LABEL VERSION="sqler-${APPVERSION}"
LABEL EMAIL="[email protected]"
LABEL AUTHOR="dalongrong"
WORKDIR /app
ENV DSN="root:[email protected](127.0.0.1:3306)/test?multiStatements=true"
ENV RESP=:3678
ENV CONFIG=config.example.hcl
ENV REST=:8025
ENV DRIVER=mysql
ENV WORKERS=4
EXPOSE 3678 8025
ENV PATH=$PATH:/usr/local/bin
COPY config/config.example.hcl /app/config.example.hcl
COPY entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh
COPY --from=build /app/sqler/sqler /usr/local/bin/sqler
ENTRYPOINT ["./entrypoint.sh"]
   

預設配置

config.example.hcl: 這個我有些調整,實際自己修改

_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
}
usersinfo {
  exec = "select * from users"
  transformer = <<JS
    // do some convert only print name && email
    (function(){
       var newResult=[];
       for (var item in $result) {
        var user = {
            user_name:$result[item].name,
            user_email:$result[item].email
        }
        newResult.push(user)
       }
       return newResult; 
    })()
  JS
}
demo {
  exec = "select * from users"
  transformer = <<JS
    // do some convert only print name && email
    (function(){
       return $result; 
    })()
  JS
}
   

entrypoiny 檔案

#!/bin/sh
sqler -driver ${DRIVER} -rest ${REST} -dsn ${DSN} -config ${CONFIG} -workers ${WORKERS} -resp ${RESP}
 

使用

docker-compose檔案

version: "3"
services:
  sqler:
    image: dalongrong/sqler:1.7
    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

說明

對於牆的問題,我們可以使用dockerhub 託管的構建,對於需要除錯程式碼的,合理上網解決吧

參考資料

https://github.com/rongfengliang/sqler-docker-compose
https://github.com/alash3al/sqler