From c52ba0c4f00561183fb269c3def4b51cbd0664cb Mon Sep 17 00:00:00 2001 From: RiccardoBiosas Date: Thu, 9 Jul 2020 06:36:07 +0200 Subject: [PATCH] :rocket: Created dockerfile for golang api, dockerfile for mysql db and docker-compose. --- Dockerfile | 17 +++++++++++++++++ api/api.go | 4 ++-- docker-compose.yml | 24 ++++++++++++++++++++++++ mysql/Dockerfile | 2 ++ mysql/test.sql | 7 +++++++ 5 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 mysql/Dockerfile create mode 100644 mysql/test.sql diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..96485c1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM golang:1.12 + +WORKDIR /app + +COPY go.mod go.sum ./ + +RUN go mod download + +COPY . . + +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build + +RUN go build -o main . + +EXPOSE 8080 + +CMD ["./main"] diff --git a/api/api.go b/api/api.go index f6670d3..0a95e26 100644 --- a/api/api.go +++ b/api/api.go @@ -6,7 +6,6 @@ import ( "fmt" "log" "net/http" - "os" "strings" helpers "github.com/RiccardoBiosas/golang-ethereum-auth/helpers" @@ -30,7 +29,8 @@ func (a *Api) Mount() { fmt.Println("error loading .env file") } var err error - a.DB, err = sql.Open("mysql", os.Getenv("DB_AUTH")) + // a.DB, err = sql.Open("mysql", os.Getenv("DB_AUTH")) + a.DB, err = sql.Open("mysql", "docker:docker@tcp(db:3306)/golang_ethereum_auth") if err != nil { log.Fatal(err) } diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..5ca8e96 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,24 @@ +version: '3' +services: + db: + build: + context: ./mysql + environment: + MYSQL_ROOT_PASSWORD: root + MYSQL_DATABASE: golang_ethereum_auth + MYSQL_USER: docker + MYSQL_PASSWORD: docker + container_name: golang_db + ports: + - '3306:3306' + tty: true + api: + build: . + volumes: + - '.:/go' + container_name: golang_api + ports: + - '8080:8080' + tty: true + depends_on: + - db diff --git a/mysql/Dockerfile b/mysql/Dockerfile new file mode 100644 index 0000000..4f5883f --- /dev/null +++ b/mysql/Dockerfile @@ -0,0 +1,2 @@ +FROM mysql:5.6 +COPY test.sql /docker-entrypoint-initdb.d/golang_ethereum_auth.sql \ No newline at end of file diff --git a/mysql/test.sql b/mysql/test.sql new file mode 100644 index 0000000..5141376 --- /dev/null +++ b/mysql/test.sql @@ -0,0 +1,7 @@ +USE golang_ethereum_auth; + +Create table `users` ( + id MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY, + public_key VARCHAR(42) UNIQUE NOT NULL, + nonce varchar(38) NOT NULL +); \ No newline at end of file