From cfa7291b83d0c1bd70e99ee125996eeaa1329cdf Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sat, 28 Sep 2024 23:37:46 +0900 Subject: [PATCH 01/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compose.yaml | 2 +- src/api/.air.toml | 2 +- src/api/controller/bad_request.go | 15 -- src/api/controller/count_holidays.go | 58 ----- src/api/controller/count_holidays_test.go | 181 ---------------- src/api/controller/get_holidays.go | 126 ----------- src/api/controller/get_holidays_test.go | 246 ---------------------- src/api/controller/is_holiday.go | 51 ----- src/api/controller/is_holiday_test.go | 86 -------- src/api/handler/bad_request.go | 14 ++ src/api/handler/count_holidays.go | 58 +++++ src/api/handler/count_holidays_test.go | 43 ++++ src/api/handler/get_holidays.go | 67 ++++++ src/api/handler/is_holiday.go | 58 +++++ src/api/main.go | 25 ++- src/api/router/router.go | 17 +- src/api/router/router_test.go | 64 +----- src/database/connect.go | 20 +- src/go.mod | 50 +++-- src/go.sum | 118 +++++++---- src/key_management/controller/key_test.go | 2 +- src/key_management/main.go | 6 +- src/logging/logger.go | 27 +++ src/logging/logger_test.go | 43 ++++ src/middleware/common.go | 16 -- src/middleware/common_test.go | 44 ---- src/middleware/logger.go | 25 --- src/middleware/logger_test.go | 68 ------ src/middleware/prometheus.go | 10 - src/middleware/prometheus_test.go | 24 --- src/middleware/redirect.go | 35 --- src/updater/main.go | 3 +- src/util/logger_initialize.go | 72 ------- src/util/unit.go | 11 + 34 files changed, 475 insertions(+), 1212 deletions(-) delete mode 100644 src/api/controller/bad_request.go delete mode 100644 src/api/controller/count_holidays.go delete mode 100644 src/api/controller/count_holidays_test.go delete mode 100644 src/api/controller/get_holidays.go delete mode 100644 src/api/controller/get_holidays_test.go delete mode 100644 src/api/controller/is_holiday.go delete mode 100644 src/api/controller/is_holiday_test.go create mode 100644 src/api/handler/bad_request.go create mode 100644 src/api/handler/count_holidays.go create mode 100644 src/api/handler/count_holidays_test.go create mode 100644 src/api/handler/get_holidays.go create mode 100644 src/api/handler/is_holiday.go create mode 100644 src/logging/logger.go create mode 100644 src/logging/logger_test.go delete mode 100644 src/middleware/common.go delete mode 100644 src/middleware/common_test.go delete mode 100644 src/middleware/logger.go delete mode 100644 src/middleware/logger_test.go delete mode 100644 src/middleware/prometheus.go delete mode 100644 src/middleware/prometheus_test.go delete mode 100644 src/middleware/redirect.go delete mode 100644 src/util/logger_initialize.go create mode 100644 src/util/unit.go diff --git a/compose.yaml b/compose.yaml index 47112d6..e7a1752 100644 --- a/compose.yaml +++ b/compose.yaml @@ -39,7 +39,7 @@ services: volumes: - ./src:/go/app:rw ports: - - 8080:80 + - "8080:8080" key_manager: build: diff --git a/src/api/.air.toml b/src/api/.air.toml index 49468ed..fce1e22 100644 --- a/src/api/.air.toml +++ b/src/api/.air.toml @@ -22,7 +22,7 @@ tmp_dir = "tmp" poll_interval = 0 post_cmd = [] pre_cmd = [] - rerun = false + rerun = true rerun_delay = 500 send_interrupt = false stop_on_error = false diff --git a/src/api/controller/bad_request.go b/src/api/controller/bad_request.go deleted file mode 100644 index 6d88ae8..0000000 --- a/src/api/controller/bad_request.go +++ /dev/null @@ -1,15 +0,0 @@ -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/count_holidays.go b/src/api/controller/count_holidays.go deleted file mode 100644 index fc16b61..0000000 --- a/src/api/controller/count_holidays.go +++ /dev/null @@ -1,58 +0,0 @@ -package controller - -import ( - "net/http" - "time" - - "github.com/doug-martin/goqu/v9" - "github.com/kynmh69/go-ja-holidays/database" - "github.com/labstack/echo/v4" -) - -type CountStruct struct { - Count int64 `json:"count"` -} - -func CountHolidays(c echo.Context) error { - logger := c.Logger() - if location, err := time.LoadLocation(LOCATION); err != nil { - return BadRequestJson(c, err.Error()) - } else { - goqu.SetTimeLocation(location) - } - - startDay, err := getStartDate(c) - if err != nil { - return BadRequestJson(c, err.Error()) - } - - endDay, err := getEndDate(c) - if err != nil { - return BadRequestJson(c, err.Error()) - } - - db := database.GetDbConnection() - - dataSet := db.From(TABLE_HOLIDAYS_JP) - if startDay != nil && endDay != nil { - dataSet = dataSet.Where( - goqu.C(COLUMN_DATE).Gte(startDay), - goqu.C(COLUMN_DATE).Lte(endDay), - ) - } else if startDay != nil { - dataSet = dataSet.Where( - goqu.C(COLUMN_DATE).Gte(startDay), - ) - } else if endDay != nil { - dataSet = dataSet.Where( - goqu.C(COLUMN_DATE).Lte(endDay), - ) - } - count, err := dataSet.Count() - if err != nil { - return BadRequestJson(c, err.Error()) - } - stru := CountStruct{Count: count} - logger.Debug("count:", stru) - return c.JSON(http.StatusOK, stru) -} diff --git a/src/api/controller/count_holidays_test.go b/src/api/controller/count_holidays_test.go deleted file mode 100644 index 4b2b864..0000000 --- a/src/api/controller/count_holidays_test.go +++ /dev/null @@ -1,181 +0,0 @@ -package controller - -import ( - "net/http" - "net/http/httptest" - "net/url" - "testing" - - "github.com/labstack/echo/v4" -) - -func TestCountHolidays(t *testing.T) { - e := echo.New() - q := make(url.Values) - q.Set("start-day", "2022-01-01") - q.Set("end-day", "2023-01-01") - req := httptest.NewRequest(http.MethodGet, "/holidays/count?"+q.Encode(), nil) - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) - rec := httptest.NewRecorder() - c := e.NewContext(req, rec) - - type args struct { - c echo.Context - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "test OK", - args: args{ - c: c, - }, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CountHolidays(tt.args.c); (err != nil) != tt.wantErr { - t.Errorf("CountHolidays() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestCountHolidaysStartDay(t *testing.T) { - e := echo.New() - q := make(url.Values) - q.Set("start-day", "2022-01-01") - req := httptest.NewRequest(http.MethodGet, "/holidays/count?"+q.Encode(), nil) - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) - rec := httptest.NewRecorder() - c := e.NewContext(req, rec) - - type args struct { - c echo.Context - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "test OK", - args: args{ - c: c, - }, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CountHolidays(tt.args.c); (err != nil) != tt.wantErr { - t.Errorf("CountHolidays() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestCountHolidaysEndDay(t *testing.T) { - e := echo.New() - q := make(url.Values) - q.Set("end-day", "2022-01-01") - req := httptest.NewRequest(http.MethodGet, "/holidays/count?"+q.Encode(), nil) - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) - rec := httptest.NewRecorder() - c := e.NewContext(req, rec) - - type args struct { - c echo.Context - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "test OK", - args: args{ - c: c, - }, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CountHolidays(tt.args.c); (err != nil) != tt.wantErr { - t.Errorf("CountHolidays() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestCountHolidaysStartDayErr(t *testing.T) { - e := echo.New() - q := make(url.Values) - q.Set("start-day", "2022-01-0") - req := httptest.NewRequest(http.MethodGet, "/holidays/count?"+q.Encode(), nil) - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) - rec := httptest.NewRecorder() - c := e.NewContext(req, rec) - - type args struct { - c echo.Context - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "test err", - args: args{ - c: c, - }, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CountHolidays(tt.args.c); (err != nil) != tt.wantErr { - t.Errorf("CountHolidays() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestCountHolidaysEndDayErr(t *testing.T) { - e := echo.New() - q := make(url.Values) - q.Set("end-day", "2022-01-0") - req := httptest.NewRequest(http.MethodGet, "/holidays/count?"+q.Encode(), nil) - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) - rec := httptest.NewRecorder() - c := e.NewContext(req, rec) - - type args struct { - c echo.Context - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "test err", - args: args{ - c: c, - }, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CountHolidays(tt.args.c); (err != nil) != tt.wantErr { - t.Errorf("CountHolidays() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} diff --git a/src/api/controller/get_holidays.go b/src/api/controller/get_holidays.go deleted file mode 100644 index a7e1908..0000000 --- a/src/api/controller/get_holidays.go +++ /dev/null @@ -1,126 +0,0 @@ -package controller - -import ( - "errors" - "log" - "net/http" - "regexp" - "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" -) - -func GetHolidays(c echo.Context) error { - var holidays []model.HolidayData - db := database.GetDbConnection() - if location, err := time.LoadLocation(LOCATION); err != nil { - return BadRequestJson(c, err.Error()) - } else { - goqu.SetTimeLocation(location) - } - - startDay, err := getStartDate(c) - if err != nil { - return BadRequestJson(c, err.Error()) - } - - endDay, err := getEndDate(c) - if err != nil { - return BadRequestJson(c, err.Error()) - } - - dataSet := db.From(TABLE_HOLIDAYS_JP).Order(goqu.C(COLUMN_DATE).Asc()) - if startDay != nil && endDay != nil { - dataSet = dataSet.Where( - goqu.C(COLUMN_DATE).Gte(startDay), - goqu.C(COLUMN_DATE).Lte(endDay), - ) - } else if startDay != nil { - dataSet = dataSet.Where( - goqu.C(COLUMN_DATE).Gte(startDay), - ) - } else if endDay != nil { - dataSet = dataSet.Where( - goqu.C(COLUMN_DATE).Lte(endDay), - ) - } - dataSet.ScanStructs(&holidays) - - return c.JSON(http.StatusOK, holidays) -} - -func getStartDate(c echo.Context) (*time.Time, error) { - logger := c.Logger() - startDateStr := c.QueryParam("start-day") - logger.Debug("start-day: ", startDateStr) - if startDateStr == "" { - return nil, nil - } - startDate, err := createTime(c, startDateStr) - if err != nil { - return nil, err - } - if startDate == nil { - return nil, errors.New("cannot parse start day") - } - - return startDate, nil -} - -func getEndDate(c echo.Context) (*time.Time, error) { - logger := c.Logger() - endDateStr := c.QueryParam("end-day") - logger.Debug("end-day: ", endDateStr) - if endDateStr == "" { - return nil, nil - } - endDate, err := createTime(c, endDateStr) - if err != nil { - return nil, err - } - if endDate == nil { - return nil, errors.New("cannot parse end day") - } - - return endDate, nil -} - -func isValidDate(dateStr string) bool { - // 正規表現パターンを作成 - pattern := `^\d{4}-\d{2}-\d{2}$` - matched, err := regexp.MatchString(pattern, dateStr) - if err != nil { - return false - } - return matched -} - -func createTime(c echo.Context, dateStr string) (*time.Time, error) { - logger := c.Logger() - layout := "2006-01-02" - location, err := time.LoadLocation(LOCATION) - if err != nil { - log.Fatalln(err) - } - if !isValidDate(dateStr) { - return nil, errors.New("the date format is invalid. like " + layout) - } - - parseTime, err := time.ParseInLocation(layout, dateStr, location) - logger.Debug(parseTime) - - if err != nil { - return nil, err - } - - return &parseTime, nil -} diff --git a/src/api/controller/get_holidays_test.go b/src/api/controller/get_holidays_test.go deleted file mode 100644 index 13b3a32..0000000 --- a/src/api/controller/get_holidays_test.go +++ /dev/null @@ -1,246 +0,0 @@ -package controller - -import ( - "log" - "net/http" - "net/http/httptest" - "net/url" - "os" - "reflect" - "testing" - "time" - - "github.com/kynmh69/go-ja-holidays/database" - "github.com/kynmh69/go-ja-holidays/util" - "github.com/labstack/echo/v4" -) - -func TestMain(m *testing.M) { - setUp() - defer tearDown() - res := m.Run() - os.Exit(res) -} -func TestGetHolidays(t *testing.T) { - // Setup - e := echo.New() - q := make(url.Values) - q.Set("end-day", "2023-01-01") - req := httptest.NewRequest(http.MethodGet, "/holidays", nil) - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) - rec := httptest.NewRecorder() - c := e.NewContext(req, rec) - type args struct { - c echo.Context - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "OK", - args: args{ - c: c, - }, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := GetHolidays(tt.args.c); (err != nil) != tt.wantErr { - t.Errorf("GetHolidays() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_getStartDate(t *testing.T) { - // Setup - e := echo.New() - q := make(url.Values) - q.Set("start-day", "2023-01-01") - req := httptest.NewRequest(http.MethodGet, "/holidays?"+q.Encode(), nil) - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) - rec := httptest.NewRecorder() - c := e.NewContext(req, rec) - loc, _ := time.LoadLocation("Asia/Tokyo") - ti := time.Date(2023, 1, 1, 0, 0, 0, 0, loc) - - type args struct { - c echo.Context - } - tests := []struct { - name string - args args - want *time.Time - wantErr bool - }{ - { - name: "OK", - args: args{ - c: c, - }, - want: &ti, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := getStartDate(tt.args.c) - if (err != nil) != tt.wantErr { - t.Errorf("getStartDate() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("getStartDate() = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_getEndDate(t *testing.T) { - // Setup - e := echo.New() - q := make(url.Values) - q.Set("end-day", "2023-01-01") - req := httptest.NewRequest(http.MethodGet, "/holidays?"+q.Encode(), nil) - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) - rec := httptest.NewRecorder() - c := e.NewContext(req, rec) - loc, _ := time.LoadLocation("Asia/Tokyo") - ti := time.Date(2023, 1, 1, 0, 0, 0, 0, loc) - type args struct { - c echo.Context - } - tests := []struct { - name string - args args - want *time.Time - wantErr bool - }{ - { - name: "ok", - args: args{ - c: c, - }, - want: &ti, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := getEndDate(tt.args.c) - if (err != nil) != tt.wantErr { - t.Errorf("getEndDate() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("getEndDate() = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_isValidDate(t *testing.T) { - type args struct { - dateStr string - } - tests := []struct { - name string - args args - want bool - }{ - { - name: "ok", - args: args{ - "2023-01-01", - }, - want: true, - }, - { - name: "ng", - args: args{ - "2023-01-0", - }, - want: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := isValidDate(tt.args.dateStr); got != tt.want { - t.Errorf("isValidDate() = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_createTime(t *testing.T) { - // Setup - e := echo.New() - req := httptest.NewRequest(http.MethodGet, "/holidays", nil) - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) - rec := httptest.NewRecorder() - c := e.NewContext(req, rec) - loc, _ := time.LoadLocation("Asia/Tokyo") - ti := time.Date(2023, 1, 1, 0, 0, 0, 0, loc) - type args struct { - c echo.Context - dateStr string - } - tests := []struct { - name string - args args - want *time.Time - wantErr bool - }{ - { - name: "ok", - args: args{ - c: c, - dateStr: "2023-01-01", - }, - want: &ti, - wantErr: false, - }, - { - name: "NG", - args: args{ - c: c, - dateStr: "2023-01-0", - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := createTime(tt.args.c, tt.args.dateStr) - if (err != nil) != tt.wantErr { - t.Errorf("createTime() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("createTime() = %v, want %v", got, tt.want) - } - }) - } -} - -func setUp() { - os.Setenv("PSQL_HOSTNAME", "localhost") - os.Setenv("DATABASE", "unittest") - url := "https://www8.cao.go.jp/chosei/shukujitsu/syukujitsu.csv" - database.ConnectDatabase() - util.CreateHolidayData(url) -} - -func tearDown() { - os.Unsetenv("PSQL_HOSTNAME") - os.Unsetenv("DATABASE") - db := database.GetDbConnection() - if _, err := db.Delete("holidays_jp").Executor().Exec(); err != nil { - log.Fatalln(err) - } - log.Println("Teardown.") -} diff --git a/src/api/controller/is_holiday.go b/src/api/controller/is_holiday.go deleted file mode 100644 index a882436..0000000 --- a/src/api/controller/is_holiday.go +++ /dev/null @@ -1,51 +0,0 @@ -package controller - -import ( - "net/http" - - "github.com/doug-martin/goqu/v9" - "github.com/kynmh69/go-ja-holidays/database" - "github.com/kynmh69/go-ja-holidays/model" - "github.com/kynmh69/go-ja-holidays/util" - "github.com/labstack/echo/v4" -) - -func IsHoliday(c echo.Context) error { - var holiday model.HolidayData - db := database.GetDbConnection() - - logger := c.Logger() - - day := c.Param("day") - logger.Debug(day) - if day == "" { - msg := "cannot get day." - logger.Warn(msg) - return BadRequestJson(c, msg) - } - dayTime, err := util.ParseDay(day) - if err != nil { - return BadRequestJson(c, err.Error()) - } - - loc := dayTime.Location() - goqu.SetTimeLocation(loc) - - dataSet := db.From(TABLE_HOLIDAYS_JP). - Where(goqu.C(COLUMN_DATE).Eq(dayTime)) - ok, err := dataSet.ScanStruct(&holiday) - - if err != nil { - logger.Error(err) - return BadRequestJson(c, err.Error()) - } - - var isHoliday model.IsHoliday - if ok { - isHoliday = model.IsHoliday{IsHoliday: ok, HolidayData: holiday} - } else { - isHoliday = model.IsHoliday{IsHoliday: ok, HolidayData: model.HolidayData{Date: *dayTime}} - } - logger.Debug(isHoliday) - return c.JSON(http.StatusOK, isHoliday) -} diff --git a/src/api/controller/is_holiday_test.go b/src/api/controller/is_holiday_test.go deleted file mode 100644 index d7387ad..0000000 --- a/src/api/controller/is_holiday_test.go +++ /dev/null @@ -1,86 +0,0 @@ -package controller - -import ( - "net/http" - "net/http/httptest" - "testing" - - "github.com/kynmh69/go-ja-holidays/util" - "github.com/labstack/echo/v4" -) - -func TestIsHoliday(t *testing.T) { - // Setup - e := echo.New() - util.EchoLoggerInitialize(e) - req := httptest.NewRequest(http.MethodGet, "/", nil) - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) - rec := httptest.NewRecorder() - c := e.NewContext(req, rec) - c.SetPath("/holidays/:day") - c.SetParamNames("day") - c.SetParamValues("2023-01-01") - - type args struct { - c echo.Context - } - tests := []struct { - name string - args args - wantErr bool - }{ - // TODO: Add test cases. - { - name: "OK", - args: args{ - c: c, - }, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := IsHoliday(tt.args.c); (err != nil) != tt.wantErr { - t.Errorf("IsHoliday() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestIsHolidayFmErr(t *testing.T) { - // Setup - e := echo.New() - util.EchoLoggerInitialize(e) - req := httptest.NewRequest(http.MethodGet, "/", nil) - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) - rec := httptest.NewRecorder() - c := e.NewContext(req, rec) - c.SetPath("/holidays/:day") - c.SetParamNames("day") - c.SetParamValues("2023-01-0") - - type args struct { - c echo.Context - } - tests := []struct { - name string - args args - wantErr bool - }{ - // TODO: Add test cases. - { - name: "NG", - args: args{ - c: c, - }, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := IsHoliday(tt.args.c); (err != nil) != tt.wantErr { - t.Errorf("IsHoliday() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} diff --git a/src/api/handler/bad_request.go b/src/api/handler/bad_request.go new file mode 100644 index 0000000..d46fab6 --- /dev/null +++ b/src/api/handler/bad_request.go @@ -0,0 +1,14 @@ +package handler + +import ( + "github.com/gin-gonic/gin" + "net/http" +) + +type BadRequest struct { + Message string `json:"message"` +} + +func BadRequestJson(c *gin.Context, msg string) { + c.AbortWithStatusJSON(http.StatusBadRequest, BadRequest{msg}) +} diff --git a/src/api/handler/count_holidays.go b/src/api/handler/count_holidays.go new file mode 100644 index 0000000..5b8dda6 --- /dev/null +++ b/src/api/handler/count_holidays.go @@ -0,0 +1,58 @@ +package handler + +import ( + "github.com/gin-gonic/gin" + "github.com/kynmh69/go-ja-holidays/logging" + "net/http" + "time" + + "github.com/doug-martin/goqu/v9" + "github.com/kynmh69/go-ja-holidays/database" +) + +type CountStruct struct { + Count int64 `json:"count"` +} + +func CountHolidays(c *gin.Context) { + logger := logging.GetLogger() + var request HolidaysRequest + if location, err := time.LoadLocation(LOCATION); err != nil { + BadRequestJson(c, err.Error()) + return + } else { + goqu.SetTimeLocation(location) + } + + if err := c.ShouldBindQuery(&request); err != nil { + logger.Error(err) + BadRequestJson(c, err.Error()) + return + } + + db := database.GetDbConnection() + + dataSet := db.From(TableHolidaysJp) + if request.StartDay != "" && request.EndDay != "" { + dataSet = dataSet.Where( + goqu.C(ColumnDate).Gte(request.StartDay), + goqu.C(ColumnDate).Lte(request.EndDay), + ) + } else if request.StartDay != "" { + dataSet = dataSet.Where( + goqu.C(ColumnDate).Gte(request.StartDay), + ) + } else if request.EndDay != "" { + dataSet = dataSet.Where( + goqu.C(ColumnDate).Lte(request.EndDay), + ) + } + count, err := dataSet.Count() + if err != nil { + BadRequestJson(c, err.Error()) + return + } + cs := CountStruct{Count: count} + logger.Debug("count:", cs) + c.JSON(http.StatusOK, cs) +} diff --git a/src/api/handler/count_holidays_test.go b/src/api/handler/count_holidays_test.go new file mode 100644 index 0000000..5828a1f --- /dev/null +++ b/src/api/handler/count_holidays_test.go @@ -0,0 +1,43 @@ +package handler + +import ( + "github.com/gin-gonic/gin" + "github.com/kynmh69/go-ja-holidays/logging" + "github.com/kynmh69/go-ja-holidays/util" + "net/http/httptest" + "testing" +) + +func TestMain(m *testing.M) { + util.SetUp() + code := m.Run() + if code > 0 { + logging.GetLogger().Error("exitcode: ", code) + } +} + +func TestCountHolidays(t *testing.T) { + r := gin.Default() + r.GET("/holidays/count", CountHolidays) + w := httptest.NewRecorder() + req := httptest.NewRequest("GET", "/holidays/count", nil) + context := gin.CreateTestContextOnly(w, r) + context.Request = req + type args struct { + c *gin.Context + } + tests := []struct { + name string + args args + }{ + { + name: "TestCountHolidays", + args: args{c: context}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + CountHolidays(tt.args.c) + }) + } +} diff --git a/src/api/handler/get_holidays.go b/src/api/handler/get_holidays.go new file mode 100644 index 0000000..dadbd84 --- /dev/null +++ b/src/api/handler/get_holidays.go @@ -0,0 +1,67 @@ +package handler + +import ( + "github.com/gin-gonic/gin" + "net/http" + "time" + + "github.com/doug-martin/goqu/v9" + "github.com/kynmh69/go-ja-holidays/database" + "github.com/kynmh69/go-ja-holidays/model" +) + +const ( + TableHolidaysJp = "holidays_jp" + ColumnDate = "holiday_date" + LOCATION = "Asia/Tokyo" +) + +type HolidaysRequest struct { + StartDay string `form:"start-day" binding:"datetime"` + EndDay string `form:"end-day" binding:"datetime"` +} + +func GetHolidays(c *gin.Context) { + var ( + reqParams HolidaysRequest + holidays []model.HolidayData + ) + // DB接続 + db := database.GetDbConnection() + // タイムゾーン設定 + if location, err := time.LoadLocation(LOCATION); err != nil { + BadRequestJson(c, err.Error()) + return + } else { + // クエリビルダーにタイムゾーンを設定 + goqu.SetTimeLocation(location) + } + // リクエストパラメータを取得 + if err := c.BindQuery(&reqParams); err != nil { + BadRequestJson(c, err.Error()) + return + } + // リクエストパラメータから開始日と終了日を取得 + dataSet := db.From(TableHolidaysJp).Order(goqu.C(ColumnDate).Asc()) + if reqParams.StartDay != "" && reqParams.EndDay != "" { + dataSet = dataSet.Where( + goqu.C(ColumnDate).Gte(reqParams.StartDay), + goqu.C(ColumnDate).Lte(reqParams.EndDay), + ) + } else if reqParams.StartDay != "" { + dataSet = dataSet.Where( + goqu.C(ColumnDate).Gte(reqParams.StartDay), + ) + } else if reqParams.EndDay != "" { + dataSet = dataSet.Where( + goqu.C(ColumnDate).Lte(reqParams.EndDay), + ) + } + // データ取得 + if err := dataSet.ScanStructs(&holidays); err != nil { + // エラー時はBad Requestを返却 + BadRequestJson(c, err.Error()) + return + } + c.JSON(http.StatusOK, holidays) +} diff --git a/src/api/handler/is_holiday.go b/src/api/handler/is_holiday.go new file mode 100644 index 0000000..382c7e0 --- /dev/null +++ b/src/api/handler/is_holiday.go @@ -0,0 +1,58 @@ +package handler + +import ( + "net/http" + "time" + + "github.com/doug-martin/goqu/v9" + "github.com/gin-gonic/gin" + + "github.com/kynmh69/go-ja-holidays/database" + "github.com/kynmh69/go-ja-holidays/logging" + "github.com/kynmh69/go-ja-holidays/model" +) + +type HolidayRequest struct { + holiday *time.Time `query:"day" binding:"date"` +} + +func IsHoliday(c *gin.Context) { + var ( + request HolidayRequest + holiday model.HolidayData + ) + db := database.GetDbConnection() + + logger := logging.GetLogger() + + if err := c.BindQuery(&request); err != nil { + logger.Error(err) + BadRequestJson(c, err.Error()) + return + } + logger.Debug(request.holiday) + + // Set the time zone to JST. + loc := request.holiday.Location() + goqu.SetTimeLocation(loc) + + // Get the holiday data for the specified day. + dataSet := db.From(TableHolidaysJp). + Where(goqu.C(ColumnDate).Eq(request.holiday)) + ok, err := dataSet.ScanStruct(&holiday) + + if err != nil { + logger.Error(err) + BadRequestJson(c, err.Error()) + return + } + + var isHoliday model.IsHoliday + if ok { + isHoliday = model.IsHoliday{IsHoliday: ok, HolidayData: holiday} + } else { + isHoliday = model.IsHoliday{IsHoliday: ok, HolidayData: model.HolidayData{Date: *request.holiday}} + } + logger.Debug(isHoliday) + c.JSON(http.StatusOK, isHoliday) +} diff --git a/src/api/main.go b/src/api/main.go index 07d94db..d3fae82 100644 --- a/src/api/main.go +++ b/src/api/main.go @@ -1,25 +1,28 @@ package main import ( + "github.com/gin-gonic/gin" "github.com/kynmh69/go-ja-holidays/api/router" "github.com/kynmh69/go-ja-holidays/database" - "github.com/kynmh69/go-ja-holidays/middleware" - "github.com/kynmh69/go-ja-holidays/util" - "github.com/labstack/echo/v4" + "github.com/kynmh69/go-ja-holidays/logging" ) func init() { - util.LoggerInitialize() + logging.LoggerInitialize() database.ConnectDatabase() } func main() { - e := initEcho() - middleware.SetMiddleware(e) - logger := e.Logger - router.MakeRoute(e) - logger.Fatal(e.Start(":80")) + logger := logging.GetLogger() + r := initGin() + router.MakeRoute(r) + err := r.Run(":8080") + if err != nil { + // Runできなかったらエラーログを出力して終了 + logger.Error(err) + return + } } -func initEcho() *echo.Echo { - return echo.New() +func initGin() *gin.Engine { + return gin.Default() } diff --git a/src/api/router/router.go b/src/api/router/router.go index 5b780be..b61f403 100644 --- a/src/api/router/router.go +++ b/src/api/router/router.go @@ -1,17 +1,12 @@ package router import ( - "github.com/kynmh69/go-ja-holidays/api/controller" - "github.com/labstack/echo-contrib/echoprometheus" - "github.com/labstack/echo/v4" + "github.com/gin-gonic/gin" + "github.com/kynmh69/go-ja-holidays/api/handler" ) -func MakeRoute(e *echo.Echo) { - e.GET("/holidays", controller.GetHolidays) - e.GET("/holidays/:day", controller.IsHoliday) - e.GET("/holidays/count", controller.CountHolidays) -} - -func SetPrometheusHandler(e *echo.Echo) { - e.GET("/metrics", echoprometheus.NewHandler()) +func MakeRoute(r *gin.Engine) { + r.GET("/holidays", handler.GetHolidays) + r.GET("/holidays/:day", handler.IsHoliday) + r.GET("/holidays/count", handler.CountHolidays) } diff --git a/src/api/router/router_test.go b/src/api/router/router_test.go index c3e03be..423bda0 100644 --- a/src/api/router/router_test.go +++ b/src/api/router/router_test.go @@ -1,79 +1,23 @@ package router import ( - "log" - "os" + "github.com/gin-gonic/gin" "testing" - - "github.com/kynmh69/go-ja-holidays/database" - "github.com/kynmh69/go-ja-holidays/util" - "github.com/labstack/echo/v4" ) -func TestMain(m *testing.M) { - setUp() - defer tearDown() - res := m.Run() - os.Exit(res) -} func TestMakeRoute(t *testing.T) { - e := echo.New() - type args struct { - e *echo.Echo - } - tests := []struct { - name string - args args - }{ - { - name: "ok", - args: args{e}, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - MakeRoute(tt.args.e) - }) - } -} - -func setUp() { - os.Setenv("PSQL_HOSTNAME", "localhost") - os.Setenv("DATABASE", "unittest") - url := "https://www8.cao.go.jp/chosei/shukujitsu/syukujitsu.csv" - database.ConnectDatabase() - util.CreateHolidayData(url) -} - -func tearDown() { - os.Unsetenv("PSQL_HOSTNAME") - os.Unsetenv("DATABASE") - db := database.GetDbConnection() - if _, err := db.Delete("holidays_jp").Executor().Exec(); err != nil { - log.Fatalln(err) - } - log.Println("Teardown.") -} - -func TestSetPrometheusHandler(t *testing.T) { - e := echo.New() type args struct { - e *echo.Echo + r *gin.Engine } tests := []struct { name string args args }{ - { - name: "test ok", - args: args{ - e: e, - }, - }, + // TODO: Add test cases. } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - SetPrometheusHandler(tt.args.e) + MakeRoute(tt.args.r) }) } } diff --git a/src/database/connect.go b/src/database/connect.go index a91fda9..ee9868a 100644 --- a/src/database/connect.go +++ b/src/database/connect.go @@ -3,7 +3,7 @@ package database import ( "database/sql" "fmt" - "log" + "github.com/kynmh69/go-ja-holidays/logging" "os" _ "github.com/lib/pq" @@ -15,29 +15,29 @@ import ( var goquDb *goqu.Database -const DATABASE_NAME = "holidays" +const NAME = "holidays" func ConnectDatabase() { // connect to database var err error + logger := logging.GetLogger() hostname, port, dataSourceName := CreateConnectInfo() db, err := sql.Open("postgres", dataSourceName) if err != nil { - log.Fatalln("can not open database.", err) + logger.Fatalln("can not open database.", err) } - log.Println("Connecting to database...", hostname, port) + logger.Info("Connecting to database...", hostname, port) err = db.Ping() if err == nil { - log.Println("Connected to database.") + logger.Info("Connected to database.") } else { - log.Fatalln("can not ping.", err) + logger.Panicln("can not ping.", err) } goquDb = goqu.New("postgres", db) - goquDb.Logger(initLogger()) } func CreateConnectInfo() (string, string, string) { @@ -68,7 +68,7 @@ func getConnectionInfo() (hostname, port, username, password, databaseName strin databaseName, ok = os.LookupEnv("DATABASE") if !ok { - databaseName = DATABASE_NAME + databaseName = NAME } return } @@ -76,7 +76,3 @@ func getConnectionInfo() (hostname, port, username, password, databaseName strin func GetDbConnection() *goqu.Database { return goquDb } - -func initLogger() *log.Logger { - return log.New(os.Stdout, "[SQL] ", log.LstdFlags|log.Lshortfile) -} diff --git a/src/go.mod b/src/go.mod index d1083eb..cb1932e 100644 --- a/src/go.mod +++ b/src/go.mod @@ -4,38 +4,56 @@ go 1.22.0 require ( github.com/doug-martin/goqu/v9 v9.19.0 + github.com/gin-gonic/gin v1.10.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 + github.com/stretchr/testify v1.9.0 + go.uber.org/zap v1.27.0 + golang.org/x/text v0.15.0 ) require ( - github.com/beorn7/perks v1.0.1 // indirect - github.com/cespare/xxhash/v2 v2.2.0 // indirect - github.com/golang/protobuf v1.5.2 // indirect + github.com/bytedance/sonic v1.11.6 // indirect + github.com/bytedance/sonic/loader v0.1.1 // indirect + github.com/cloudwego/base64x v0.1.4 // indirect + github.com/cloudwego/iasm v0.2.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/doug-martin/goqu v5.0.0+incompatible // indirect + github.com/gabriel-vasile/mimetype v1.4.3 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.20.0 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/google/go-cmp v0.5.9 // indirect github.com/json-iterator/go v1.1.12 // indirect - github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect + github.com/klauspost/cpuid/v2 v2.2.7 // indirect + github.com/kr/pretty v0.3.1 // indirect + github.com/labstack/gommon v0.4.2 // indirect + github.com/leodido/go-urn v1.4.0 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect - github.com/prometheus/client_golang v1.14.0 // indirect - github.com/prometheus/client_model v0.3.0 // indirect - github.com/prometheus/common v0.40.0 // indirect - github.com/prometheus/procfs v0.9.0 // indirect - google.golang.org/protobuf v1.33.0 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/rogpeppe/go-internal v1.13.1 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + go.uber.org/multierr v1.10.0 // indirect + golang.org/x/arch v0.8.0 // indirect + google.golang.org/protobuf v1.34.1 // indirect + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) require ( github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/google/uuid v1.6.0 - github.com/labstack/echo-contrib v0.15.0 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/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect - golang.org/x/crypto v0.21.0 // indirect - golang.org/x/net v0.23.0 // indirect - golang.org/x/sys v0.18.0 // indirect + golang.org/x/crypto v0.23.0 // indirect + golang.org/x/net v0.25.0 // indirect + golang.org/x/sys v0.21.0 // indirect golang.org/x/time v0.5.0 // indirect ) diff --git a/src/go.sum b/src/go.sum index 7c1955e..d0eca2e 100644 --- a/src/go.sum +++ b/src/go.sum @@ -1,25 +1,42 @@ github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60= github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= -github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= -github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= -github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= +github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= +github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= +github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= +github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= +github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= +github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/denisenkom/go-mssqldb v0.10.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= +github.com/doug-martin/goqu v5.0.0+incompatible h1:C7O6xQYoWpSGX32C1faMJWe1s82Ktr2jjWf2joReiSQ= +github.com/doug-martin/goqu v5.0.0+incompatible/go.mod h1:4xBntUHXkdIh+CnYd+I2kdHgUTq1kJ+p7OBCngg7RrY= github.com/doug-martin/goqu/v9 v9.19.0 h1:PD7t1X3tRcUiSdc5TEyOFKujZA5gs3VSA7wxSvBx7qo= github.com/doug-martin/goqu/v9 v9.19.0/go.mod h1:nf0Wc2/hV3gYK9LiyqIrzBEVGlI8qW3GuDCEobC4wBQ= +github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= +github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= +github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8= +github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= -github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= @@ -27,12 +44,23 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= -github.com/labstack/echo-contrib v0.15.0 h1:9K+oRU265y4Mu9zpRDv3X+DGTqUALY6oRHCSZZKCRVU= -github.com/labstack/echo-contrib v0.15.0/go.mod h1:lei+qt5CLB4oa7VHTE0yEfQSEB9XTJI1LUqko9UWvo4= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= +github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= 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/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= 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= @@ -41,60 +69,76 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/ 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/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= -github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= +github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus/client_golang v1.14.0 h1:nJdhIvne2eSX/XRAFV9PcvFFRbrjbcTUj0VP62TMhnw= -github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y= -github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4= -github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w= -github.com/prometheus/common v0.40.0 h1:Afz7EVRqGg2Mqqf4JuF9vdvp1pi220m55Pi9T2JnO4Q= -github.com/prometheus/common v0.40.0/go.mod h1:L65ZJPSmfn/UBWLQIHV7dBrKFidB/wPlF1y5TlSt9OE= -github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI= -github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= +github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 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.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= +github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= 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= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= +go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= +go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc= +golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= 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.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= -golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= -golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= -golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= 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.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= -golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= +golang.org/x/sys v0.21.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= +golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= -google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= +google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= +rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/src/key_management/controller/key_test.go b/src/key_management/controller/key_test.go index a6a5252..5ecd66a 100644 --- a/src/key_management/controller/key_test.go +++ b/src/key_management/controller/key_test.go @@ -252,7 +252,7 @@ func tearDown() { } func TestNewKeyManagement(t *testing.T) { - name := "test controller" + name := "test handler" type args struct { controllerName string } diff --git a/src/key_management/main.go b/src/key_management/main.go index 401e46e..98f8460 100644 --- a/src/key_management/main.go +++ b/src/key_management/main.go @@ -1,26 +1,24 @@ package main import ( + "github.com/kynmh69/go-ja-holidays/logging" "os" "github.com/kynmh69/go-ja-holidays/database" "github.com/kynmh69/go-ja-holidays/key_management/router" "github.com/kynmh69/go-ja-holidays/key_management/template" - "github.com/kynmh69/go-ja-holidays/middleware" "github.com/kynmh69/go-ja-holidays/util" "github.com/labstack/echo/v4" mid "github.com/labstack/echo/v4/middleware" ) func init() { - util.LoggerInitialize() + logging.LoggerInitialize() database.ConnectDatabase() } func main() { e := echo.New() - util.EchoLoggerInitialize(e) - middleware.SetMiddleware(e) e.Use(mid.Static("./static")) logger := e.Logger diff --git a/src/logging/logger.go b/src/logging/logger.go new file mode 100644 index 0000000..641c10f --- /dev/null +++ b/src/logging/logger.go @@ -0,0 +1,27 @@ +package logging + +import ( + "github.com/gin-gonic/gin" + "go.uber.org/zap" +) + +var logger *zap.SugaredLogger + +// LoggerInitialize is a function to initialize logger +func LoggerInitialize() { + ginMode := gin.Mode() + var i *zap.Logger + switch ginMode { + case gin.DebugMode: + i, _ = zap.NewDevelopment() + case gin.TestMode, gin.ReleaseMode: + i, _ = zap.NewProduction() + default: + i, _ = zap.NewDevelopment() + } + logger = i.Sugar() +} + +func GetLogger() *zap.SugaredLogger { + return logger +} diff --git a/src/logging/logger_test.go b/src/logging/logger_test.go new file mode 100644 index 0000000..56e2074 --- /dev/null +++ b/src/logging/logger_test.go @@ -0,0 +1,43 @@ +package logging + +import ( + "go.uber.org/zap" + "reflect" + "testing" +) + +func TestGetLogger(t *testing.T) { + tests := []struct { + name string + want *zap.SugaredLogger + }{ + // TODO: Add test cases. + { + name: "TestGetLogger", + want: nil, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := GetLogger(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("GetLogger() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestLoggerInitialize(t *testing.T) { + tests := []struct { + name string + }{ + // TODO: Add test cases. + { + name: "testLoggerInitialize", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + LoggerInitialize() + }) + } +} diff --git a/src/middleware/common.go b/src/middleware/common.go deleted file mode 100644 index 457e29d..0000000 --- a/src/middleware/common.go +++ /dev/null @@ -1,16 +0,0 @@ -package middleware - -import ( - "github.com/labstack/echo/v4" - mid "github.com/labstack/echo/v4/middleware" -) - -func SetMiddleware(e *echo.Echo) { - e.Use(setLogger()) - e.Use(mid.CORS()) - // e.Use(mid.CSRF()) - e.Use(SetPrometheus()) - e.Use(mid.Recover()) - e.Use(mid.RequestID()) - // e.Use(Redirect()) -} diff --git a/src/middleware/common_test.go b/src/middleware/common_test.go deleted file mode 100644 index 810d353..0000000 --- a/src/middleware/common_test.go +++ /dev/null @@ -1,44 +0,0 @@ -package middleware - -import ( - "os" - "testing" - - "github.com/labstack/echo/v4" -) - -func TestMain(m *testing.M) { - setUp() - defer tearDown() - res := m.Run() - os.Exit(res) -} - -func TestSetMiddleware(t *testing.T) { - e := echo.New() - type args struct { - e *echo.Echo - } - tests := []struct { - name string - args args - }{ - { - name: "test OK", - args: args{e: e}, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - SetMiddleware(tt.args.e) - }) - } -} - -func setUp() { - -} - -func tearDown() { - -} diff --git a/src/middleware/logger.go b/src/middleware/logger.go deleted file mode 100644 index 92be91c..0000000 --- a/src/middleware/logger.go +++ /dev/null @@ -1,25 +0,0 @@ -package middleware - -import ( - "os" - - "github.com/kynmh69/go-ja-holidays/util" - "github.com/labstack/echo/v4" - mid "github.com/labstack/echo/v4/middleware" -) - -func getLogFormat() string { - format, ok := os.LookupEnv("LOG_FORMAT") - if !ok { - format = "${time_rfc3339_nano} [${method}] ${uri} ${status} ${id}\n" - } - return format -} - -func confLogFormat() mid.LoggerConfig { - return mid.LoggerConfig{Format: getLogFormat(), Output: util.InitWriter()} -} - -func setLogger() echo.MiddlewareFunc { - return mid.LoggerWithConfig(confLogFormat()) -} diff --git a/src/middleware/logger_test.go b/src/middleware/logger_test.go deleted file mode 100644 index 4dabb22..0000000 --- a/src/middleware/logger_test.go +++ /dev/null @@ -1,68 +0,0 @@ -package middleware - -import ( - "reflect" - "testing" - - "github.com/kynmh69/go-ja-holidays/util" - "github.com/labstack/echo/v4" - mid "github.com/labstack/echo/v4/middleware" -) - -func Test_getLogFormat(t *testing.T) { - format := "${time_rfc3339_nano} [${method}] ${uri} ${status} ${id}\n" - tests := []struct { - name string - want string - }{ - { - name: "test ok", - want: format, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := getLogFormat(); got != tt.want { - t.Errorf("getLogFormat() = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_confLogFormat(t *testing.T) { - tests := []struct { - name string - want mid.LoggerConfig - }{ - { - name: "test OK", - want: mid.LoggerConfig{Format: getLogFormat(), Output: util.InitWriter()}, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := confLogFormat(); !reflect.DeepEqual(got.Format, tt.want.Format) { - t.Errorf("confLogFormat() = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_setLogger(t *testing.T) { - tests := []struct { - name string - want echo.MiddlewareFunc - }{ - { - "test OK", - mid.LoggerWithConfig(confLogFormat()), - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := setLogger(); !reflect.DeepEqual(got, tt.want) { - t.Logf("setLogger() = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/src/middleware/prometheus.go b/src/middleware/prometheus.go deleted file mode 100644 index 09329ab..0000000 --- a/src/middleware/prometheus.go +++ /dev/null @@ -1,10 +0,0 @@ -package middleware - -import ( - "github.com/labstack/echo-contrib/echoprometheus" - "github.com/labstack/echo/v4" -) - -func SetPrometheus() echo.MiddlewareFunc{ - return echoprometheus.NewMiddleware("app") -} diff --git a/src/middleware/prometheus_test.go b/src/middleware/prometheus_test.go deleted file mode 100644 index d7c27a8..0000000 --- a/src/middleware/prometheus_test.go +++ /dev/null @@ -1,24 +0,0 @@ -package middleware - -import ( - "testing" - - "github.com/labstack/echo/v4" -) - -func TestSetPrometheus(t *testing.T) { - tests := []struct { - name string - want echo.MiddlewareFunc - }{ - { - name: "test ok", - want: nil, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - - }) - } -} diff --git a/src/middleware/redirect.go b/src/middleware/redirect.go deleted file mode 100644 index c31f360..0000000 --- a/src/middleware/redirect.go +++ /dev/null @@ -1,35 +0,0 @@ -package middleware - -import ( - "net/http" - - "github.com/labstack/echo/v4" -) - -func Redirect() echo.MiddlewareFunc { - registeredPaths := []string{ - "/manage/key", - "/css", - } - return func(next echo.HandlerFunc) echo.HandlerFunc { - return func(c echo.Context) error { - path := c.Request().URL.Path - c.Logger().Debug(c.Path()) - // 登録されているパス以外の場合はトップページにリダイレクト - if !contains(registeredPaths, path) { - return c.Redirect(http.StatusMovedPermanently, "/manage/key") - } - - return next(c) - } - } -} - -func contains(slice []string, item string) bool { - for _, s := range slice { - if s == item { - return true - } - } - return false -} diff --git a/src/updater/main.go b/src/updater/main.go index b7d0303..0bca398 100644 --- a/src/updater/main.go +++ b/src/updater/main.go @@ -2,12 +2,13 @@ package main import ( "github.com/kynmh69/go-ja-holidays/database" + "github.com/kynmh69/go-ja-holidays/logging" "github.com/kynmh69/go-ja-holidays/updater/controller" "github.com/kynmh69/go-ja-holidays/util" ) func init() { - util.LoggerInitialize() + logging.LoggerInitialize() database.ConnectDatabase() } diff --git a/src/util/logger_initialize.go b/src/util/logger_initialize.go deleted file mode 100644 index 804cc9d..0000000 --- a/src/util/logger_initialize.go +++ /dev/null @@ -1,72 +0,0 @@ -package util - -import ( - "fmt" - "io" - "log" - "os" - "path/filepath" - "strings" - "time" - - "github.com/labstack/echo/v4" - echoLog "github.com/labstack/gommon/log" -) - -func LoggerInitialize() { - log.SetFlags(log.LstdFlags | log.Lshortfile) - log.SetPrefix("[App] ") -} - -func GetLoggerLevel() echoLog.Lvl { - level, ok := os.LookupEnv("LOG_LEVEL") - if !ok { - level = "info" - } - level = strings.ToLower(level) - levelNo := echoLog.INFO - switch level { - case "debug": - levelNo = echoLog.DEBUG - case "info": - levelNo = echoLog.INFO - case "warn": - levelNo = echoLog.WARN - case "warning": - levelNo = echoLog.WARN - case "error": - levelNo = echoLog.ERROR - default: - levelNo = echoLog.OFF - } - return levelNo -} - -func EchoLoggerInitialize(e *echo.Echo) { - logger := e.Logger - logger.SetPrefix("[APP]") - logger.SetLevel(GetLoggerLevel()) - logger.SetOutput(InitWriter()) - logger.SetHeader("${prefix} ${time_rfc3339_nano} [${level}] ${path} Line.${line} ") -} - -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 { - nowStr := time.Now().Format("2006-01-02") - 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 { - log.Fatalln("Not open log file", err) - } - return file -} diff --git a/src/util/unit.go b/src/util/unit.go new file mode 100644 index 0000000..f3cd289 --- /dev/null +++ b/src/util/unit.go @@ -0,0 +1,11 @@ +package util + +import ( + "github.com/kynmh69/go-ja-holidays/database" + "github.com/kynmh69/go-ja-holidays/logging" +) + +func SetUp() { + logging.LoggerInitialize() + database.ConnectDatabase() +} From 67de117afb9b104126db0f753a343de5682c4463 Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sat, 28 Sep 2024 23:40:25 +0900 Subject: [PATCH 02/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/handler/is_holiday.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/handler/is_holiday.go b/src/api/handler/is_holiday.go index 382c7e0..eed3d93 100644 --- a/src/api/handler/is_holiday.go +++ b/src/api/handler/is_holiday.go @@ -13,7 +13,7 @@ import ( ) type HolidayRequest struct { - holiday *time.Time `query:"day" binding:"date"` + holiday *time.Time `uri:"day" binding:"datetime"` } func IsHoliday(c *gin.Context) { @@ -25,7 +25,7 @@ func IsHoliday(c *gin.Context) { logger := logging.GetLogger() - if err := c.BindQuery(&request); err != nil { + if err := c.ShouldBindUri(&request); err != nil { logger.Error(err) BadRequestJson(c, err.Error()) return From 0a28c4ce400e0213f90f6b0950b3d0e958b29ea3 Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sat, 28 Sep 2024 23:52:37 +0900 Subject: [PATCH 03/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/.air.toml | 2 +- src/api/handler/is_holiday.go | 4 ++-- src/go.mod | 4 ---- src/go.sum | 2 -- 4 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/api/.air.toml b/src/api/.air.toml index fce1e22..49468ed 100644 --- a/src/api/.air.toml +++ b/src/api/.air.toml @@ -22,7 +22,7 @@ tmp_dir = "tmp" poll_interval = 0 post_cmd = [] pre_cmd = [] - rerun = true + rerun = false rerun_delay = 500 send_interrupt = false stop_on_error = false diff --git a/src/api/handler/is_holiday.go b/src/api/handler/is_holiday.go index eed3d93..5566e14 100644 --- a/src/api/handler/is_holiday.go +++ b/src/api/handler/is_holiday.go @@ -13,7 +13,7 @@ import ( ) type HolidayRequest struct { - holiday *time.Time `uri:"day" binding:"datetime"` + holiday time.Time `uri:"day" time_format:"2006-01-02" time_utc:"false"` } func IsHoliday(c *gin.Context) { @@ -51,7 +51,7 @@ func IsHoliday(c *gin.Context) { if ok { isHoliday = model.IsHoliday{IsHoliday: ok, HolidayData: holiday} } else { - isHoliday = model.IsHoliday{IsHoliday: ok, HolidayData: model.HolidayData{Date: *request.holiday}} + isHoliday = model.IsHoliday{IsHoliday: ok, HolidayData: model.HolidayData{Date: request.holiday}} } logger.Debug(isHoliday) c.JSON(http.StatusOK, isHoliday) diff --git a/src/go.mod b/src/go.mod index cb1932e..db760b5 100644 --- a/src/go.mod +++ b/src/go.mod @@ -7,7 +7,6 @@ require ( github.com/gin-gonic/gin v1.10.0 github.com/labstack/echo/v4 v4.11.4 github.com/lib/pq v1.10.1 - github.com/stretchr/testify v1.9.0 go.uber.org/zap v1.27.0 golang.org/x/text v0.15.0 ) @@ -17,8 +16,6 @@ require ( github.com/bytedance/sonic/loader v0.1.1 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/doug-martin/goqu v5.0.0+incompatible // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect @@ -34,7 +31,6 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.2.2 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rogpeppe/go-internal v1.13.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect diff --git a/src/go.sum b/src/go.sum index d0eca2e..52084b1 100644 --- a/src/go.sum +++ b/src/go.sum @@ -13,8 +13,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/denisenkom/go-mssqldb v0.10.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= -github.com/doug-martin/goqu v5.0.0+incompatible h1:C7O6xQYoWpSGX32C1faMJWe1s82Ktr2jjWf2joReiSQ= -github.com/doug-martin/goqu v5.0.0+incompatible/go.mod h1:4xBntUHXkdIh+CnYd+I2kdHgUTq1kJ+p7OBCngg7RrY= github.com/doug-martin/goqu/v9 v9.19.0 h1:PD7t1X3tRcUiSdc5TEyOFKujZA5gs3VSA7wxSvBx7qo= github.com/doug-martin/goqu/v9 v9.19.0/go.mod h1:nf0Wc2/hV3gYK9LiyqIrzBEVGlI8qW3GuDCEobC4wBQ= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= From 7b69cffa70baa45d0a48645fc8fc036bb1d977ab Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sat, 28 Sep 2024 23:59:55 +0900 Subject: [PATCH 04/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/handler/is_holiday.go | 10 +++++----- src/api/router/router.go | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/api/handler/is_holiday.go b/src/api/handler/is_holiday.go index 5566e14..32b7c90 100644 --- a/src/api/handler/is_holiday.go +++ b/src/api/handler/is_holiday.go @@ -13,7 +13,7 @@ import ( ) type HolidayRequest struct { - holiday time.Time `uri:"day" time_format:"2006-01-02" time_utc:"false"` + Date time.Time `uri:"date" time_format:"2006-01-02" time_utc:"false"` } func IsHoliday(c *gin.Context) { @@ -30,15 +30,15 @@ func IsHoliday(c *gin.Context) { BadRequestJson(c, err.Error()) return } - logger.Debug(request.holiday) + logger.Debug(request.Date) // Set the time zone to JST. - loc := request.holiday.Location() + loc := request.Date.Location() goqu.SetTimeLocation(loc) // Get the holiday data for the specified day. dataSet := db.From(TableHolidaysJp). - Where(goqu.C(ColumnDate).Eq(request.holiday)) + Where(goqu.C(ColumnDate).Eq(request.Date)) ok, err := dataSet.ScanStruct(&holiday) if err != nil { @@ -51,7 +51,7 @@ func IsHoliday(c *gin.Context) { if ok { isHoliday = model.IsHoliday{IsHoliday: ok, HolidayData: holiday} } else { - isHoliday = model.IsHoliday{IsHoliday: ok, HolidayData: model.HolidayData{Date: request.holiday}} + isHoliday = model.IsHoliday{IsHoliday: ok, HolidayData: model.HolidayData{Date: request.Date}} } logger.Debug(isHoliday) c.JSON(http.StatusOK, isHoliday) diff --git a/src/api/router/router.go b/src/api/router/router.go index b61f403..5947d5b 100644 --- a/src/api/router/router.go +++ b/src/api/router/router.go @@ -7,6 +7,6 @@ import ( func MakeRoute(r *gin.Engine) { r.GET("/holidays", handler.GetHolidays) - r.GET("/holidays/:day", handler.IsHoliday) + r.GET("/holidays/:date", handler.IsHoliday) r.GET("/holidays/count", handler.CountHolidays) } From 06bf63047025492136cf6536161b1883251ba167 Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 00:04:15 +0900 Subject: [PATCH 05/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compose.yaml | 2 +- src/key_management/controller/key_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/compose.yaml b/compose.yaml index e7a1752..1916092 100644 --- a/compose.yaml +++ b/compose.yaml @@ -55,7 +55,7 @@ services: volumes: - ./src:/go/app:rw ports: - - 8081:80 + - "8081:8080" volumes: diff --git a/src/key_management/controller/key_test.go b/src/key_management/controller/key_test.go index 5ecd66a..552ee86 100644 --- a/src/key_management/controller/key_test.go +++ b/src/key_management/controller/key_test.go @@ -27,7 +27,7 @@ const VIEW_DIR = "../view/*.html" func TestKeyManagement_Retrieve(t *testing.T) { e := echo.New() - util.EchoLoggerInitialize(e) + util.SetUp() req := httptest.NewRequest(http.MethodGet, "/manage/key", nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() From 79c586ef3bc4f8fa9d47027ecf2ce77ab443720d Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 00:33:21 +0900 Subject: [PATCH 06/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/go.mod | 3 +-- src/go.sum | 6 ++--- src/key_management/controller/base.go | 12 +++++---- src/key_management/controller/key.go | 34 +++++++++++++++---------- src/key_management/main.go | 17 +++++-------- src/key_management/router/router.go | 12 ++++----- src/key_management/template/template.go | 28 ++++++++++---------- src/model/api_key.go | 11 ++++---- 8 files changed, 64 insertions(+), 59 deletions(-) diff --git a/src/go.mod b/src/go.mod index db760b5..3f27a68 100644 --- a/src/go.mod +++ b/src/go.mod @@ -4,6 +4,7 @@ go 1.22.0 require ( github.com/doug-martin/goqu/v9 v9.19.0 + github.com/gin-contrib/multitemplate v1.0.1 github.com/gin-gonic/gin v1.10.0 github.com/labstack/echo/v4 v4.11.4 github.com/lib/pq v1.10.1 @@ -42,7 +43,6 @@ require ( ) require ( - github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/google/uuid v1.6.0 github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect @@ -51,5 +51,4 @@ require ( golang.org/x/crypto v0.23.0 // indirect golang.org/x/net v0.25.0 // indirect golang.org/x/sys v0.21.0 // indirect - golang.org/x/time v0.5.0 // indirect ) diff --git a/src/go.sum b/src/go.sum index 52084b1..66a6242 100644 --- a/src/go.sum +++ b/src/go.sum @@ -17,6 +17,8 @@ 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/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +github.com/gin-contrib/multitemplate v1.0.1 h1:Asi8boB7NctSoQzbWDosLObon0cYMP5OM+ihQMjlW5M= +github.com/gin-contrib/multitemplate v1.0.1/go.mod h1:uU+PnuKoiEHWqB9Zvco+Kqv9KNrsHi6IZOUUgTctMPA= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= @@ -32,8 +34,6 @@ github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaC github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= -github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= -github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= @@ -128,8 +128,6 @@ golang.org/x/sys v0.21.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.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= -golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/src/key_management/controller/base.go b/src/key_management/controller/base.go index 254c7c9..432b9a8 100644 --- a/src/key_management/controller/base.go +++ b/src/key_management/controller/base.go @@ -1,11 +1,13 @@ package controller -import "github.com/labstack/echo/v4" +import ( + "github.com/gin-gonic/gin" +) type Controller interface { - Retrieve(c echo.Context) error - Create(c echo.Context) error - Update(c echo.Context) error - Delete(c echo.Context) error + Retrieve(r *gin.Context) error + Create(r *gin.Context) error + Update(r *gin.Context) error + Delete(r *gin.Context) error GetControllerName() string } diff --git a/src/key_management/controller/key.go b/src/key_management/controller/key.go index 1dce1f0..98a570e 100644 --- a/src/key_management/controller/key.go +++ b/src/key_management/controller/key.go @@ -1,11 +1,11 @@ package controller import ( - "errors" + "github.com/gin-gonic/gin" + "github.com/kynmh69/go-ja-holidays/logging" "net/http" "github.com/kynmh69/go-ja-holidays/model" - "github.com/labstack/echo/v4" ) const ( @@ -17,25 +17,33 @@ type KeyManagement struct { ControllerName string } -func (k KeyManagement) Retrieve(c echo.Context) error { - logger := c.Logger() +func (k KeyManagement) Retrieve(c *gin.Context) { + logger := logging.GetLogger() apiKeys, _ := model.GetApiKeys() logger.Debug("APIKEYS", apiKeys) - return c.Render(http.StatusOK, TOP_PAGE_NAME, apiKeys) + c.HTML(http.StatusOK, TOP_PAGE_NAME, apiKeys) } -func (k KeyManagement) Create(c echo.Context) error { - model.CreateApiKey(c) - return c.Redirect(http.StatusFound, TOP_PATH) +func (k KeyManagement) Create(c *gin.Context) { + err := model.CreateApiKey(c) + if err != nil { + c.HTML(500, "error", gin.H{"error": err.Error()}) + return + } + c.Redirect(http.StatusFound, TOP_PATH) } -func (k KeyManagement) Update(c echo.Context) error { - return errors.New("not implemented") +func (k KeyManagement) Update(c *gin.Context) { + c.HTML(500, "update", nil) } -func (k KeyManagement) Delete(c echo.Context) error { - model.DeleteApiKey(c) - return c.Redirect(http.StatusFound, TOP_PATH) +func (k KeyManagement) Delete(c *gin.Context) { + err := model.DeleteApiKey(c) + if err != nil { + c.HTML(500, "error", gin.H{"error": err.Error()}) + return + } + c.Redirect(http.StatusFound, TOP_PATH) } func (k KeyManagement) GetControllerName() string { diff --git a/src/key_management/main.go b/src/key_management/main.go index 98f8460..d04ba6e 100644 --- a/src/key_management/main.go +++ b/src/key_management/main.go @@ -1,15 +1,13 @@ package main import ( + "github.com/gin-gonic/gin" "github.com/kynmh69/go-ja-holidays/logging" "os" "github.com/kynmh69/go-ja-holidays/database" "github.com/kynmh69/go-ja-holidays/key_management/router" "github.com/kynmh69/go-ja-holidays/key_management/template" - "github.com/kynmh69/go-ja-holidays/util" - "github.com/labstack/echo/v4" - mid "github.com/labstack/echo/v4/middleware" ) func init() { @@ -18,14 +16,13 @@ func init() { } func main() { - e := echo.New() - e.Use(mid.Static("./static")) - logger := e.Logger + r := gin.Default() + r.Static("/static", "./static") + logger := logging.GetLogger() - t := template.NewTemplate("view/*.html") - e.Renderer = t - e.HTTPErrorHandler = util.CustomHTTPErrorHandler - router.MakeRoute(e) + r.HTMLRender = template.Render("view/*.html") + //e.HTTPErrorHandler = util.CustomHTTPErrorHandler + router.MakeRoute(r) wd, _ := os.Getwd() logger.Debug(wd) logger.Fatal(e.Start(":80")) diff --git a/src/key_management/router/router.go b/src/key_management/router/router.go index dcf6791..d44dd1c 100644 --- a/src/key_management/router/router.go +++ b/src/key_management/router/router.go @@ -2,22 +2,22 @@ package router import ( "fmt" + "github.com/gin-gonic/gin" "log" "github.com/kynmh69/go-ja-holidays/key_management/controller" - "github.com/labstack/echo/v4" ) -func MakeRoute(e *echo.Echo) { +func MakeRoute(r *gin.Engine) { controllers := []controller.Controller{ controller.KeyManagement{ControllerName: "key"}, } for _, v := range controllers { path := fmt.Sprintf("/manage/%s", v.GetControllerName()) log.Println(path) - e.POST(fmt.Sprintf("%s/create", path), v.Create) - e.GET(path, v.Retrieve) - e.PUT(path, v.Update) - e.POST(fmt.Sprintf("%s/delete", path), v.Delete) + r.POST(fmt.Sprintf("%s/create", path), v.Create) + r.GET(path, v.Retrieve) + r.PUT(path, v.Update) + r.POST(fmt.Sprintf("%s/delete", path), v.Delete) } } diff --git a/src/key_management/template/template.go b/src/key_management/template/template.go index af2e1e5..c708cad 100644 --- a/src/key_management/template/template.go +++ b/src/key_management/template/template.go @@ -1,22 +1,22 @@ package template import ( - "html/template" - "io" - - "github.com/labstack/echo/v4" + "github.com/gin-contrib/multitemplate" + "github.com/kynmh69/go-ja-holidays/logging" + "path/filepath" ) -type Template struct { - templates *template.Template -} +func Render(templateGlob string) multitemplate.Renderer { + logger := logging.GetLogger() + logger.Debug("Glob:", templateGlob) + r := multitemplate.NewRenderer() -func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error { - logger := c.Logger() - logger.Debug(w, name, data) - return t.templates.ExecuteTemplate(w, name, data) -} + layouts, err := filepath.Glob(templateGlob) + if err != nil { + logger.Panicln(err.Error()) + } -func NewTemplate(path string) *Template { - return &Template{templates: template.Must(template.ParseGlob(path))} + // Generate our templates map from our layouts/ and includes/ directories + r.AddFromFiles("view", layouts...) + return r } diff --git a/src/model/api_key.go b/src/model/api_key.go index e247aeb..429ea29 100644 --- a/src/model/api_key.go +++ b/src/model/api_key.go @@ -1,12 +1,13 @@ package model import ( + "github.com/gin-gonic/gin" + "github.com/kynmh69/go-ja-holidays/logging" "time" "github.com/doug-martin/goqu/v9" "github.com/google/uuid" "github.com/kynmh69/go-ja-holidays/database" - "github.com/labstack/echo/v4" ) const TABLE_API_KEY = "api_key" @@ -26,8 +27,8 @@ func GetApiKeys() ([]ApiKey, error) { return apiKeys, err } -func CreateApiKey(c echo.Context) error { - logger := c.Logger() +func CreateApiKey(c *gin.Context) error { + logger := logging.GetLogger() key := uuid.New() db := database.GetDbConnection() result, err := db.Insert(TABLE_API_KEY). @@ -44,8 +45,8 @@ func CreateApiKey(c echo.Context) error { return err } -func DeleteApiKey(c echo.Context) error { - logger := c.Logger() +func DeleteApiKey(c *gin.Context) error { + logger := logging.GetLogger() db := database.GetDbConnection() defaultLocation := time.Local logger.Debug(defaultLocation) From dc48af1a9739d53c1367e96d6431e77b95df8e8e Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 00:34:38 +0900 Subject: [PATCH 07/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/key_management/controller/base.go | 8 ++++---- src/key_management/router/router.go | 5 ++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/key_management/controller/base.go b/src/key_management/controller/base.go index 432b9a8..2ffdfe6 100644 --- a/src/key_management/controller/base.go +++ b/src/key_management/controller/base.go @@ -5,9 +5,9 @@ import ( ) type Controller interface { - Retrieve(r *gin.Context) error - Create(r *gin.Context) error - Update(r *gin.Context) error - Delete(r *gin.Context) error + Retrieve(r *gin.Context) + Create(r *gin.Context) + Update(r *gin.Context) + Delete(r *gin.Context) GetControllerName() string } diff --git a/src/key_management/router/router.go b/src/key_management/router/router.go index d44dd1c..9569e4f 100644 --- a/src/key_management/router/router.go +++ b/src/key_management/router/router.go @@ -3,9 +3,8 @@ package router import ( "fmt" "github.com/gin-gonic/gin" - "log" - "github.com/kynmh69/go-ja-holidays/key_management/controller" + "github.com/kynmh69/go-ja-holidays/logging" ) func MakeRoute(r *gin.Engine) { @@ -14,7 +13,7 @@ func MakeRoute(r *gin.Engine) { } for _, v := range controllers { path := fmt.Sprintf("/manage/%s", v.GetControllerName()) - log.Println(path) + logging.GetLogger().Debugln(path) r.POST(fmt.Sprintf("%s/create", path), v.Create) r.GET(path, v.Retrieve) r.PUT(path, v.Update) From 73dcba7e4a5eea0e9423469bb38fbed0da096e08 Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 00:35:45 +0900 Subject: [PATCH 08/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/key_management/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/key_management/main.go b/src/key_management/main.go index d04ba6e..23bf669 100644 --- a/src/key_management/main.go +++ b/src/key_management/main.go @@ -25,5 +25,5 @@ func main() { router.MakeRoute(r) wd, _ := os.Getwd() logger.Debug(wd) - logger.Fatal(e.Start(":80")) + logger.Fatal(r.Run(":8080")) } From ce49e9b1f2c6ee71f6a138e85abca1d42f58dab4 Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 00:37:53 +0900 Subject: [PATCH 09/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/key_management/controller/key.go | 14 +++++++------- src/model/api_key.go | 5 ++--- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/key_management/controller/key.go b/src/key_management/controller/key.go index 98a570e..f886fe9 100644 --- a/src/key_management/controller/key.go +++ b/src/key_management/controller/key.go @@ -9,8 +9,8 @@ import ( ) const ( - TOP_PAGE_NAME = "top.html" - TOP_PATH = "/manage/key" + TopPageName = "top.html" + TopPath = "/manage/key" ) type KeyManagement struct { @@ -21,16 +21,16 @@ func (k KeyManagement) Retrieve(c *gin.Context) { logger := logging.GetLogger() apiKeys, _ := model.GetApiKeys() logger.Debug("APIKEYS", apiKeys) - c.HTML(http.StatusOK, TOP_PAGE_NAME, apiKeys) + c.HTML(http.StatusOK, TopPageName, apiKeys) } func (k KeyManagement) Create(c *gin.Context) { - err := model.CreateApiKey(c) + err := model.CreateApiKey() if err != nil { c.HTML(500, "error", gin.H{"error": err.Error()}) return } - c.Redirect(http.StatusFound, TOP_PATH) + c.Redirect(http.StatusFound, TopPath) } func (k KeyManagement) Update(c *gin.Context) { @@ -38,12 +38,12 @@ func (k KeyManagement) Update(c *gin.Context) { } func (k KeyManagement) Delete(c *gin.Context) { - err := model.DeleteApiKey(c) + err := model.DeleteApiKey() if err != nil { c.HTML(500, "error", gin.H{"error": err.Error()}) return } - c.Redirect(http.StatusFound, TOP_PATH) + c.Redirect(http.StatusFound, TopPath) } func (k KeyManagement) GetControllerName() string { diff --git a/src/model/api_key.go b/src/model/api_key.go index 429ea29..f546c74 100644 --- a/src/model/api_key.go +++ b/src/model/api_key.go @@ -1,7 +1,6 @@ package model import ( - "github.com/gin-gonic/gin" "github.com/kynmh69/go-ja-holidays/logging" "time" @@ -27,7 +26,7 @@ func GetApiKeys() ([]ApiKey, error) { return apiKeys, err } -func CreateApiKey(c *gin.Context) error { +func CreateApiKey() error { logger := logging.GetLogger() key := uuid.New() db := database.GetDbConnection() @@ -45,7 +44,7 @@ func CreateApiKey(c *gin.Context) error { return err } -func DeleteApiKey(c *gin.Context) error { +func DeleteApiKey() error { logger := logging.GetLogger() db := database.GetDbConnection() defaultLocation := time.Local From 5cd00d22d898a98883c5e775d3e41d97e516c91f Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 00:38:55 +0900 Subject: [PATCH 10/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/key_management/controller/key.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/key_management/controller/key.go b/src/key_management/controller/key.go index f886fe9..6921430 100644 --- a/src/key_management/controller/key.go +++ b/src/key_management/controller/key.go @@ -20,7 +20,7 @@ type KeyManagement struct { func (k KeyManagement) Retrieve(c *gin.Context) { logger := logging.GetLogger() apiKeys, _ := model.GetApiKeys() - logger.Debug("APIKEYS", apiKeys) + logger.Debug("APIKEY", apiKeys) c.HTML(http.StatusOK, TopPageName, apiKeys) } From b09f79ba08911ec11f4d0b5b1c0ec802537ffcfc Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 10:18:17 +0900 Subject: [PATCH 11/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/go.mod | 2 +- src/go.sum | 4 ++-- src/key_management/main.go | 10 ++++------ src/key_management/template/template.go | 22 ---------------------- 4 files changed, 7 insertions(+), 31 deletions(-) delete mode 100644 src/key_management/template/template.go diff --git a/src/go.mod b/src/go.mod index 3f27a68..173fc77 100644 --- a/src/go.mod +++ b/src/go.mod @@ -4,8 +4,8 @@ go 1.22.0 require ( github.com/doug-martin/goqu/v9 v9.19.0 - github.com/gin-contrib/multitemplate v1.0.1 github.com/gin-gonic/gin v1.10.0 + github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15 github.com/labstack/echo/v4 v4.11.4 github.com/lib/pq v1.10.1 go.uber.org/zap v1.27.0 diff --git a/src/go.sum b/src/go.sum index 66a6242..c6da7b6 100644 --- a/src/go.sum +++ b/src/go.sum @@ -17,8 +17,6 @@ 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/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= -github.com/gin-contrib/multitemplate v1.0.1 h1:Asi8boB7NctSoQzbWDosLObon0cYMP5OM+ihQMjlW5M= -github.com/gin-contrib/multitemplate v1.0.1/go.mod h1:uU+PnuKoiEHWqB9Zvco+Kqv9KNrsHi6IZOUUgTctMPA= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= @@ -40,6 +38,8 @@ github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15 h1:cW/amwGEJK5MSKntPXRjX4dxs/nGxGT8gXKIsKFmHGc= +github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15/go.mod h1:Fdm/oWRW+CH8PRbLntksCNtmcCBximKPkVQYvmMl80k= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= diff --git a/src/key_management/main.go b/src/key_management/main.go index 23bf669..5e1080a 100644 --- a/src/key_management/main.go +++ b/src/key_management/main.go @@ -2,12 +2,10 @@ package main import ( "github.com/gin-gonic/gin" - "github.com/kynmh69/go-ja-holidays/logging" - "os" - "github.com/kynmh69/go-ja-holidays/database" "github.com/kynmh69/go-ja-holidays/key_management/router" - "github.com/kynmh69/go-ja-holidays/key_management/template" + "github.com/kynmh69/go-ja-holidays/logging" + "os" ) func init() { @@ -17,10 +15,10 @@ func init() { func main() { r := gin.Default() - r.Static("/static", "./static") + r.LoadHTMLGlob("view/*.html") + r.Static("/css", "./static/css") logger := logging.GetLogger() - r.HTMLRender = template.Render("view/*.html") //e.HTTPErrorHandler = util.CustomHTTPErrorHandler router.MakeRoute(r) wd, _ := os.Getwd() diff --git a/src/key_management/template/template.go b/src/key_management/template/template.go deleted file mode 100644 index c708cad..0000000 --- a/src/key_management/template/template.go +++ /dev/null @@ -1,22 +0,0 @@ -package template - -import ( - "github.com/gin-contrib/multitemplate" - "github.com/kynmh69/go-ja-holidays/logging" - "path/filepath" -) - -func Render(templateGlob string) multitemplate.Renderer { - logger := logging.GetLogger() - logger.Debug("Glob:", templateGlob) - r := multitemplate.NewRenderer() - - layouts, err := filepath.Glob(templateGlob) - if err != nil { - logger.Panicln(err.Error()) - } - - // Generate our templates map from our layouts/ and includes/ directories - r.AddFromFiles("view", layouts...) - return r -} From 0082ede148588547ed07a70ae183bb5b1646bfb8 Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 10:29:39 +0900 Subject: [PATCH 12/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/key_management/main.go | 3 +- src/key_management/view/top.html | 100 ------------------------------- 2 files changed, 2 insertions(+), 101 deletions(-) delete mode 100644 src/key_management/view/top.html diff --git a/src/key_management/main.go b/src/key_management/main.go index 5e1080a..2319464 100644 --- a/src/key_management/main.go +++ b/src/key_management/main.go @@ -15,8 +15,9 @@ func init() { func main() { r := gin.Default() - r.LoadHTMLGlob("view/*.html") + r.LoadHTMLGlob("view/*/*.html") r.Static("/css", "./static/css") + r.Static("/img", "./static/img") logger := logging.GetLogger() //e.HTTPErrorHandler = util.CustomHTTPErrorHandler diff --git a/src/key_management/view/top.html b/src/key_management/view/top.html deleted file mode 100644 index ba5bc85..0000000 --- a/src/key_management/view/top.html +++ /dev/null @@ -1,100 +0,0 @@ - - - - - - - - - API Key Manager - - - - -
-
- -
-
-
- -
-
- -
-
- -
-
-
-
- - -
- - - - - - - - - - {{range .}} - - - - - - {{end}} - -
IDAPI Key作成日
{{.Id}}{{.Key}}{{.CreatedAt.Format "2006年01月02日 15時04分05秒"}} -
-
-
- - - - - - \ No newline at end of file From 36af669ddfee64be57121959b6d7cb456fd35ecf Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 10:51:46 +0900 Subject: [PATCH 13/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/key_management/controller/key_test.go | 164 +++------------------- src/key_management/view/main/top.html | 100 +++++++++++++ src/util/file.go | 35 +++++ 3 files changed, 157 insertions(+), 142 deletions(-) create mode 100644 src/key_management/view/main/top.html create mode 100644 src/util/file.go diff --git a/src/key_management/controller/key_test.go b/src/key_management/controller/key_test.go index 552ee86..d47129f 100644 --- a/src/key_management/controller/key_test.go +++ b/src/key_management/controller/key_test.go @@ -1,6 +1,8 @@ package controller import ( + "github.com/gin-gonic/gin" + "github.com/kynmh69/go-ja-holidays/logging" "log" "net/http" "net/http/httptest" @@ -10,10 +12,8 @@ import ( "github.com/google/uuid" "github.com/kynmh69/go-ja-holidays/database" - "github.com/kynmh69/go-ja-holidays/key_management/template" "github.com/kynmh69/go-ja-holidays/model" "github.com/kynmh69/go-ja-holidays/util" - "github.com/labstack/echo/v4" ) func TestMain(m *testing.M) { @@ -23,23 +23,25 @@ func TestMain(m *testing.M) { os.Exit(res) } -const VIEW_DIR = "../view/*.html" +const ViewDir = "key_management/view/*/*.html" func TestKeyManagement_Retrieve(t *testing.T) { - e := echo.New() + r := gin.Default() util.SetUp() - req := httptest.NewRequest(http.MethodGet, "/manage/key", nil) - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) + mg := NewKeyManagement("key") + target := "/manage/key" + r.GET(target, mg.Retrieve) + req := httptest.NewRequest(http.MethodGet, target, nil) rec := httptest.NewRecorder() - c := e.NewContext(req, rec) - wd, _ := os.Getwd() - log.Println(wd) - e.Renderer = template.NewTemplate(VIEW_DIR) + ctx := gin.CreateTestContextOnly(rec, r) + ctx.Request = req + wd, _ := util.JoinProjectRootPath(ViewDir) + r.LoadHTMLGlob(wd) type fields struct { ControllerName string } type args struct { - c echo.Context + c *gin.Context } tests := []struct { name string @@ -53,7 +55,7 @@ func TestKeyManagement_Retrieve(t *testing.T) { ControllerName: "key", }, args: args{ - c: c, + c: ctx, }, wantErr: false, }, @@ -63,130 +65,7 @@ func TestKeyManagement_Retrieve(t *testing.T) { k := KeyManagement{ ControllerName: tt.fields.ControllerName, } - if err := k.Retrieve(tt.args.c); (err != nil) != tt.wantErr { - t.Errorf("KeyManagement.Retrieve() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestKeyManagement_Create(t *testing.T) { - e := echo.New() - util.EchoLoggerInitialize(e) - req := httptest.NewRequest(http.MethodPost, "/manage/key/create", nil) - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) - rec := httptest.NewRecorder() - c := e.NewContext(req, rec) - e.Renderer = template.NewTemplate(VIEW_DIR) - type fields struct { - ControllerName string - } - type args struct { - c echo.Context - } - tests := []struct { - name string - fields fields - args args - wantErr bool - }{ - { - name: "test ok", - fields: fields{ - ControllerName: "key", - }, - args: args{ - c: c, - }, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - k := KeyManagement{ - ControllerName: tt.fields.ControllerName, - } - if err := k.Create(tt.args.c); (err != nil) != tt.wantErr { - t.Errorf("KeyManagement.Create() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestKeyManagement_Update(t *testing.T) { - e := echo.New() - util.EchoLoggerInitialize(e) - req := httptest.NewRequest(http.MethodPut, "/manage/key", nil) - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) - rec := httptest.NewRecorder() - c := e.NewContext(req, rec) - e.Renderer = template.NewTemplate(VIEW_DIR) - type fields struct { - ControllerName string - } - type args struct { - c echo.Context - } - tests := []struct { - name string - fields fields - args args - wantErr bool - }{ - { - name: "test error", - fields: fields{ControllerName: "key"}, - args: args{c: c}, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - k := KeyManagement{ - ControllerName: tt.fields.ControllerName, - } - if err := k.Update(tt.args.c); (err != nil) != tt.wantErr { - t.Errorf("KeyManagement.Update() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestKeyManagement_Delete(t *testing.T) { - e := echo.New() - util.EchoLoggerInitialize(e) - req := httptest.NewRequest(http.MethodDelete, "/manage/key", nil) - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) - rec := httptest.NewRecorder() - c := e.NewContext(req, rec) - e.Renderer = template.NewTemplate(VIEW_DIR) - type fields struct { - ControllerName string - } - type args struct { - c echo.Context - } - tests := []struct { - name string - fields fields - args args - wantErr bool - }{ - { - name: "test ok", - fields: fields{ControllerName: "key"}, - args: args{c: c}, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - k := KeyManagement{ - ControllerName: tt.fields.ControllerName, - } - if err := k.Delete(tt.args.c); (err != nil) != tt.wantErr { - t.Errorf("KeyManagement.Delete() error = %v, wantErr %v", err, tt.wantErr) - } + k.Retrieve(tt.args.c) }) } } @@ -219,14 +98,15 @@ func TestKeyManagement_GetControllerName(t *testing.T) { } func setUp() { - os.Setenv("PSQL_HOSTNAME", "localhost") - os.Setenv("DATABASE", "unittest") + _ = os.Setenv("PSQL_HOSTNAME", "localhost") + _ = os.Setenv("DATABASE", "unittest") url := "https://www8.cao.go.jp/chosei/shukujitsu/syukujitsu.csv" + logging.LoggerInitialize() database.ConnectDatabase() util.CreateHolidayData(url) db := database.GetDbConnection() - uuid := uuid.New() - apikey := model.ApiKey{Key: uuid.String()} + u := uuid.New() + apikey := model.ApiKey{Key: u.String()} res, err := db.Insert(model.TABLE_API_KEY).Rows( apikey, ). @@ -242,8 +122,8 @@ func setUp() { } func tearDown() { - os.Unsetenv("PSQL_HOSTNAME") - os.Unsetenv("DATABASE") + _ = os.Unsetenv("PSQL_HOSTNAME") + _ = os.Unsetenv("DATABASE") db := database.GetDbConnection() if _, err := db.Delete(model.TABLE_API_KEY).Executor().Exec(); err != nil { log.Fatalln(err) diff --git a/src/key_management/view/main/top.html b/src/key_management/view/main/top.html new file mode 100644 index 0000000..ba5bc85 --- /dev/null +++ b/src/key_management/view/main/top.html @@ -0,0 +1,100 @@ + + + + + + + + + API Key Manager + + + + +
+
+ +
+
+
+ +
+
+ +
+
+ +
+
+
+
+ + +
+ + + + + + + + + + {{range .}} + + + + + + {{end}} + +
IDAPI Key作成日
{{.Id}}{{.Key}}{{.CreatedAt.Format "2006年01月02日 15時04分05秒"}} +
+
+
+ + + + + + \ No newline at end of file diff --git a/src/util/file.go b/src/util/file.go new file mode 100644 index 0000000..bae7332 --- /dev/null +++ b/src/util/file.go @@ -0,0 +1,35 @@ +package util + +import ( + "fmt" + "os" + "path/filepath" +) + +func GetProjectRoot() (string, error) { + currentDir, err := os.Getwd() + if err != nil { + return "", err + } + + for { + if _, err := os.Stat(filepath.Join(currentDir, "go.mod")); err == nil { + return currentDir, nil + } + + parentDir := filepath.Dir(currentDir) + if parentDir == currentDir { + return "", fmt.Errorf("project root not found") + } + currentDir = parentDir + } +} + +// JoinProjectRootPath プロジェクトルートにファイルパスを結合する +func JoinProjectRootPath(paths ...string) (string, error) { + root, err := GetProjectRoot() + if err != nil { + return "", err + } + return filepath.Join(append([]string{root}, paths...)...), nil +} From 52de9a3ac8c18b0d1aea047f212cd721b3ef02c8 Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 11:02:11 +0900 Subject: [PATCH 14/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/key_management/controller/key_test.go | 126 +++++++++++++++++++++- 1 file changed, 122 insertions(+), 4 deletions(-) diff --git a/src/key_management/controller/key_test.go b/src/key_management/controller/key_test.go index d47129f..99c2bbd 100644 --- a/src/key_management/controller/key_test.go +++ b/src/key_management/controller/key_test.go @@ -31,10 +31,8 @@ func TestKeyManagement_Retrieve(t *testing.T) { mg := NewKeyManagement("key") target := "/manage/key" r.GET(target, mg.Retrieve) - req := httptest.NewRequest(http.MethodGet, target, nil) - rec := httptest.NewRecorder() - ctx := gin.CreateTestContextOnly(rec, r) - ctx.Request = req + get := http.MethodGet + ctx := createContext(get, target, r) wd, _ := util.JoinProjectRootPath(ViewDir) r.LoadHTMLGlob(wd) type fields struct { @@ -70,6 +68,14 @@ func TestKeyManagement_Retrieve(t *testing.T) { } } +func createContext(get string, target string, r *gin.Engine) *gin.Context { + req := httptest.NewRequest(get, target, nil) + rec := httptest.NewRecorder() + ctx := gin.CreateTestContextOnly(rec, r) + ctx.Request = req + return ctx +} + func TestKeyManagement_GetControllerName(t *testing.T) { type fields struct { ControllerName string @@ -157,3 +163,115 @@ func TestNewKeyManagement(t *testing.T) { }) } } + +func TestKeyManagement_Create(t *testing.T) { + r := gin.Default() + util.SetUp() + mg := NewKeyManagement("key") + target := "/manage/key" + r.POST(target, mg.Create) + post := http.MethodPost + ctx := createContext(post, target, r) + wd, _ := util.JoinProjectRootPath(ViewDir) + r.LoadHTMLGlob(wd) + type fields struct { + ControllerName string + } + type args struct { + c *gin.Context + } + tests := []struct { + name string + fields fields + args args + }{ + { + name: "test OK", + fields: fields{ControllerName: "key"}, + args: args{c: ctx}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + k := KeyManagement{ + ControllerName: tt.fields.ControllerName, + } + k.Create(tt.args.c) + }) + } +} + +func TestKeyManagement_Update(t *testing.T) { + r := gin.Default() + util.SetUp() + mg := NewKeyManagement("key") + target := "/manage/key" + r.PUT(target, mg.Update) + post := http.MethodPut + ctx := createContext(post, target, r) + wd, _ := util.JoinProjectRootPath(ViewDir) + r.LoadHTMLGlob(wd) + type fields struct { + ControllerName string + } + type args struct { + c *gin.Context + } + tests := []struct { + name string + fields fields + args args + }{ + { + name: "test OK", + fields: fields{ControllerName: "key"}, + args: args{c: ctx}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + k := KeyManagement{ + ControllerName: tt.fields.ControllerName, + } + k.Update(tt.args.c) + }) + } +} + +func TestKeyManagement_Delete(t *testing.T) { + r := gin.Default() + util.SetUp() + mg := NewKeyManagement("key") + target := "/manage/key" + r.POST(target, mg.Create) + post := http.MethodPost + ctx := createContext(post, target, r) + wd, _ := util.JoinProjectRootPath(ViewDir) + r.LoadHTMLGlob(wd) + type fields struct { + ControllerName string + } + type args struct { + c *gin.Context + } + tests := []struct { + name string + fields fields + args args + }{ + // TODO: Add test cases. + { + name: "test OK", + fields: fields{ControllerName: "key"}, + args: args{c: ctx}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + k := KeyManagement{ + ControllerName: tt.fields.ControllerName, + } + k.Delete(tt.args.c) + }) + } +} From 00e55db33256cd133fc1fc2ac59b06540120b866 Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 11:02:39 +0900 Subject: [PATCH 15/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/key_management/controller/key_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/src/key_management/controller/key_test.go b/src/key_management/controller/key_test.go index 99c2bbd..7c0025d 100644 --- a/src/key_management/controller/key_test.go +++ b/src/key_management/controller/key_test.go @@ -259,7 +259,6 @@ func TestKeyManagement_Delete(t *testing.T) { fields fields args args }{ - // TODO: Add test cases. { name: "test OK", fields: fields{ControllerName: "key"}, From 3ef85334e49e4ee356da6575aee90c211c1b03fb Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 11:04:06 +0900 Subject: [PATCH 16/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/key_management/router/router_test.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/key_management/router/router_test.go b/src/key_management/router/router_test.go index 151ffa7..188a693 100644 --- a/src/key_management/router/router_test.go +++ b/src/key_management/router/router_test.go @@ -1,17 +1,14 @@ package router import ( + "github.com/gin-gonic/gin" "testing" - - "github.com/kynmh69/go-ja-holidays/util" - "github.com/labstack/echo/v4" ) func TestMakeRoute(t *testing.T) { - e := echo.New() - util.EchoLoggerInitialize(e) + r := gin.Default() type args struct { - e *echo.Echo + e *gin.Engine } tests := []struct { name string @@ -19,7 +16,7 @@ func TestMakeRoute(t *testing.T) { }{ { name: "test ok", - args: args{e: e}, + args: args{e: r}, }, } for _, tt := range tests { From dd649b56b35dad3ebbc11cf19249d1334bc8ccf0 Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 11:05:06 +0900 Subject: [PATCH 17/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/key_management/router/router_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/key_management/router/router_test.go b/src/key_management/router/router_test.go index 188a693..99e9783 100644 --- a/src/key_management/router/router_test.go +++ b/src/key_management/router/router_test.go @@ -2,10 +2,12 @@ package router import ( "github.com/gin-gonic/gin" + "github.com/kynmh69/go-ja-holidays/logging" "testing" ) func TestMakeRoute(t *testing.T) { + logging.LoggerInitialize() r := gin.Default() type args struct { e *gin.Engine From bd32df1c78d23e7935cf3b6ae467733dbf6b6364 Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 11:06:38 +0900 Subject: [PATCH 18/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 51df864..c5ad10b 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,11 @@ ## 技術スタック | 言語/フレームワーク/ミドルウェア | バージョン | -| -------------------------------- | ---------- | -| Go | 1.22 | -| Echo | v4 | -| goqu | v9 | -| postgreSQL | 16 | +|-------------------|-------| +| Go | 1.22 | +| gin | v1.10 | +| goqu | v9 | +| postgresSQL | 16 | ## 機能 From b1bed6288e242e5f5730495d417cd52bf67ded2b Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 11:07:30 +0900 Subject: [PATCH 19/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/go.mod | 1 - src/go.sum | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/go.mod b/src/go.mod index 173fc77..9963b31 100644 --- a/src/go.mod +++ b/src/go.mod @@ -5,7 +5,6 @@ go 1.22.0 require ( github.com/doug-martin/goqu/v9 v9.19.0 github.com/gin-gonic/gin v1.10.0 - github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15 github.com/labstack/echo/v4 v4.11.4 github.com/lib/pq v1.10.1 go.uber.org/zap v1.27.0 diff --git a/src/go.sum b/src/go.sum index c6da7b6..75d7efe 100644 --- a/src/go.sum +++ b/src/go.sum @@ -38,8 +38,6 @@ github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15 h1:cW/amwGEJK5MSKntPXRjX4dxs/nGxGT8gXKIsKFmHGc= -github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15/go.mod h1:Fdm/oWRW+CH8PRbLntksCNtmcCBximKPkVQYvmMl80k= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= From 40f30ba3226db556270affc62ac2d1d0ae20ee60 Mon Sep 17 00:00:00 2001 From: Hiroki Kayanuma <32707877+kynmh69@users.noreply.github.com> Date: Sun, 29 Sep 2024 11:09:36 +0900 Subject: [PATCH 20/27] Fix code scanning alert no. 2: Clear-text logging of sensitive information Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> --- src/key_management/controller/key.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/key_management/controller/key.go b/src/key_management/controller/key.go index 6921430..e42fa95 100644 --- a/src/key_management/controller/key.go +++ b/src/key_management/controller/key.go @@ -20,7 +20,7 @@ type KeyManagement struct { func (k KeyManagement) Retrieve(c *gin.Context) { logger := logging.GetLogger() apiKeys, _ := model.GetApiKeys() - logger.Debug("APIKEY", apiKeys) + logger.Debug("API keys retrieved successfully.") c.HTML(http.StatusOK, TopPageName, apiKeys) } From 051767ac5332f017454a4786bf290565b1ebe13f Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 11:37:07 +0900 Subject: [PATCH 21/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/updater/controller/save_holidays.go | 7 ++++--- src/updater/controller/save_holidays_test.go | 14 ++++++++------ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/updater/controller/save_holidays.go b/src/updater/controller/save_holidays.go index 4518591..a9b6bd7 100644 --- a/src/updater/controller/save_holidays.go +++ b/src/updater/controller/save_holidays.go @@ -1,6 +1,7 @@ package controller import ( + "github.com/kynmh69/go-ja-holidays/logging" "log" "time" @@ -66,7 +67,7 @@ func getLatestHoliday() (HolidayDbData, bool) { func firstInsertHolidays(holidays []HolidayDbData) { db := database.GetDbConnection() - + logger := logging.GetLogger() // タムゾーンをCSV元のものに変更 timeLocation := holidays[0].Date.Location() goqu.SetTimeLocation(timeLocation) @@ -76,9 +77,9 @@ func firstInsertHolidays(holidays []HolidayDbData) { log.Println(err) } if affected, err := result.RowsAffected(); err == nil { - log.Println("successfull.", affected) + logger.Infoln("successfull.", affected) } else { - log.Fatalln("Rows Affected err.", err) + logger.Panicln("Rows Affected err.", err) } } diff --git a/src/updater/controller/save_holidays_test.go b/src/updater/controller/save_holidays_test.go index b372a41..a382886 100644 --- a/src/updater/controller/save_holidays_test.go +++ b/src/updater/controller/save_holidays_test.go @@ -1,6 +1,7 @@ package controller import ( + "github.com/kynmh69/go-ja-holidays/logging" "log" "os" "reflect" @@ -68,8 +69,8 @@ func Test_getLatestHoliday(t *testing.T) { want1 bool }{ { - name: "ok", - want: wants, + name: "ok", + want: wants, want1: true, }, } @@ -179,8 +180,9 @@ func Test_updateData(t *testing.T) { } func setUp() { - os.Setenv("DATABASE", "unittest") - os.Setenv("PSQL_HOSTNAME", "localhost") + logging.LoggerInitialize() + _ = os.Setenv("DATABASE", "unittest") + _ = os.Setenv("PSQL_HOSTNAME", "localhost") loc, err := time.LoadLocation("Asia/Tokyo") if err != nil { log.Fatalln(err) @@ -200,7 +202,7 @@ func tearDown() { if _, err := db.Delete(TABLE_HOLIDAYS_JP).Executor().Exec(); err != nil { log.Fatalln(err) } - os.Unsetenv("DATABASE") - os.Unsetenv("PSQL_HOSTNAME") + _ = os.Unsetenv("DATABASE") + _ = os.Unsetenv("PSQL_HOSTNAME") log.Println("Tear down.") } From cac26f475ccdbdb30fe9ca3ecf1f3ce357e28ba0 Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 11:46:47 +0900 Subject: [PATCH 22/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/codecov.yml | 6 +++--- .github/workflows/docker-publish-api.yml | 2 +- .github/workflows/docker-publish-key-manager.yml | 2 +- .github/workflows/docker-publish-updater.yml | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 985e2ac..43981f1 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -38,7 +38,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v5 with: - go-version: '1.22' + go-version: '1.23' cache-dependency-path: | src/go.sum @@ -86,7 +86,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v5 with: - go-version: '1.22' + go-version: '1.23' cache-dependency-path: | src/go.sum @@ -135,7 +135,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v5 with: - go-version: '1.22' + go-version: '1.23' cache-dependency-path: | src/go.sum diff --git a/.github/workflows/docker-publish-api.yml b/.github/workflows/docker-publish-api.yml index a15887c..a2c3039 100644 --- a/.github/workflows/docker-publish-api.yml +++ b/.github/workflows/docker-publish-api.yml @@ -123,7 +123,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v5 with: - go-version: '1.22' + go-version: '1.23' cache-dependency-path: | src/go.sum diff --git a/.github/workflows/docker-publish-key-manager.yml b/.github/workflows/docker-publish-key-manager.yml index a716364..d9ebde5 100644 --- a/.github/workflows/docker-publish-key-manager.yml +++ b/.github/workflows/docker-publish-key-manager.yml @@ -123,7 +123,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v5 with: - go-version: '1.22' + go-version: '1.23' cache-dependency-path: | src/go.sum diff --git a/.github/workflows/docker-publish-updater.yml b/.github/workflows/docker-publish-updater.yml index b18d192..519f224 100644 --- a/.github/workflows/docker-publish-updater.yml +++ b/.github/workflows/docker-publish-updater.yml @@ -121,7 +121,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v5 with: - go-version: '1.22' + go-version: '1.23' cache-dependency-path: | src/go.sum From 8b852b18f433cc84a8854c99f3506bf85403c0cf Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 12:07:46 +0900 Subject: [PATCH 23/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/handler/count_holidays_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/api/handler/count_holidays_test.go b/src/api/handler/count_holidays_test.go index 5828a1f..d84a05b 100644 --- a/src/api/handler/count_holidays_test.go +++ b/src/api/handler/count_holidays_test.go @@ -5,10 +5,12 @@ import ( "github.com/kynmh69/go-ja-holidays/logging" "github.com/kynmh69/go-ja-holidays/util" "net/http/httptest" + "os" "testing" ) func TestMain(m *testing.M) { + _ = os.Setenv("PSQL_HOSTNAME", "localhost") util.SetUp() code := m.Run() if code > 0 { From 27228023f7de191cb847f68ac21b5c3ece995480 Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 12:11:19 +0900 Subject: [PATCH 24/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/handler/count_holidays_test.go | 2 ++ src/logging/logger.go | 3 +++ 2 files changed, 5 insertions(+) diff --git a/src/api/handler/count_holidays_test.go b/src/api/handler/count_holidays_test.go index d84a05b..7fef7c5 100644 --- a/src/api/handler/count_holidays_test.go +++ b/src/api/handler/count_holidays_test.go @@ -10,6 +10,8 @@ import ( ) func TestMain(m *testing.M) { + logging.LoggerInitialize() + _ = os.Setenv("DATABASE", "unittest") _ = os.Setenv("PSQL_HOSTNAME", "localhost") util.SetUp() code := m.Run() diff --git a/src/logging/logger.go b/src/logging/logger.go index 641c10f..120290a 100644 --- a/src/logging/logger.go +++ b/src/logging/logger.go @@ -9,6 +9,9 @@ var logger *zap.SugaredLogger // LoggerInitialize is a function to initialize logger func LoggerInitialize() { + if logger != nil { + return + } ginMode := gin.Mode() var i *zap.Logger switch ginMode { From ed214b04925057864aa56b531b18d12dca07e5c8 Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 12:30:43 +0900 Subject: [PATCH 25/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/handler/bad_request_test.go | 41 +++++++++++++++++++++++++++++ src/api/router/router_test.go | 6 +++++ 2 files changed, 47 insertions(+) create mode 100644 src/api/handler/bad_request_test.go diff --git a/src/api/handler/bad_request_test.go b/src/api/handler/bad_request_test.go new file mode 100644 index 0000000..a72352b --- /dev/null +++ b/src/api/handler/bad_request_test.go @@ -0,0 +1,41 @@ +package handler + +import ( + "github.com/gin-gonic/gin" + "net/http/httptest" + "testing" +) + +func testHandler(c *gin.Context) { + BadRequestJson(c, "TestBadRequestJson") +} + +func TestBadRequestJson(t *testing.T) { + r := gin.Default() + r.GET("/", testHandler) + w := httptest.NewRecorder() + ctx := gin.CreateTestContextOnly(w, r) + + request := httptest.NewRequest("GET", "/", nil) + ctx.Request = request + + type args struct { + c *gin.Context + } + tests := []struct { + name string + args args + }{ + { + name: "TestBadRequestJson", + args: args{ + c: ctx, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + testHandler(tt.args.c) + }) + } +} diff --git a/src/api/router/router_test.go b/src/api/router/router_test.go index 423bda0..b85c277 100644 --- a/src/api/router/router_test.go +++ b/src/api/router/router_test.go @@ -14,6 +14,12 @@ func TestMakeRoute(t *testing.T) { args args }{ // TODO: Add test cases. + { + name: "Test MakeRoute", + args: args{ + r: gin.Default(), + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From c007f85e40ecd44e577f018640fa7e44facad00e Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 20:10:34 +0900 Subject: [PATCH 26/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/handler/count_holidays.go | 8 +++--- src/api/handler/count_holidays_test.go | 37 +++++++++++++++++--------- src/api/handler/get_holidays.go | 15 +++++++---- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/api/handler/count_holidays.go b/src/api/handler/count_holidays.go index 5b8dda6..3a4f6f5 100644 --- a/src/api/handler/count_holidays.go +++ b/src/api/handler/count_holidays.go @@ -29,20 +29,20 @@ func CountHolidays(c *gin.Context) { BadRequestJson(c, err.Error()) return } - + logger.Debug("request:", request) db := database.GetDbConnection() dataSet := db.From(TableHolidaysJp) - if request.StartDay != "" && request.EndDay != "" { + if !request.StartDay.IsZero() && !request.EndDay.IsZero() { dataSet = dataSet.Where( goqu.C(ColumnDate).Gte(request.StartDay), goqu.C(ColumnDate).Lte(request.EndDay), ) - } else if request.StartDay != "" { + } else if !request.StartDay.IsZero() { dataSet = dataSet.Where( goqu.C(ColumnDate).Gte(request.StartDay), ) - } else if request.EndDay != "" { + } else if !request.EndDay.IsZero() { dataSet = dataSet.Where( goqu.C(ColumnDate).Lte(request.EndDay), ) diff --git a/src/api/handler/count_holidays_test.go b/src/api/handler/count_holidays_test.go index 7fef7c5..f9bf155 100644 --- a/src/api/handler/count_holidays_test.go +++ b/src/api/handler/count_holidays_test.go @@ -23,25 +23,38 @@ func TestMain(m *testing.M) { func TestCountHolidays(t *testing.T) { r := gin.Default() r.GET("/holidays/count", CountHolidays) - w := httptest.NewRecorder() - req := httptest.NewRequest("GET", "/holidays/count", nil) - context := gin.CreateTestContextOnly(w, r) - context.Request = req - type args struct { - c *gin.Context - } tests := []struct { - name string - args args + name string + target string }{ { - name: "TestCountHolidays", - args: args{c: context}, + name: "TestCountHolidays", + target: "/holidays/count", + }, + { + name: "TestCountHolidaysWithQuery", + target: "/holidays/count?start_day=2021-01-01&end_day=2021-12-31", + }, + { + name: "TestCountHolidaysWithQueryStartDay", + target: "/holidays/count?start_day=2021-01-01", + }, + { + name: "TestCountHolidaysWithQueryEndDay", + target: "/holidays/count?end_day=2021-12-31", + }, + { + name: "TestCountHolidaysWithQueryInvalid", + target: "/holidays/count?start_day=2021-01", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - CountHolidays(tt.args.c) + w := httptest.NewRecorder() + req := httptest.NewRequest("GET", tt.target, nil) + context := gin.CreateTestContextOnly(w, r) + context.Request = req + CountHolidays(context) }) } } diff --git a/src/api/handler/get_holidays.go b/src/api/handler/get_holidays.go index dadbd84..bd6a63f 100644 --- a/src/api/handler/get_holidays.go +++ b/src/api/handler/get_holidays.go @@ -1,6 +1,7 @@ package handler import ( + "fmt" "github.com/gin-gonic/gin" "net/http" "time" @@ -17,8 +18,12 @@ const ( ) type HolidaysRequest struct { - StartDay string `form:"start-day" binding:"datetime"` - EndDay string `form:"end-day" binding:"datetime"` + StartDay time.Time `form:"start_day" time_format:"2006-01-02"` + EndDay time.Time `form:"end_day" time_format:"2006-01-02"` +} + +func (receiver HolidaysRequest) String() string { + return fmt.Sprintf("StartDay: \"%s\", EndDay: \"%s\"", receiver.StartDay, receiver.EndDay) } func GetHolidays(c *gin.Context) { @@ -43,16 +48,16 @@ func GetHolidays(c *gin.Context) { } // リクエストパラメータから開始日と終了日を取得 dataSet := db.From(TableHolidaysJp).Order(goqu.C(ColumnDate).Asc()) - if reqParams.StartDay != "" && reqParams.EndDay != "" { + if !reqParams.StartDay.IsZero() && !reqParams.EndDay.IsZero() { dataSet = dataSet.Where( goqu.C(ColumnDate).Gte(reqParams.StartDay), goqu.C(ColumnDate).Lte(reqParams.EndDay), ) - } else if reqParams.StartDay != "" { + } else if !reqParams.StartDay.IsZero() { dataSet = dataSet.Where( goqu.C(ColumnDate).Gte(reqParams.StartDay), ) - } else if reqParams.EndDay != "" { + } else if !reqParams.EndDay.IsZero() { dataSet = dataSet.Where( goqu.C(ColumnDate).Lte(reqParams.EndDay), ) From 35615e355c91324165a42fd840450ab4679c5618 Mon Sep 17 00:00:00 2001 From: kynmh69 Date: Sun, 29 Sep 2024 20:52:56 +0900 Subject: [PATCH 27/27] =?UTF-8?q?Gin=E3=81=B8=E3=81=AE=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/handler/get_holidays_test.go | 86 ++++++++++++++++++++++++++++ src/api/handler/is_holiday.go | 2 + src/api/handler/is_holiday_test.go | 40 +++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 src/api/handler/get_holidays_test.go create mode 100644 src/api/handler/is_holiday_test.go diff --git a/src/api/handler/get_holidays_test.go b/src/api/handler/get_holidays_test.go new file mode 100644 index 0000000..053e3c2 --- /dev/null +++ b/src/api/handler/get_holidays_test.go @@ -0,0 +1,86 @@ +package handler + +import ( + "github.com/gin-gonic/gin" + "github.com/kynmh69/go-ja-holidays/util" + "net/http/httptest" + "os" + "testing" + "time" +) + +func TestGetHolidays(t *testing.T) { + _ = os.Setenv("DATABASE", "unittest") + _ = os.Setenv("PSQL_HOSTNAME", "localhost") + util.SetUp() + r := gin.Default() + r.GET("/holidays", GetHolidays) + tests := []struct { + name string + target string + }{ + { + name: "TestGetHolidays", + target: "/holidays", + }, + { + name: "TestGetHolidaysWithQuery", + target: "/holidays?start_day=2021-01-01&end_day=2021-12-31", + }, + { + name: "TestGetHolidaysWithQueryStartDay", + target: "/holidays?start_day=2021-01-01", + }, + { + name: "TestGetHolidaysWithQueryEndDay", + target: "/holidays?end_day=2021-12-31", + }, + { + name: "TestGetHolidaysWithQueryInvalid", + target: "/holidays?start_day=2021-01", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + w := httptest.NewRecorder() + req := httptest.NewRequest("GET", tt.target, nil) + context := gin.CreateTestContextOnly(w, r) + context.Request = req + GetHolidays(context) + }) + } +} + +func TestHolidaysRequest_String(t *testing.T) { + util.SetUp() + now := time.Now() + type fields struct { + StartDay time.Time + EndDay time.Time + } + tests := []struct { + name string + fields fields + want string + }{ + { + name: "TestHolidaysRequest_String", + fields: fields{ + StartDay: now, + EndDay: now, + }, + want: "StartDay: \"" + now.String() + "\", EndDay: \"" + now.String() + "\"", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + receiver := HolidaysRequest{ + StartDay: tt.fields.StartDay, + EndDay: tt.fields.EndDay, + } + if got := receiver.String(); got != tt.want { + t.Errorf("String() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/src/api/handler/is_holiday.go b/src/api/handler/is_holiday.go index 32b7c90..4b8d109 100644 --- a/src/api/handler/is_holiday.go +++ b/src/api/handler/is_holiday.go @@ -49,8 +49,10 @@ func IsHoliday(c *gin.Context) { var isHoliday model.IsHoliday if ok { + // If the holiday data exists, return it. isHoliday = model.IsHoliday{IsHoliday: ok, HolidayData: holiday} } else { + // If the holiday data does not exist, return the date. isHoliday = model.IsHoliday{IsHoliday: ok, HolidayData: model.HolidayData{Date: request.Date}} } logger.Debug(isHoliday) diff --git a/src/api/handler/is_holiday_test.go b/src/api/handler/is_holiday_test.go new file mode 100644 index 0000000..0518260 --- /dev/null +++ b/src/api/handler/is_holiday_test.go @@ -0,0 +1,40 @@ +package handler + +import ( + "github.com/gin-gonic/gin" + "github.com/kynmh69/go-ja-holidays/util" + "net/http/httptest" + "os" + "testing" +) + +func TestIsHoliday(t *testing.T) { + _ = os.Setenv("DATABASE", "unittest") + _ = os.Setenv("PSQL_HOSTNAME", "localhost") + util.SetUp() + r := gin.Default() + r.GET("/holidays/:date", IsHoliday) + + tests := []struct { + name string + target string + }{ + { + name: "TestIsHoliday", + target: "/holidays/2021-01-01", + }, + { + name: "TestIsHolidayInvalid", + target: "/holidays/2021-01", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + w := httptest.NewRecorder() + req := httptest.NewRequest("GET", tt.target, nil) + context := gin.CreateTestContextOnly(w, r) + context.Request = req + IsHoliday(context) + }) + } +}