diff --git a/.gitignore b/.gitignore index 611ac9f..36ce461 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,7 @@ __pycache__ .vscode /.env *.log + +temp/ + +/blotter \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 7501ca9..64fe892 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,28 +1,15 @@ -FROM ubuntu +FROM ubuntu:latest -ENV DOCKER_PROXY="http://host.docker.internal:1081" -ENV NODE_REGISTRY="https://registry.npm.taobao.org" -ENV GOPROXY="https://goproxy.cn,direct" +ENV mongoURI="mongodb:27017" -# set proxy and update -ENV HTTP_PROXY=$DOCKER_PROXY \ - HTTPS_PROXY=$DOCKER_PROXY \ - http_proxy=$DOCKER_PROXY \ - https_proxy=$DOCKER_PROXY -ENV PATH=$PATH:/usr/local/bin +COPY ./blotter /blotter -RUN apt-get update && apt-get install -y ca-certificates && update-ca-certificates -RUN mv /etc/apt/sources.list /etc/apt/sources.list.back -RUN printf 'deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse\ndeb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse\ndeb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse\ndeb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse' >> /etc/apt/sources.list +# gojieba 字典文件 +COPY ./temp/mod/github.com/ttys3/gojieba@v1.1.3/dict/hmm_model.utf8 /go/pkg/mod/github.com/ttys3/gojieba@v1.1.3/dict/hmm_model.utf8 +COPY ./temp/mod/github.com/ttys3/gojieba@v1.1.3/dict/idf.utf8 /go/pkg/mod/github.com/ttys3/gojieba@v1.1.3/dict/idf.utf8 +COPY ./temp/mod/github.com/ttys3/gojieba@v1.1.3/dict/jieba.dict.utf8 /go/pkg/mod/github.com/ttys3/gojieba@v1.1.3/dict/jieba.dict.utf8 +COPY ./temp/mod/github.com/ttys3/gojieba@v1.1.3/dict/stop_words.utf8 /go/pkg/mod/github.com/ttys3/gojieba@v1.1.3/dict/stop_words.utf8 +COPY ./temp/mod/github.com/ttys3/gojieba@v1.1.3/dict/user.dict.utf8 /go/pkg/mod/github.com/ttys3/gojieba@v1.1.3/dict/user.dict.utf8 -RUN unset HTTP_PROXY -RUN unset HTTPS_PROXY -RUN unset http_proxy -RUN unset https_proxy - -RUN apt-get update && apt-get install git screen golang nodejs npm yarn mongodb -y -RUN npm install -g yarn -RUN yarn config set registry $NODE_REGISTRY -RUN yarn config set proxy $DOCKER_PROXY -RUN service mongodb start +ENTRYPOINT [ "/blotter", "-address=0.0.0.0:50000"] \ No newline at end of file diff --git a/build.bash b/build.bash index 0f7c4f2..1dc61bc 100644 --- a/build.bash +++ b/build.bash @@ -1,10 +1,33 @@ -#!/usr/bin/env bash +#!/bin/bash -_date=$(date '+%Y-%m-%d %H:%M:%S') -_branch=$(git rev-parse --abbrev-ref HEAD); _branch="${_branch}@" -_version=$(git describe --abbrev=0 --tags 2>/dev/null); if [[ -z $_version ]]; then _version="v0.0.0"; fi -_ldflags="${_branch}${_version} (${_date})" -go build -ldflags "-X 'main._version=${_ldflags}'" +SHELL_FOLDER=$(cd "$(dirname "$0")";pwd) +IMAGE="golang:1.16.3" -unset _date _branch _version _ldflags \ No newline at end of file + +# 拉取镜像 +if [[ $(docker images ${IMAGE} | wc -l) -eq "1" ]]; then + echo "Pulling docker image ${IMAGE}..." + docker pull ${IMAGE} +fi + +# 更新代码 +echo "Pulling latest code..." +git pull + + +# 在 docker 中挂载当前目录编译 +echo "Building execute in docker..." + +docker run --rm \ + -v ${SHELL_FOLDER}:/data/blotter \ + -v ${SHELL_FOLDER}/temp:/go/pkg \ + ${IMAGE} \ + bash -c "go env -w GOPROXY=https://goproxy.cn,direct && cd /data/blotter && echo 'Generating...' && go generate" + +echo "Build finished" + +docker build -t blotter . + +echo "Docker image 'blotter' build finished." +echo 'Using `docker run --rm --name=backend blotter` to start server.' \ No newline at end of file diff --git a/generate.bash b/generate.bash new file mode 100644 index 0000000..73c98b9 --- /dev/null +++ b/generate.bash @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +# 生成代码 + +_date=$(date '+%Y-%m-%d %H:%M:%S') +_branch=$(git rev-parse --abbrev-ref HEAD); _branch="${_branch}@" +_version=$(git describe --abbrev=0 --tags 2>/dev/null); if [[ -z $_version ]]; then _version="v0.0.0"; fi +_ldflags="${_branch}${_version} (${_date})" + +go build -ldflags "-X 'main._version=${_ldflags}'" + +unset _date _branch _version _ldflags \ No newline at end of file diff --git a/main.go b/main.go index 752eacd..18ab43b 100644 --- a/main.go +++ b/main.go @@ -1,13 +1,15 @@ package main import ( + "flag" + "github.com/OhYee/blotter/api" "github.com/OhYee/blotter/http" "github.com/OhYee/blotter/output" "github.com/OhYee/blotter/register" ) -const ( +var ( addr = "127.0.0.1:50000" prefix = "/api/" ) @@ -16,8 +18,18 @@ var ( _version string ) -//go:generate /bin/bash ./build.bash +//go:generate /bin/bash ./generate.bash + +func parseFlags() { + flag.StringVar(&addr, "address", "127.0.0.1:50000", "listen address") + flag.StringVar(&prefix, "prefix", "/api/", "api url prefix") + + flag.Parse() +} + func main() { + parseFlags() + register.SetContext("version", _version) api.Register()