From 4868e4d7cf96b577560609b8e56016bb153b17e6 Mon Sep 17 00:00:00 2001 From: log101 Date: Thu, 30 May 2024 12:52:18 +0300 Subject: [PATCH] ci: add docker scripts --- .gitea/workflows/go-deploy.yaml | 17 ----------------- .gitignore | 1 + docker-compose.yml | 32 ++++++++++++++++++++++++++++++++ docker/go/Dockerfile | 12 ++++++++++++ main.go | 5 +++-- schema.sql | 4 ++++ 6 files changed, 52 insertions(+), 19 deletions(-) delete mode 100644 .gitea/workflows/go-deploy.yaml create mode 100644 docker-compose.yml create mode 100644 docker/go/Dockerfile diff --git a/.gitea/workflows/go-deploy.yaml b/.gitea/workflows/go-deploy.yaml deleted file mode 100644 index 2ca063e..0000000 --- a/.gitea/workflows/go-deploy.yaml +++ /dev/null @@ -1,17 +0,0 @@ -run-name: ${{ gitea.actor }}, deploy log101.dev services -on: [push] - -jobs: - build: - name: Build - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - name: Setup Go - uses: actions/setup-go@v5 - with: - go-version: "1.21.x" - - name: Install dependencies - run: go get . - - name: Build - run: go build -v ./... diff --git a/.gitignore b/.gitignore index 04d91ae..d98725e 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ go.work gin-bin TODO +.env diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..7519c45 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,32 @@ +version: '3.8' + +services: + web: + build: + dockerfile: docker/go/Dockerfile + container_name: go_gin_app + ports: + - "8000:8000" + depends_on: + db: + condition: service_healthy + env_file: + - .env + + db: + image: mysql:8.0 + container_name: mysql_db + restart: always + volumes: + - db_data:/var/lib/mysql + - ./schema.sql:/docker-entrypoint-initdb.d/schema.sql + env_file: + - .env + healthcheck: + test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"] + interval: 2s + timeout: 20s + retries: 10 + +volumes: + db_data: diff --git a/docker/go/Dockerfile b/docker/go/Dockerfile new file mode 100644 index 0000000..dc69898 --- /dev/null +++ b/docker/go/Dockerfile @@ -0,0 +1,12 @@ +FROM golang:1.22.3 + +WORKDIR /usr/src/app + +# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change +COPY go.mod go.sum ./ +RUN go mod download && go mod verify + +COPY . . +RUN go build -v -o /usr/local/bin/app ./... + +CMD ["app"] diff --git a/main.go b/main.go index 020b1da..1279f42 100644 --- a/main.go +++ b/main.go @@ -66,11 +66,12 @@ func countEmojis(postId string) ([]EmojiCount, error) { } func main() { + mysqlHost := os.Getenv("DBHOST") cfg := mysql.Config{ User: os.Getenv("DBUSER"), Passwd: os.Getenv("DBPASS"), Net: "tcp", - Addr: "127.0.0.1:3306", + Addr: mysqlHost + ":3306", DBName: "emojis", } // Get a database handle. @@ -145,5 +146,5 @@ func main() { c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"postId": postId, "results": emojiCounter}) }) - r.Run(":3001") + r.Run(":8000") } diff --git a/schema.sql b/schema.sql index 4743729..cd01a56 100644 --- a/schema.sql +++ b/schema.sql @@ -1,4 +1,8 @@ -- schema.sql +CREATE DATABASE IF NOT EXISTS emojis; + +USE emojis; + CREATE TABLE IF NOT EXISTS emoji_clicks ( id INT PRIMARY KEY AUTO_INCREMENT, post_id TEXT NOT NULL,