diff --git a/go.mod b/go.mod index baff8040..fa8752c2 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/traPtitech/Jomon -go 1.24.11 +go 1.25.0 require ( ariga.io/atlas v1.0.0 @@ -12,8 +12,8 @@ require ( github.com/google/go-cmp v0.7.0 github.com/google/uuid v1.6.0 github.com/gorilla/sessions v1.4.0 - github.com/labstack/echo-contrib v0.17.4 - github.com/labstack/echo/v4 v4.14.0 + github.com/labstack/echo-contrib/v5 v5.0.1 + github.com/labstack/echo/v5 v5.0.4 github.com/ncw/swift v1.0.53 github.com/samber/lo v1.52.0 github.com/stretchr/testify v1.11.1 @@ -37,13 +37,9 @@ require ( github.com/bmatcuk/doublestar v1.3.4 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-openapi/inflect v0.21.0 // indirect - github.com/gorilla/context v1.1.2 // indirect github.com/gorilla/securecookie v1.1.2 // indirect github.com/hashicorp/hcl/v2 v2.23.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/labstack/gommon v0.4.2 // indirect - github.com/mattn/go-colorable v0.1.14 // indirect - github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.9 // indirect github.com/mitchellh/go-wordwrap v1.0.1 // indirect github.com/olekukonko/tablewriter v0.0.5 // indirect @@ -51,19 +47,14 @@ require ( github.com/rogpeppe/go-internal v1.14.1 // indirect github.com/spf13/cobra v1.7.0 // indirect github.com/spf13/pflag v1.0.5 // indirect - github.com/valyala/bytebufferpool v1.0.0 // indirect - github.com/valyala/fasttemplate v1.2.2 // indirect github.com/zclconf/go-cty v1.15.1 // indirect github.com/zclconf/go-cty-yaml v1.1.0 // indirect go.uber.org/multierr v1.11.0 // indirect - golang.org/x/crypto v0.46.0 // indirect - golang.org/x/mod v0.30.0 // indirect - golang.org/x/net v0.48.0 // indirect + golang.org/x/mod v0.31.0 // indirect golang.org/x/sync v0.19.0 // indirect - golang.org/x/sys v0.39.0 // indirect - golang.org/x/text v0.32.0 // indirect + golang.org/x/text v0.33.0 // indirect golang.org/x/time v0.14.0 // indirect - golang.org/x/tools v0.39.0 // indirect + golang.org/x/tools v0.40.0 // indirect golang.org/x/tools/go/packages/packagestest v0.1.1-deprecated // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index c1cdc860..f77b665c 100644 --- a/go.sum +++ b/go.sum @@ -67,8 +67,6 @@ github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= github.com/google/gofuzz v1.2.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/gorilla/context v1.1.2 h1:WRkNAv2uoa03QNIc1A6u4O7DAGMUVoopZhkiXWA2V1o= -github.com/gorilla/context v1.1.2/go.mod h1:KDPwT9i/MeWHiLl90fuTgrt4/wPcv75vFAZLaOOcbxM= github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA= github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo= github.com/gorilla/sessions v1.4.0 h1:kpIYOp/oi6MG/p5PgxApU8srsSw9tuFbt46Lt7auzqQ= @@ -81,16 +79,10 @@ github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= 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-contrib v0.17.4 h1:g5mfsrJfJTKv+F5uNKCyrjLK7js+ZW6HTjg4FnDxxgk= -github.com/labstack/echo-contrib v0.17.4/go.mod h1:9O7ZPAHUeMGTOAfg80YqQduHzt0CzLak36PZRldYrZ0= -github.com/labstack/echo/v4 v4.14.0 h1:+tiMrDLxwv6u0oKtD03mv+V1vXXB3wCqPHJqPuIe+7M= -github.com/labstack/echo/v4 v4.14.0/go.mod h1:xmw1clThob0BSVRX1CRQkGQ/vjwcpOMjQZSZa9fKA/c= -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/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE= -github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= -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/labstack/echo-contrib/v5 v5.0.1 h1:Z23m8p1F9Doax+pa8ek8ll5d6wvzCbvrAn25ZWmSgrE= +github.com/labstack/echo-contrib/v5 v5.0.1/go.mod h1:ajrqEwSloW0vQSTfJ4j9u4tHoS+dg4umCDPxuTvChZs= +github.com/labstack/echo/v5 v5.0.4 h1:ll3I/O8BifjMztj9dD1vx/peZQv8cR2CTUdQK6QxGGc= +github.com/labstack/echo/v5 v5.0.4/go.mod h1:SyvlSdObGjRXeQfCCXW/sybkZdOOQZBmpKF0bvALaeo= github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A= @@ -114,10 +106,6 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= -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= github.com/zclconf/go-cty v1.15.1 h1:RgQYm4j2EvoBRXOPxhUvxPzRrGDo1eCOhHXuGfrj5S0= github.com/zclconf/go-cty v1.15.1/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE= github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940 h1:4r45xpDWB6ZMSMNJFMOjqrGHynW3DIBuR2H9j0ug+Mo= @@ -132,23 +120,18 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.27.1 h1:08RqriUEv8+ArZRYSTXy1LeBScaMpVSTBhCeaZYfMYc= go.uber.org/zap v1.27.1/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= -golang.org/x/crypto v0.46.0 h1:cKRW/pmt1pKAfetfu+RCEvjvZkA9RimPbh7bhFjGVBU= -golang.org/x/crypto v0.46.0/go.mod h1:Evb/oLKmMraqjZ2iQTwDwvCtJkczlDuTmdJXoZVzqU0= -golang.org/x/mod v0.30.0 h1:fDEXFVZ/fmCKProc/yAXXUijritrDzahmwwefnjoPFk= -golang.org/x/mod v0.30.0/go.mod h1:lAsf5O2EvJeSFMiBxXDki7sCgAxEUcZHXoXMKT4GJKc= -golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU= -golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY= +golang.org/x/mod v0.31.0 h1:HaW9xtz0+kOcWKwli0ZXy79Ix+UW/vOfmWI5QVd2tgI= +golang.org/x/mod v0.31.0/go.mod h1:43JraMp9cGx1Rx3AqioxrbrhNsLl2l/iNAvuBkrezpg= +golang.org/x/net v0.49.0 h1:eeHFmOGUTtaaPSGNmjBKpbng9MulQsJURQUAfUwY++o= +golang.org/x/net v0.49.0/go.mod h1:/ysNB2EvaqvesRkuLAyjI1ycPZlQHM3q01F02UY/MV8= golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4= golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk= -golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= -golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU= -golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY= +golang.org/x/text v0.33.0 h1:B3njUFyqtHDUI5jMn1YIr5B0IE2U0qck04r6d4KPAxE= +golang.org/x/text v0.33.0/go.mod h1:LuMebE6+rBincTi9+xWTY8TztLzKHc/9C1uBCG27+q8= golang.org/x/time v0.14.0 h1:MRx4UaLrDotUKUdCIqzPC48t1Y9hANFKIRpNx+Te8PI= golang.org/x/time v0.14.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4= -golang.org/x/tools v0.39.0 h1:ik4ho21kwuQln40uelmciQPp9SipgNDdrafrYA4TmQQ= -golang.org/x/tools v0.39.0/go.mod h1:JnefbkDPyD8UU2kI5fuf8ZX4/yUeh9W877ZeBONxUqQ= +golang.org/x/tools v0.40.0 h1:yLkxfA+Qnul4cs9QA3KnlFu0lVmd8JJfoq+E41uSutA= +golang.org/x/tools v0.40.0/go.mod h1:Ik/tzLRlbscWpqqMRjyWYDisX8bG13FrdXp3o4Sr9lc= golang.org/x/tools/go/expect v0.1.0-deprecated h1:jY2C5HGYR5lqex3gEniOQL0r7Dq5+VGVgY1nudX5lXY= golang.org/x/tools/go/expect v0.1.0-deprecated/go.mod h1:eihoPOH+FgIqa3FpoTwguz/bVUSGBlGQU67vpBeOrBY= golang.org/x/tools/go/packages/packagestest v0.1.1-deprecated h1:1h2MnaIAIXISqTFKdENegdpAgUXz6NrPEsbIeWaBRvM= diff --git a/internal/router/account_manager.go b/internal/router/account_manager.go index 789a87a5..a387e11a 100644 --- a/internal/router/account_manager.go +++ b/internal/router/account_manager.go @@ -4,7 +4,7 @@ import ( "net/http" "github.com/google/uuid" - "github.com/labstack/echo/v4" + "github.com/labstack/echo/v5" "github.com/samber/lo" "github.com/traPtitech/Jomon/internal/logging" "github.com/traPtitech/Jomon/internal/model" @@ -12,7 +12,7 @@ import ( "go.uber.org/zap" ) -func (h Handlers) GetAccountManagers(c echo.Context) error { +func (h Handlers) GetAccountManagers(c *echo.Context) error { ctx := c.Request().Context() logger := logging.GetLogger(ctx) accountManagers, err := h.Repository.GetAccountManagers(ctx) @@ -28,7 +28,7 @@ func (h Handlers) GetAccountManagers(c echo.Context) error { return c.JSON(http.StatusOK, res) } -func (h Handlers) PostAccountManagers(c echo.Context) error { +func (h Handlers) PostAccountManagers(c *echo.Context) error { ctx := c.Request().Context() logger := logging.GetLogger(ctx) @@ -48,7 +48,7 @@ func (h Handlers) PostAccountManagers(c echo.Context) error { return c.NoContent(http.StatusOK) } -func (h Handlers) DeleteAccountManagers(c echo.Context) error { +func (h Handlers) DeleteAccountManagers(c *echo.Context) error { ctx := c.Request().Context() logger := logging.GetLogger(ctx) diff --git a/internal/router/account_manager_test.go b/internal/router/account_manager_test.go index fbd42d2e..78e959bf 100644 --- a/internal/router/account_manager_test.go +++ b/internal/router/account_manager_test.go @@ -9,7 +9,7 @@ import ( "testing" "github.com/google/uuid" - "github.com/labstack/echo/v4" + "github.com/labstack/echo/v5" "github.com/stretchr/testify/require" "github.com/traPtitech/Jomon/internal/model" "github.com/traPtitech/Jomon/internal/service" diff --git a/internal/router/application.go b/internal/router/application.go index 220febce..e5d3d964 100644 --- a/internal/router/application.go +++ b/internal/router/application.go @@ -8,7 +8,7 @@ import ( "time" "github.com/google/uuid" - "github.com/labstack/echo/v4" + "github.com/labstack/echo/v5" "github.com/samber/lo" "github.com/traPtitech/Jomon/internal/logging" "github.com/traPtitech/Jomon/internal/model" @@ -116,7 +116,7 @@ type TargetOverview struct { CreatedAt time.Time `json:"created_at"` } -func (h Handlers) GetApplications(c echo.Context) error { +func (h Handlers) GetApplications(c *echo.Context) error { ctx := c.Request().Context() logger := logging.GetLogger(ctx) @@ -268,7 +268,7 @@ func (h Handlers) GetApplications(c echo.Context) error { return c.JSON(http.StatusOK, applications) } -func (h Handlers) PostApplication(c echo.Context) error { +func (h Handlers) PostApplication(c *echo.Context) error { var req Application var err error if err = c.Bind(&req); err != nil { @@ -362,7 +362,7 @@ func (h Handlers) PostApplication(c echo.Context) error { return c.JSON(http.StatusOK, res) } -func (h Handlers) GetApplication(c echo.Context) error { +func (h Handlers) GetApplication(c *echo.Context) error { ctx := c.Request().Context() logger := logging.GetLogger(ctx) @@ -448,7 +448,7 @@ func (h Handlers) GetApplication(c echo.Context) error { return c.JSON(http.StatusOK, res) } -func (h Handlers) PutApplication(c echo.Context) error { +func (h Handlers) PutApplication(c *echo.Context) error { ctx := c.Request().Context() logger := logging.GetLogger(ctx) @@ -563,7 +563,7 @@ func (h Handlers) PutApplication(c echo.Context) error { return c.JSON(http.StatusOK, res) } -func (h Handlers) PostComment(c echo.Context) error { +func (h Handlers) PostComment(c *echo.Context) error { ctx := c.Request().Context() logger := logging.GetLogger(ctx) @@ -601,7 +601,7 @@ func (h Handlers) PostComment(c echo.Context) error { return c.JSON(http.StatusOK, res) } -func (h Handlers) PutStatus(c echo.Context) error { +func (h Handlers) PutStatus(c *echo.Context) error { ctx := c.Request().Context() logger := logging.GetLogger(ctx) diff --git a/internal/router/application_test.go b/internal/router/application_test.go index bb4ed572..2bb32ff4 100644 --- a/internal/router/application_test.go +++ b/internal/router/application_test.go @@ -11,7 +11,7 @@ import ( "time" "github.com/google/uuid" - "github.com/labstack/echo/v4" + "github.com/labstack/echo/v5" "github.com/samber/lo" "github.com/stretchr/testify/require" "github.com/traPtitech/Jomon/internal/model" @@ -919,8 +919,9 @@ func TestHandlers_GetApplication(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/applications/:applicationID") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) @@ -990,8 +991,9 @@ func TestHandlers_GetApplication(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/applications/:applicationID") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) @@ -1056,8 +1058,9 @@ func TestHandlers_GetApplication(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/applications/:applicationID") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) @@ -1095,8 +1098,9 @@ func TestHandlers_GetApplication(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/applications/:applicationID") - c.SetParamNames("applicationID") - c.SetParamValues(invalidUUID) + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: invalidUUID}, + }) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) @@ -1117,8 +1121,9 @@ func TestHandlers_GetApplication(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/applications/:applicationID") - c.SetParamNames("applicationID") - c.SetParamValues(uuid.Nil.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: uuid.Nil.String()}, + }) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) @@ -1141,8 +1146,9 @@ func TestHandlers_GetApplication(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/applications/:applicationID") - c.SetParamNames("applicationID") - c.SetParamValues(unknownID.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: unknownID.String()}, + }) resErr := service.NewNotFoundError("user not found") h, err := NewTestHandlers(t, ctrl) @@ -1218,9 +1224,10 @@ func TestHandlers_PutApplication(t *testing.T) { req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - c.SetPath("api/applications/:applicationID") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPath("/api/applications/:applicationID") + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) c.Set(loginUserKey, user) targets := lo.Map( @@ -1335,9 +1342,10 @@ func TestHandlers_PutApplication(t *testing.T) { req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - c.SetPath("api/applications/:applicationID") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPath("/api/applications/:applicationID") + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) @@ -1448,9 +1456,10 @@ func TestHandlers_PutApplication(t *testing.T) { req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - c.SetPath("api/applications/:applicationID") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPath("/api/applications/:applicationID") + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) c.Set(loginUserKey, user) targets := lo.Map( @@ -1559,9 +1568,10 @@ func TestHandlers_PutApplication(t *testing.T) { req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - c.SetPath("api/applications/:applicationID") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPath("/api/applications/:applicationID") + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) c.Set(loginUserKey, user) targets := lo.Map( @@ -1611,8 +1621,9 @@ func TestHandlers_PutApplication(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/applications/:applicationID") - c.SetParamNames("applicationID") - c.SetParamValues(invalidUUID) + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: invalidUUID}, + }) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) @@ -1634,8 +1645,9 @@ func TestHandlers_PutApplication(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/applications/:applicationID") - c.SetParamNames("applicationID") - c.SetParamValues(uuid.Nil.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: uuid.Nil.String()}, + }) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) @@ -1669,8 +1681,9 @@ func TestHandlers_PutApplication(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/applications/:applicationID") - c.SetParamNames("applicationID") - c.SetParamValues(unknownID.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: unknownID.String()}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) @@ -1734,8 +1747,9 @@ func TestHandlers_PutApplication(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/applications/:applicationID") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) @@ -1811,9 +1825,10 @@ func TestHandlers_PutStatus(t *testing.T) { req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - c.SetPath("api/applications/:applicationID/status") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPath("/api/applications/:applicationID/status") + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) @@ -1906,9 +1921,10 @@ func TestHandlers_PutStatus(t *testing.T) { req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - c.SetPath("api/applications/:applicationID/status") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPath("/api/applications/:applicationID/status") + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) @@ -2001,9 +2017,10 @@ func TestHandlers_PutStatus(t *testing.T) { req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - c.SetPath("api/applications/:applicationID/status") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPath("/api/applications/:applicationID/status") + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) @@ -2098,8 +2115,9 @@ func TestHandlers_PutStatus(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/applications/:applicationID/status") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) @@ -2192,9 +2210,10 @@ func TestHandlers_PutStatus(t *testing.T) { req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - c.SetPath("api/applications/:applicationID/status") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPath("/api/applications/:applicationID/status") + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) @@ -2294,9 +2313,10 @@ func TestHandlers_PutStatus(t *testing.T) { req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - c.SetPath("api/applications/:applicationID/status") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPath("/api/applications/:applicationID/status") + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) @@ -2383,9 +2403,10 @@ func TestHandlers_PutStatus(t *testing.T) { req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - c.SetPath("api/applications/:applicationID/status") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPath("/api/applications/:applicationID/status") + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) @@ -2417,9 +2438,10 @@ func TestHandlers_PutStatus(t *testing.T) { req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - c.SetPath("api/applications/:applicationID/status") - c.SetParamNames("applicationID") - c.SetParamValues(invalidUUID) + c.SetPath("/api/applications/:applicationID/status") + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: invalidUUID}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) @@ -2450,9 +2472,10 @@ func TestHandlers_PutStatus(t *testing.T) { req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - c.SetPath("api/applications/:applicationID/status") - c.SetParamNames("applicationID") - c.SetParamValues(uuid.Nil.String()) + c.SetPath("/api/applications/:applicationID/status") + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: uuid.Nil.String()}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) @@ -2503,9 +2526,10 @@ func TestHandlers_PutStatus(t *testing.T) { req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - c.SetPath("api/applications/:applicationID/status") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPath("/api/applications/:applicationID/status") + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) @@ -2561,9 +2585,10 @@ func TestHandlers_PutStatus(t *testing.T) { req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - c.SetPath("api/applications/:applicationID/status") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPath("/api/applications/:applicationID/status") + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) @@ -2618,9 +2643,10 @@ func TestHandlers_PutStatus(t *testing.T) { req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - c.SetPath("api/applications/:applicationID/status") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPath("/api/applications/:applicationID/status") + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) @@ -2675,9 +2701,10 @@ func TestHandlers_PutStatus(t *testing.T) { req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - c.SetPath("api/applications/:applicationID/status") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPath("/api/applications/:applicationID/status") + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) @@ -2733,9 +2760,10 @@ func TestHandlers_PutStatus(t *testing.T) { req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - c.SetPath("api/applications/:applicationID/status") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPath("/api/applications/:applicationID/status") + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) @@ -2798,9 +2826,10 @@ func TestHandlers_PutStatus(t *testing.T) { req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - c.SetPath("api/applications/:applicationID/status") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPath("/api/applications/:applicationID/status") + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) @@ -2861,9 +2890,10 @@ func TestHandlers_PutStatus(t *testing.T) { req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - c.SetPath("api/applications/:applicationID/status") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPath("/api/applications/:applicationID/status") + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) @@ -2919,9 +2949,10 @@ func TestHandlers_PutStatus(t *testing.T) { req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - c.SetPath("api/applications/:applicationID/status") - c.SetParamNames("applicationID") - c.SetParamValues(application.ID.String()) + c.SetPath("/api/applications/:applicationID/status") + c.SetPathValues([]echo.PathValue{ + {Name: "applicationID", Value: application.ID.String()}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) diff --git a/internal/router/auth.go b/internal/router/auth.go index 5290d91a..372e432f 100644 --- a/internal/router/auth.go +++ b/internal/router/auth.go @@ -10,7 +10,7 @@ import ( "net/http" "sync" - "github.com/labstack/echo/v4" + "github.com/labstack/echo/v5" "github.com/traPtitech/Jomon/internal/logging" "github.com/traPtitech/Jomon/internal/model" "github.com/traPtitech/Jomon/internal/router/wrapsession" @@ -25,7 +25,7 @@ type AuthResponse struct { RefreshToken string `json:"refresh_token"` } -func (h Handlers) AuthCallback(c echo.Context) error { +func (h Handlers) AuthCallback(c *echo.Context) error { ctx := c.Request().Context() logger := logging.GetLogger(ctx) @@ -99,7 +99,7 @@ func (h Handlers) AuthCallback(c echo.Context) error { return c.Redirect(http.StatusSeeOther, location) } -func (h Handlers) GeneratePKCE(c echo.Context) error { +func (h Handlers) GeneratePKCE(c *echo.Context) error { codeVerifier := randAlphabetAndNumberString(43) _, err := wrapsession.WithSession(c, h.SessionName, func(w *wrapsession.W) (struct{}, error) { diff --git a/internal/router/error.go b/internal/router/error.go index 731478f4..d16ee4e2 100644 --- a/internal/router/error.go +++ b/internal/router/error.go @@ -4,7 +4,7 @@ import ( "errors" "net/http" - "github.com/labstack/echo/v4" + "github.com/labstack/echo/v5" "github.com/traPtitech/Jomon/internal/router/wrapsession" "github.com/traPtitech/Jomon/internal/service" @@ -14,29 +14,33 @@ func HTTPErrorHandlerInner(err error) *echo.HTTPError { if err == nil { return nil } + // FIXME(error-handling): type switchで最上位のエラーに対してhandlingする if he := new(echo.HTTPError); errors.As(err, &he) { return he } + var herr error if e := new(service.BadInputError); errors.As(err, &e) { - return echo.NewHTTPError(http.StatusBadRequest, e.Message).SetInternal(e) - } - if e := new(service.NotFoundError); errors.As(err, &e) { - return echo.NewHTTPError(http.StatusNotFound, e.Message).SetInternal(e) - } - if e := new(service.ForbiddenError); errors.As(err, &e) { - return echo.NewHTTPError(http.StatusForbidden, e.Message).SetInternal(e) - } - if e := new(service.UnauthenticatedError); errors.As(err, &e) { - return echo.NewHTTPError(http.StatusUnauthorized, e.Message).SetInternal(e) - } - if e := new(service.UnexpectedError); errors.As(err, &e) { - return echo.ErrInternalServerError.WithInternal(e) - } - if e := new(wrapsession.GetSessionError); errors.As(err, &e) { - return echo.ErrInternalServerError.WithInternal(e) - } - if e := new(wrapsession.SaveSessionError); errors.As(err, &e) { - return echo.ErrInternalServerError.WithInternal(e) - } - return echo.ErrInternalServerError.WithInternal(err) + herr = echo.NewHTTPError(http.StatusBadRequest, e.Message).Wrap(e) + } else if e := new(service.NotFoundError); errors.As(err, &e) { + herr = echo.NewHTTPError(http.StatusNotFound, e.Message).Wrap(e) + } else if e := new(service.ForbiddenError); errors.As(err, &e) { + herr = echo.NewHTTPError(http.StatusForbidden, e.Message).Wrap(e) + } else if e := new(service.UnauthenticatedError); errors.As(err, &e) { + herr = echo.NewHTTPError(http.StatusUnauthorized, e.Message).Wrap(e) + } else if e := new(service.UnexpectedError); errors.As(err, &e) { + herr = echo.ErrInternalServerError.Wrap(e) + } else if e := new(wrapsession.GetSessionError); errors.As(err, &e) { + herr = echo.ErrInternalServerError.Wrap(e) + } else if e := new(wrapsession.SaveSessionError); errors.As(err, &e) { + herr = echo.ErrInternalServerError.Wrap(e) + } else { + herr = echo.ErrInternalServerError.Wrap(err) + } + he := new(echo.HTTPError) + if !errors.As(herr, &he) { + // ここには来ないはず + code := http.StatusInternalServerError + return echo.NewHTTPError(code, http.StatusText(code)) + } + return he } diff --git a/internal/router/file.go b/internal/router/file.go index 4cc2bd2c..1cfd55cd 100644 --- a/internal/router/file.go +++ b/internal/router/file.go @@ -7,7 +7,7 @@ import ( "time" "github.com/google/uuid" - "github.com/labstack/echo/v4" + "github.com/labstack/echo/v5" "github.com/traPtitech/Jomon/internal/ent" "github.com/traPtitech/Jomon/internal/logging" "github.com/traPtitech/Jomon/internal/service" @@ -40,7 +40,7 @@ var ( "user is not accountManager or file creator") ) -func (h Handlers) PostFile(c echo.Context) error { +func (h Handlers) PostFile(c *echo.Context) error { ctx := c.Request().Context() logger := logging.GetLogger(ctx) @@ -105,7 +105,7 @@ func (h Handlers) PostFile(c echo.Context) error { return c.JSON(http.StatusOK, &FileResponse{file.ID}) } -func (h Handlers) GetFile(c echo.Context) error { +func (h Handlers) GetFile(c *echo.Context) error { ctx := c.Request().Context() logger := logging.GetLogger(ctx) @@ -120,7 +120,7 @@ func (h Handlers) GetFile(c echo.Context) error { if err != nil { if ent.IsNotFound(err) { logger.Info("could not find file in repository", zap.String("ID", fileID.String())) - return echo.NewHTTPError(http.StatusNotFound, err) + return echo.ErrNotFound.Wrap(err) } logger.Error("failed to get file from repository", zap.Error(err)) return service.NewUnexpectedError(err) @@ -161,7 +161,7 @@ func (h Handlers) GetFile(c echo.Context) error { return c.Stream(http.StatusOK, file.MimeType, f) } -func (h Handlers) GetFileMeta(c echo.Context) error { +func (h Handlers) GetFileMeta(c *echo.Context) error { ctx := c.Request().Context() logger := logging.GetLogger(ctx) @@ -176,7 +176,7 @@ func (h Handlers) GetFileMeta(c echo.Context) error { if err != nil { if ent.IsNotFound(err) { logger.Info("could not find file in repository", zap.String("ID", fileID.String())) - return echo.NewHTTPError(http.StatusNotFound, err) + return echo.ErrNotFound.Wrap(err) } logger.Error("failed to get file from repository", zap.Error(err)) return service.NewUnexpectedError(err) @@ -191,7 +191,7 @@ func (h Handlers) GetFileMeta(c echo.Context) error { }) } -func (h Handlers) DeleteFile(c echo.Context) error { +func (h Handlers) DeleteFile(c *echo.Context) error { ctx := c.Request().Context() logger := logging.GetLogger(ctx) @@ -210,16 +210,16 @@ func (h Handlers) DeleteFile(c echo.Context) error { if err != nil { if ent.IsConstraintError(err) { logger.Info("constraint error while deleting file", zap.Error(err)) - return echo.NewHTTPError(http.StatusBadRequest, err) + return echo.ErrBadRequest.Wrap(err) } logger.Error("failed to delete file in repository", zap.Error(err)) - return echo.NewHTTPError(http.StatusInternalServerError, err) + return service.NewUnexpectedError(err) } err = h.Storage.Delete(ctx, fileID.String()) if err != nil { logger.Error("failed to delete file in storage", zap.Error(err)) - return echo.NewHTTPError(http.StatusInternalServerError, err) + return service.NewUnexpectedError(err) } return c.NoContent(http.StatusOK) @@ -236,7 +236,7 @@ func (h Handlers) isFileCreator(ctx context.Context, userID, fileID uuid.UUID) ( func (h Handlers) filterAccountManagerOrFileCreator( ctx context.Context, user *User, fileID uuid.UUID, -) *echo.HTTPError { +) error { logger := logging.GetLogger(ctx) if user.AccountManager { return nil @@ -244,10 +244,10 @@ func (h Handlers) filterAccountManagerOrFileCreator( isCreator, err := h.isFileCreator(ctx, user.ID, fileID) if err != nil { logger.Error("failed to check if user is file creator", zap.Error(err)) - return echo.NewHTTPError(http.StatusInternalServerError, err) + return echo.ErrInternalServerError.Wrap(err) } if isCreator { return nil } - return echo.NewHTTPError(http.StatusForbidden, errUserIsNotAccountManagerOrFileCreator) + return echo.ErrForbidden.Wrap(errUserIsNotAccountManagerOrFileCreator) } diff --git a/internal/router/file_test.go b/internal/router/file_test.go index 3a5068b5..df9e1c3e 100644 --- a/internal/router/file_test.go +++ b/internal/router/file_test.go @@ -15,7 +15,7 @@ import ( "time" "github.com/google/uuid" - "github.com/labstack/echo/v4" + "github.com/labstack/echo/v5" "github.com/stretchr/testify/require" "github.com/traPtitech/Jomon/internal/model" "github.com/traPtitech/Jomon/internal/service" @@ -238,8 +238,9 @@ func TestHandlers_GetFile(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/files/:fileID") - c.SetParamNames("fileID") - c.SetParamValues(file.ID.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "fileID", Value: file.ID.String()}, + }) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) @@ -275,8 +276,9 @@ func TestHandlers_GetFile(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/files/:fileID") - c.SetParamNames("fileID") - c.SetParamValues(file.ID.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "fileID", Value: file.ID.String()}, + }) resErr := service.NewUnexpectedError(errors.New("file not found")) @@ -310,8 +312,9 @@ func TestHandlers_GetFile(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/files/:fileID") - c.SetParamNames("fileID") - c.SetParamValues(file.ID.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "fileID", Value: file.ID.String()}, + }) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) @@ -342,8 +345,9 @@ func TestHandlers_GetFile(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/files/:fileID") - c.SetParamNames("fileID") - c.SetParamValues("po") + c.SetPathValues([]echo.PathValue{ + {Name: "fileID", Value: "po"}, + }) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) @@ -376,8 +380,9 @@ func TestHandlers_GetFileMeta(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/files/:fileID/meta") - c.SetParamNames("fileID") - c.SetParamValues(file.ID.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "fileID", Value: file.ID.String()}, + }) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) @@ -420,8 +425,9 @@ func TestHandlers_GetFileMeta(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/files/:fileID/meta") - c.SetParamNames("fileID") - c.SetParamValues(file.ID.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "fileID", Value: file.ID.String()}, + }) resErr := service.NewUnexpectedError(errors.New("file not found")) @@ -447,8 +453,9 @@ func TestHandlers_GetFileMeta(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/files/:fileID/meta") - c.SetParamNames("fileID") - c.SetParamValues("po") + c.SetPathValues([]echo.PathValue{ + {Name: "fileID", Value: "po"}, + }) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) @@ -484,8 +491,9 @@ func TestHandlers_DeleteFile(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/files/:fileID") - c.SetParamNames("fileID") - c.SetParamValues(file.ID.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "fileID", Value: file.ID.String()}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) @@ -529,8 +537,9 @@ func TestHandlers_DeleteFile(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/files/:fileID") - c.SetParamNames("fileID") - c.SetParamValues(file.ID.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "fileID", Value: file.ID.String()}, + }) c.Set(loginUserKey, user) resErr := service.NewUnexpectedError(errors.New("file could not be deleted")) @@ -573,8 +582,9 @@ func TestHandlers_DeleteFile(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/files/:fileID") - c.SetParamNames("fileID") - c.SetParamValues(file.ID.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "fileID", Value: file.ID.String()}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) @@ -616,8 +626,9 @@ func TestHandlers_DeleteFile(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/files/:fileID") - c.SetParamNames("fileID") - c.SetParamValues(invalidUUID) + c.SetPathValues([]echo.PathValue{ + {Name: "fileID", Value: invalidUUID}, + }) c.Set(loginUserKey, user) h, err := NewTestHandlers(t, ctrl) diff --git a/internal/router/middleware.go b/internal/router/middleware.go index 2e4571f1..4a9874dd 100644 --- a/internal/router/middleware.go +++ b/internal/router/middleware.go @@ -6,7 +6,7 @@ import ( "time" "github.com/google/uuid" - "github.com/labstack/echo/v4" + "github.com/labstack/echo/v5" "github.com/traPtitech/Jomon/internal/ent" "github.com/traPtitech/Jomon/internal/logging" "github.com/traPtitech/Jomon/internal/router/wrapsession" @@ -20,7 +20,7 @@ const ( func (h Handlers) setLoggerMiddleware(logger *zap.Logger) echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { - return func(c echo.Context) error { + return func(c *echo.Context) error { req := c.Request() ctx := req.Context() reqID := req.Header.Get(echo.HeaderXRequestID) @@ -34,31 +34,43 @@ func (h Handlers) setLoggerMiddleware(logger *zap.Logger) echo.MiddlewareFunc { // AccessLoggingMiddleware ですべてのエラーを出力する func (h Handlers) AccessLoggingMiddleware(next echo.HandlerFunc) echo.HandlerFunc { - return func(c echo.Context) error { + // TODO(logging): https://echo.labstack.com/docs/middleware/logger を使う + return func(c *echo.Context) error { start := time.Now() err := next(c) if err != nil { - c.Error(err) + defaultHTTPErrorHandler(c, HTTPErrorHandlerInner(err)) } stop := time.Now() req := c.Request() - res := c.Response() - logger := logging.GetLogger(req.Context()) latency := strconv.FormatFloat(stop.Sub(start).Seconds(), 'f', 9, 64) + "s" fields := []zapcore.Field{ zap.String("requestMethod", req.Method), - zap.Int("status", res.Status), zap.String("userAgent", req.UserAgent()), zap.String("remoteIp", c.RealIP()), zap.String("referer", req.Referer()), zap.String("protocol", req.Proto), zap.String("requestUrl", req.URL.String()), zap.String("requestSize", req.Header.Get(echo.HeaderContentLength)), - zap.String("responseSize", strconv.FormatInt(res.Size, 10)), zap.String("latency", latency), } - httpCode := res.Status + rw, uErr := echo.UnwrapResponse(c.Response()) + if uErr != nil { + logger := logging.GetLogger(req.Context()) + fields = append( + fields, + zap.Error(uErr), + zap.String("status", "unknown"), + zap.String("responseSize", "unknown")) + logger.Error("failed to unwrap response for access logging", fields...) + return nil + } + fields = append(fields, + zap.Int("status", rw.Status), + zap.String("responseSize", strconv.FormatInt(rw.Size, 10))) + logger := logging.GetLogger(req.Context()) + httpCode := rw.Status switch { case httpCode >= 500: fields = append(fields, zap.Error(err)) @@ -76,7 +88,7 @@ func (h Handlers) AccessLoggingMiddleware(next echo.HandlerFunc) echo.HandlerFun } func (h Handlers) CheckLoginMiddleware(next echo.HandlerFunc) echo.HandlerFunc { - return func(c echo.Context) error { + return func(c *echo.Context) error { ctx := c.Request().Context() logger := logging.GetLogger(ctx) @@ -99,7 +111,7 @@ func (h Handlers) CheckLoginMiddleware(next echo.HandlerFunc) echo.HandlerFunc { return echo.NewHTTPError(http.StatusUnauthorized, "you are not logged in") } logger.Error("failed to get user from repository", zap.Error(err)) - return echo.NewHTTPError(http.StatusInternalServerError, err) + return echo.ErrInternalServerError.Wrap(err) } c.Set(loginUserKey, userFromModelUser(*user)) @@ -108,7 +120,7 @@ func (h Handlers) CheckLoginMiddleware(next echo.HandlerFunc) echo.HandlerFunc { } func (h Handlers) CheckAccountManagerMiddleware(next echo.HandlerFunc) echo.HandlerFunc { - return func(c echo.Context) error { + return func(c *echo.Context) error { loginUser, _ := c.Get(loginUserKey).(User) if !loginUser.AccountManager { return echo.NewHTTPError(http.StatusForbidden, "you are not accountManager") diff --git a/internal/router/router.go b/internal/router/router.go index dcf9f457..63fcdcf8 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -6,9 +6,9 @@ import ( "github.com/google/uuid" "github.com/gorilla/sessions" - "github.com/labstack/echo-contrib/session" - "github.com/labstack/echo/v4" - "github.com/labstack/echo/v4/middleware" + "github.com/labstack/echo-contrib/v5/session" + "github.com/labstack/echo/v5" + "github.com/labstack/echo/v5/middleware" "go.uber.org/zap" "github.com/traPtitech/Jomon/internal/logging" @@ -24,14 +24,16 @@ type Handlers struct { SessionName string } +var defaultHTTPErrorHandler = echo.DefaultHTTPErrorHandler(false) + func (h Handlers) NewServer(logger *zap.Logger) *echo.Echo { e := echo.New() - e.Debug = os.Getenv("IS_DEBUG_MODE") != "" - e.HTTPErrorHandler = func(err error, c echo.Context) { + // TODO(logging): e.Loggerに与えられたloggerを適用する + e.HTTPErrorHandler = func(c *echo.Context, err error) { logger := logging.GetLogger(c.Request().Context()) logger.Debug("handling error", zap.Error(err)) he := HTTPErrorHandlerInner(err) - c.Echo().DefaultHTTPErrorHandler(he, c) + defaultHTTPErrorHandler(c, he) } e.Use(middleware.RequestID()) e.Use(h.setLoggerMiddleware(logger)) diff --git a/internal/router/tag.go b/internal/router/tag.go index 137f41a5..9545c577 100644 --- a/internal/router/tag.go +++ b/internal/router/tag.go @@ -5,7 +5,7 @@ import ( "time" "github.com/google/uuid" - "github.com/labstack/echo/v4" + "github.com/labstack/echo/v5" "github.com/samber/lo" "github.com/traPtitech/Jomon/internal/logging" "github.com/traPtitech/Jomon/internal/model" @@ -26,7 +26,7 @@ type TagResponse struct { UpdatedAt time.Time `json:"updated_at"` } -func (h Handlers) GetTags(c echo.Context) error { +func (h Handlers) GetTags(c *echo.Context) error { ctx := c.Request().Context() logger := logging.GetLogger(ctx) @@ -48,7 +48,7 @@ func (h Handlers) GetTags(c echo.Context) error { return c.JSON(http.StatusOK, res) } -func (h Handlers) PostTag(c echo.Context) error { +func (h Handlers) PostTag(c *echo.Context) error { ctx := c.Request().Context() logger := logging.GetLogger(ctx) @@ -75,7 +75,7 @@ func (h Handlers) PostTag(c echo.Context) error { return c.JSON(http.StatusOK, res) } -func (h Handlers) PutTag(c echo.Context) error { +func (h Handlers) PutTag(c *echo.Context) error { ctx := c.Request().Context() logger := logging.GetLogger(ctx) @@ -112,7 +112,7 @@ func (h Handlers) PutTag(c echo.Context) error { return c.JSON(http.StatusOK, res) } -func (h Handlers) DeleteTag(c echo.Context) error { +func (h Handlers) DeleteTag(c *echo.Context) error { ctx := c.Request().Context() logger := logging.GetLogger(ctx) diff --git a/internal/router/tag_test.go b/internal/router/tag_test.go index 02b215dc..6db612cb 100644 --- a/internal/router/tag_test.go +++ b/internal/router/tag_test.go @@ -11,7 +11,7 @@ import ( "time" "github.com/google/uuid" - "github.com/labstack/echo/v4" + "github.com/labstack/echo/v5" "github.com/samber/lo" "github.com/stretchr/testify/require" "github.com/traPtitech/Jomon/internal/model" @@ -251,8 +251,9 @@ func TestHandlers_PutTag(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/tags/:tagID") - c.SetParamNames("tagID") - c.SetParamValues(tag.ID.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "tagID", Value: tag.ID.String()}, + }) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) @@ -302,8 +303,9 @@ func TestHandlers_PutTag(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/tags/:tagID") - c.SetParamNames("tagID") - c.SetParamValues(tag.ID.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "tagID", Value: tag.ID.String()}, + }) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) @@ -345,8 +347,9 @@ func TestHandlers_PutTag(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/tags/:tagID") - c.SetParamNames("tagID") - c.SetParamValues(invalidUUID) + c.SetPathValues([]echo.PathValue{ + {Name: "tagID", Value: invalidUUID}, + }) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) @@ -381,8 +384,9 @@ func TestHandlers_PutTag(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/tags/:tagID") - c.SetParamNames("tagID") - c.SetParamValues(tag.ID.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "tagID", Value: tag.ID.String()}, + }) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) @@ -416,8 +420,9 @@ func TestHandlers_DeleteTag(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/tags/:tagID") - c.SetParamNames("tagID") - c.SetParamValues(tag.ID.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "tagID", Value: tag.ID.String()}, + }) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) @@ -451,8 +456,9 @@ func TestHandlers_DeleteTag(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/tags/:tagID") - c.SetParamNames("tagID") - c.SetParamValues(tag.ID.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "tagID", Value: tag.ID.String()}, + }) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) @@ -482,8 +488,9 @@ func TestHandlers_DeleteTag(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/tags/:tagID") - c.SetParamNames("tagID") - c.SetParamValues(invalidUUID) + c.SetPathValues([]echo.PathValue{ + {Name: "tagID", Value: invalidUUID}, + }) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) @@ -513,8 +520,9 @@ func TestHandlers_DeleteTag(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/api/tags/:tagID") - c.SetParamNames("tagID") - c.SetParamValues(tag.ID.String()) + c.SetPathValues([]echo.PathValue{ + {Name: "tagID", Value: tag.ID.String()}, + }) h, err := NewTestHandlers(t, ctrl) require.NoError(t, err) diff --git a/internal/router/user.go b/internal/router/user.go index 38f253e6..f7dbfa5b 100644 --- a/internal/router/user.go +++ b/internal/router/user.go @@ -5,7 +5,7 @@ import ( "time" "github.com/google/uuid" - "github.com/labstack/echo/v4" + "github.com/labstack/echo/v5" "github.com/samber/lo" "github.com/traPtitech/Jomon/internal/logging" "github.com/traPtitech/Jomon/internal/model" @@ -24,7 +24,7 @@ type User struct { DeletedAt nulltime.NullTime `json:"deleted_at"` } -func (h Handlers) GetUsers(c echo.Context) error { +func (h Handlers) GetUsers(c *echo.Context) error { ctx := c.Request().Context() logger := logging.GetLogger(ctx) @@ -55,7 +55,7 @@ type PutUserRequest struct { AccountManager bool `json:"account_manager"` } -func (h Handlers) UpdateUserInfo(c echo.Context) error { +func (h Handlers) UpdateUserInfo(c *echo.Context) error { ctx := c.Request().Context() logger := logging.GetLogger(ctx) @@ -99,7 +99,7 @@ func userFromModelUser(u model.User) User { } } -func (h Handlers) GetMe(c echo.Context) error { +func (h Handlers) GetMe(c *echo.Context) error { loginUser, _ := c.Get(loginUserKey).(User) return c.JSON(http.StatusOK, loginUser) } diff --git a/internal/router/user_test.go b/internal/router/user_test.go index 7d67c167..125b0c4f 100644 --- a/internal/router/user_test.go +++ b/internal/router/user_test.go @@ -10,7 +10,7 @@ import ( "time" "github.com/google/go-cmp/cmp/cmpopts" - "github.com/labstack/echo/v4" + "github.com/labstack/echo/v5" "github.com/samber/lo" "github.com/stretchr/testify/require" "github.com/traPtitech/Jomon/internal/model" diff --git a/internal/router/wrapsession/wrapper.go b/internal/router/wrapsession/wrapper.go index 03d1b080..34b78ffd 100644 --- a/internal/router/wrapsession/wrapper.go +++ b/internal/router/wrapsession/wrapper.go @@ -3,7 +3,7 @@ package wrapsession import ( "github.com/google/uuid" "github.com/gorilla/sessions" - "github.com/labstack/echo/v4" + "github.com/labstack/echo/v5" ) // NOTE: ここの値は外から変更する必要がないので非公開にしている @@ -49,7 +49,7 @@ func (w *W) setValue(key string, value interface{}) { w.changed = true } -func (w *W) drop(c echo.Context) error { +func (w *W) drop(c *echo.Context) error { if !w.changed { return nil } diff --git a/internal/router/wrapsession/wrapsession.go b/internal/router/wrapsession/wrapsession.go index d508ece0..70cfd8c1 100644 --- a/internal/router/wrapsession/wrapsession.go +++ b/internal/router/wrapsession/wrapsession.go @@ -2,7 +2,7 @@ // wrapsession は `*session.Session` のラッパーを提供します. // -// var c echo.Context, sessionName string +// var c *echo.Context, sessionName string // v, err := wrapsession.WithSession(c, sessionName, func (w *wrapsession.W) (T, error) { // return doSomething(w) // }) @@ -11,8 +11,8 @@ package wrapsession import ( - "github.com/labstack/echo-contrib/session" - "github.com/labstack/echo/v4" + "github.com/labstack/echo-contrib/v5/session" + "github.com/labstack/echo/v5" ) // `WithSession` はセッションを使用するスコープを提供します. @@ -44,7 +44,7 @@ import ( // エラーの型が区別されるため, type switchを用いて詳細なハンドリングが可能です. // // nolint:ireturn -func WithSession[T any](c echo.Context, sessionName string, op func(w *W) (T, error)) (T, error) { +func WithSession[T any](c *echo.Context, sessionName string, op func(w *W) (T, error)) (T, error) { var res T sess, err := session.Get(sessionName, c) if err != nil { diff --git a/internal/traq/traq.go b/internal/traq/traq.go index 5de333a1..dc2e4553 100644 --- a/internal/traq/traq.go +++ b/internal/traq/traq.go @@ -8,7 +8,7 @@ import ( "os" "strings" - "github.com/labstack/echo/v4" + "github.com/labstack/echo/v5" ) type Auth struct { diff --git a/internal/webhook/webhook.go b/internal/webhook/webhook.go index 3a65c84f..d3d8db95 100644 --- a/internal/webhook/webhook.go +++ b/internal/webhook/webhook.go @@ -16,7 +16,7 @@ import ( "time" "github.com/google/uuid" - "github.com/labstack/echo/v4" + "github.com/labstack/echo/v5" "github.com/samber/lo" ) @@ -98,7 +98,13 @@ func New(secret, channelID, webhookID string) *Service { } } -func (ws *Service) WebhookApplicationsEventHandler(c echo.Context, reqBody, resBody []byte) { +func (ws *Service) WebhookApplicationsEventHandler( + c *echo.Context, reqBody, resBody []byte, err error, +) { + if err != nil { + return + } + var message string if strings.Contains(c.Request().URL.Path, "/comments") {