Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker and model #3

Merged
merged 7 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@

# Go workspace file
go.work

*.env
temp
Empty file removed build/.keep
Empty file.
14 changes: 14 additions & 0 deletions build/app.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM golang:1.22.0-alpine3.19 as build
WORKDIR /src
COPY go.mod go.sum /src/
RUN go mod download && go mod verify
COPY cmd /src/cmd
COPY internal /src/internal
COPY website /src/website
RUN go build -o /app/app /src/cmd/thunderlight/thunderlight.go

FROM alpine:3.19
WORKDIR /app
COPY --from=build /app/app /app/app
EXPOSE 80
CMD ["/app/app"]
Empty file removed deployment/.keep
Empty file.
46 changes: 46 additions & 0 deletions deployment/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
version: '3'

services:
# App container
app:
build:
context: ../
dockerfile: build/app.Dockerfile
image: thunderlight-app:latest
container_name: app
environment:
- MYSQL_LINK=${MYSQL_USER}:${MYSQL_PASSWORD}@tcp(database:3306)/${MYSQL_DATABASE}?charset=utf8&parseTime=True&loc=Local
networks:
- app-network
ports:
- 80:80
depends_on:
database:
condition: service_healthy

# MySQL container
database:
image: mysql:8.3.0
container_name: database
environment:
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_USER=${MYSQL_USER}
volumes:
- db-data:/var/lib/mysql:rw
networks:
- app-network
ports:
- 3306:3306
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
timeout: 20s
retries: 10


networks:
app-network:
driver: bridge
volumes:
db-data:
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/thunderlight-shogi/engine

go 1.22.0

require (
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
gorm.io/driver/mysql v1.5.4
gorm.io/gorm v1.25.7
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
gorm.io/driver/mysql v1.5.4 h1:igQmHfKcbaTVyAIHNhhB888vvxh8EdQ2uSUT0LPcBso=
gorm.io/driver/mysql v1.5.4/go.mod h1:9rYxJph/u9SWkWc9yY4XJ1F/+xO0S/ChOmbk3+Z5Tvs=
gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/gorm v1.25.7 h1:VsD6acwRjz2zFxGO50gPO6AkNs7KKnvfzUjHQhZDz/A=
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
Empty file removed internal/database/.keep
Empty file.
70 changes: 70 additions & 0 deletions internal/model/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package model

import (
"os"

"gorm.io/driver/mysql"
"gorm.io/gorm"
)

type Player uint

const (
Sente Player = iota
Gote
)

type FigureType struct {
Id uint `gorm:"primarykey"`
Name string
Moves []Move `gorm:"foreignKey:FigureTypeId"`
TurnFigureId *uint
TurnFigure *FigureType
}

type StartingPosition struct {
Id uint `gorm:"primarykey"`
Name string
Figures []StartingPositionFigure `gorm:"foreignKey:StartingPositionId"`
}

type Move struct {
Id uint `gorm:"primarykey"`
FigureTypeId uint
HorizontalShift int
VerticalShift int
}

type StartingPositionFigure struct {
Id uint `gorm:"primarykey"`
StartingPositionId uint
FigureTypeId uint
FigureType FigureType
HorizontalOffset uint
VerticalOffset uint
Player Player
}

var db *gorm.DB

func init() {
link, ok := os.LookupEnv("MYSQL_LINK")
if !ok {
panic("MYSQL_LINK environment variable is undefined")
}
var err error
db, err = gorm.Open(mysql.Open(link), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
db.AutoMigrate(&StartingPosition{}, &StartingPositionFigure{}, &FigureType{}, &Move{})

result := db.Find(&FigureType{})
if result.RowsAffected == 0 {
seed()
}
}

func GetDB() *gorm.DB {
return db
}
Loading
Loading