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="1141519465@qq.com"
LABEL AUTHOR="dalongrong"
WORKDIR /app
ENV DSN="root:root@tcp(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: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
说明
对于墙的问题,我们可以使用dockerhub 托管的构建,对于需要调试代码的,合理上网解决吧
参考资料
https://github.com/rongfengliang/sqler-docker-compose
https://github.com/alash3al/sqler