Skip to content

Commit

Permalink
Dockerize the app
Browse files Browse the repository at this point in the history
- Since sqlite has problems with CGO=0 compiling, moving to mysql!
  • Loading branch information
cemalkilic committed Jan 1, 2021
1 parent e0187fb commit afea2a0
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 6 deletions.
27 changes: 27 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Build the Go API
FROM golang:1.15 AS builder
ADD . /app
WORKDIR /app/
RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-w" -a -o /main .

# Build the React app
FROM node:alpine3.10 AS node_builder
COPY --from=builder /app/frontend ./
# node-sass needs python & others :(
RUN apk --no-cache --virtual build-dependencies add \
python \
make \
g++ \
bash
RUN yarn install
RUN yarn build
RUN apk del build-dependencies

FROM alpine:latest
RUN apk --no-cache add ca-certificates
COPY --from=builder /main ./
COPY --from=node_builder /build ./frontend/build
RUN chmod +x ./main
EXPOSE 8080
CMD ./main
16 changes: 13 additions & 3 deletions database/sqlite.go → database/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package database
import (
"database/sql"
"errors"
"fmt"
"github.com/cemalkilic/jsonServer/models"
"log"
"os"

_ "github.com/mattn/go-sqlite3"
_ "github.com/go-sql-driver/mysql"
)

type sqlDatabase struct {
Expand All @@ -17,12 +19,20 @@ var db DataStore

func Init() {

sqliteDatabase, err := sql.Open("sqlite3", "./sqlite-database.db")
mysqlUsername := os.Getenv("MYSQL_USER")
mysqlPassword := os.Getenv("MYSQL_PASS")
mysqlDBName := os.Getenv("MYSQL_DB")
mysqlPort := os.Getenv("MYSQL_PORT")
mysqlHost := os.Getenv("MYSQL_HOST")

connStr := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", mysqlUsername, mysqlPassword, mysqlHost, mysqlPort, mysqlDBName)

database, err := sql.Open("mysql", connStr)
if err != nil {
log.Fatal(err)
}

db = &sqlDatabase{db: sqliteDatabase}
db = &sqlDatabase{db: database}
}

func GetDB() DataStore {
Expand Down
29 changes: 29 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: '3.7'
services:
app:
build: .
environment:
MYSQL_USER: root
MYSQL_PASS: root
MYSQL_DB: jsonServer
MYSQL_PORT: 3306
MYSQL_HOST: db_mysql
depends_on:
- database
ports:
- "80:8080"
database:
container_name: db_mysql
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test_db
ports:
- "3306:3306"
volumes:
- db-data/:/var/lib/mysql
- $HOME/Desktop/MySQL-Snippets/school.sql:/school.sql
volumes:
db-data:

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ require (
github.com/gin-contrib/cors v1.3.1
github.com/gin-gonic/gin v1.6.3
github.com/go-playground/validator/v10 v10.4.1
github.com/mattn/go-sqlite3 v1.14.6
github.com/go-sql-driver/mysql v1.5.0
github.com/stretchr/testify v1.6.1
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
Expand All @@ -38,8 +40,6 @@ github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgx
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
Expand Down

0 comments on commit afea2a0

Please sign in to comment.