diff --git a/docker-compose.yaml b/docker-compose.yaml index 0e642a7..104843f 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -28,7 +28,7 @@ services: api: build: context: . - dockerfile: .docker/api/Dockerfile + dockerfile: ./docker/api/Dockerfile environment: - TZ=Asia/Tokyo depends_on: diff --git a/docker/api/Dockerfile b/docker/api/Dockerfile index 1598877..4a2f463 100644 --- a/docker/api/Dockerfile +++ b/docker/api/Dockerfile @@ -7,4 +7,4 @@ RUN go install github.com/cosmtrek/air@latest RUN go mod download WORKDIR /go/app/api -CMD [ "air", "-c", "air.toml"] +CMD [ "air", "-c", ".air.toml"] diff --git a/src/api/README.md b/src/api/README.md new file mode 100644 index 0000000..87d33bd --- /dev/null +++ b/src/api/README.md @@ -0,0 +1,9 @@ +# 祝日取得API + +## 設定できる環境変数 + +- `PSQL_HOSTNAME`:データベースのホスト名 +- `PSQL_PORT`:データベースのポート +- `PSQL_USERNAME`:データベースのユーザ名 +- `PSQL_PASSWORD`:データベースのパスワード +- `LOG_DIR`:ログの出力先 diff --git a/src/api/controller/bad_request.go b/src/api/controller/bad_request.go new file mode 100644 index 0000000..6d88ae8 --- /dev/null +++ b/src/api/controller/bad_request.go @@ -0,0 +1,15 @@ +package controller + +import ( + "net/http" + + "github.com/labstack/echo/v4" +) + +type BadRequest struct { + Message string `json:"message"` +} + +func BadRequestJson(c echo.Context, msg string) error { + return c.JSON(http.StatusBadRequest, BadRequest{msg}) +} diff --git a/src/api/controller/get_holidays.go b/src/api/controller/get_holidays.go new file mode 100644 index 0000000..1708f45 --- /dev/null +++ b/src/api/controller/get_holidays.go @@ -0,0 +1,39 @@ +package controller + +import ( + "net/http" + "time" + + "github.com/doug-martin/goqu/v9" + "github.com/kynmh69/go-ja-holidays/database" + "github.com/kynmh69/go-ja-holidays/model" + "github.com/labstack/echo/v4" +) + +const ( + TABLE_HOLIDAYS_JP = "holidays_jp" + COLUMN_DATE = "holiday_date" + LOCATION = "Asia/Tokyo" +) + +type HolidaysQ struct { + StartDate time.Time `query:"start-date"` + EndDate time.Time `query:"end-date"` +} + +func GetHolidays(c echo.Context) error { + logger := c.Logger() + h := new(HolidaysQ) + var holidays []model.HolidayData + db := database.GetDbConnection() + + if err := c.Bind(h); err != nil { + logger.Error("Bind err", err) + return BadRequestJson(c, "cannot bind query param") + } + logger.Debug(h) + dataSet := db.From(TABLE_HOLIDAYS_JP).Order(goqu.C(COLUMN_DATE).Asc()) + dataSet.ScanStructs(&holidays) + + return c.JSON(http.StatusOK, holidays) +} diff --git a/src/api/main.go b/src/api/main.go index 6fd40af..092db30 100644 --- a/src/api/main.go +++ b/src/api/main.go @@ -1,8 +1,18 @@ package main import ( + "fmt" + "io" + originLog "log" + "os" + "path/filepath" + "time" + + "github.com/kynmh69/go-ja-holidays/api/router" "github.com/kynmh69/go-ja-holidays/database" "github.com/kynmh69/go-ja-holidays/util" + "github.com/labstack/echo/v4" + "github.com/labstack/gommon/log" ) func init() { @@ -10,5 +20,42 @@ func init() { database.ConnectDatabase() } func main() { - -} \ No newline at end of file + e := initEcho() + loggerInitialize(e) + logger := e.Logger + router.MakeRoute(e) + logger.Fatal(e.Start(":80")) +} + +func initEcho() *echo.Echo { + return echo.New() +} + +func loggerInitialize(e *echo.Echo) { + logger := e.Logger + logger.SetPrefix("[APP]") + logger.SetLevel(log.DEBUG) + logger.SetOutput(initWriter()) +} + +func initWriter() io.Writer { + logDir, ok := os.LookupEnv("LOG_DIR") + if !ok { + logDir = "./log/" + } + os.Mkdir(logDir, 0755) + + return io.MultiWriter(os.Stdout, createFile(logDir)) +} + +func createFile(logDir string) *os.File { + now := time.Now() + nowStr := now.Format("2006-01-02-15-04-05") + logFileName := fmt.Sprintf("app_%s.log", nowStr) + logFile := filepath.Join(logDir, logFileName) + file, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE, 0644) + if err != nil { + originLog.Fatalln("Not open log file", err) + } + return file +} diff --git a/src/api/router/router.go b/src/api/router/router.go new file mode 100644 index 0000000..4d9d15a --- /dev/null +++ b/src/api/router/router.go @@ -0,0 +1,10 @@ +package router + +import ( + "github.com/kynmh69/go-ja-holidays/api/controller" + "github.com/labstack/echo/v4" +) + +func MakeRoute(e *echo.Echo) { + e.GET("/holidays", controller.GetHolidays) +} \ No newline at end of file diff --git a/src/go.mod b/src/go.mod index 7b39e9e..8cc5034 100644 --- a/src/go.mod +++ b/src/go.mod @@ -4,11 +4,19 @@ go 1.22.0 require ( github.com/doug-martin/goqu/v9 v9.19.0 + github.com/labstack/echo/v4 v4.11.4 + github.com/labstack/gommon v0.4.2 github.com/lib/pq v1.10.1 golang.org/x/text v0.14.0 ) require ( + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect github.com/stretchr/objx v0.5.2 // indirect - github.com/stretchr/testify v1.8.4 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasttemplate v1.2.2 // indirect + golang.org/x/crypto v0.17.0 // indirect + golang.org/x/net v0.19.0 // indirect + golang.org/x/sys v0.15.0 // indirect ) diff --git a/src/go.sum b/src/go.sum index 25c1fbe..45d366f 100644 --- a/src/go.sum +++ b/src/go.sum @@ -8,8 +8,17 @@ github.com/doug-martin/goqu/v9 v9.19.0 h1:PD7t1X3tRcUiSdc5TEyOFKujZA5gs3VSA7wxSv github.com/doug-martin/goqu/v9 v9.19.0/go.mod h1:nf0Wc2/hV3gYK9LiyqIrzBEVGlI8qW3GuDCEobC4wBQ= github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= +github.com/labstack/echo/v4 v4.11.4 h1:vDZmA+qNeh1pd/cCkEicDMrjtrnMGQ1QFI9gWN1zGq8= +github.com/labstack/echo/v4 v4.11.4/go.mod h1:noh7EvLwqDsmh/X/HWKPUl1AjzJrhyptRyEbQJfxen8= +github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= +github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU= github.com/lib/pq v1.10.1 h1:6VXZrLU0jHBYyAqrSPa+MgPfnSvTPuMgK+k0o5kVFWo= github.com/lib/pq v1.10.1/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -19,12 +28,24 @@ github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= +github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= +golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= +golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= diff --git a/src/model/holiday.go b/src/model/holiday.go index 3bfbad8..dfefcb1 100644 --- a/src/model/holiday.go +++ b/src/model/holiday.go @@ -3,6 +3,6 @@ package model import "time" type HolidayData struct { - Date time.Time `json:"date"` - Name string `json:"name"` + Date time.Time `json:"date" db:"holiday_date"` + Name string `json:"name" db:"holiday_name"` } diff --git a/src/updater/README.md b/src/updater/README.md index 74e19e1..bdd49e0 100644 --- a/src/updater/README.md +++ b/src/updater/README.md @@ -44,3 +44,10 @@ docker composeで実行します。 ```bash docker compose up -d ``` + +## 設定できる環境変数 + +- `PSQL_HOSTNAME`:データベースのホスト名 +- `PSQL_PORT`:データベースのポート +- `PSQL_USERNAME`:データベースのユーザ名 +- `PSQL_PASSWORD`:データベースのパスワード