Browse Source

feat: Docker support

master
OhYee 1 year ago
parent
commit
dfec9fd2a2
Signed by: OhYee
GPG Key ID: 5A9E1F63ED274FBB
  1. 4
      .gitignore
  2. 33
      Dockerfile
  3. 37
      build.bash
  4. 12
      generate.bash
  5. 16
      main.go

4
.gitignore vendored

@ -3,3 +3,7 @@ __pycache__ @@ -3,3 +3,7 @@ __pycache__
.vscode
/.env
*.log
temp/
/blotter

33
Dockerfile

@ -1,28 +1,15 @@ @@ -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"]

37
build.bash

@ -1,10 +1,33 @@ @@ -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
# 拉取镜像
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.'

12
generate.bash

@ -0,0 +1,12 @@ @@ -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

16
main.go

@ -1,13 +1,15 @@ @@ -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 ( @@ -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()

Loading…
Cancel
Save