diff --git a/handler/middleware.go b/handler/middleware.go index 02f8ee51..b7e0167f 100644 --- a/handler/middleware.go +++ b/handler/middleware.go @@ -41,7 +41,7 @@ const ( var adminUserIDs = []string{"ryoha", "xxarupakaxx", "kaitoyama", "cp20", "itzmeowww"} // SetUserIDMiddleware X-Showcase-UserからユーザーIDを取得しセットする -func SetUserIDMiddleware(next echo.HandlerFunc) echo.HandlerFunc { +func (*Middleware) SetUserIDMiddleware(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { userID := c.Request().Header.Get("X-Showcase-User") if userID == "" { @@ -55,7 +55,7 @@ func SetUserIDMiddleware(next echo.HandlerFunc) echo.HandlerFunc { } // TraPMemberAuthenticate traP部員かの認証 -func TraPMemberAuthenticate(next echo.HandlerFunc) echo.HandlerFunc { +func (*Middleware) TraPMemberAuthenticate(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { userID, err := getUserID(c) if err != nil { @@ -74,7 +74,7 @@ func TraPMemberAuthenticate(next echo.HandlerFunc) echo.HandlerFunc { } // TrapRateLimitMiddlewareFunc traP IDベースのリクエスト制限 -func TrapRateLimitMiddlewareFunc() echo.MiddlewareFunc { +func (*Middleware) TrapRateLimitMiddlewareFunc() echo.MiddlewareFunc { config := middleware.RateLimiterConfig{ Store: middleware.NewRateLimiterMemoryStore(5), IdentifierExtractor: func(c echo.Context) (string, error) { @@ -92,9 +92,8 @@ func TrapRateLimitMiddlewareFunc() echo.MiddlewareFunc { } // QuestionnaireReadAuthenticate アンケートの閲覧権限があるかの認証 -func QuestionnaireReadAuthenticate(next echo.HandlerFunc) echo.HandlerFunc { +func (m *Middleware) QuestionnaireReadAuthenticate(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { - m := NewMiddleware() userID, err := getUserID(c) if err != nil { @@ -148,9 +147,8 @@ func QuestionnaireReadAuthenticate(next echo.HandlerFunc) echo.HandlerFunc { } // QuestionnaireAdministratorAuthenticate アンケートの管理者かどうかの認証 -func QuestionnaireAdministratorAuthenticate(next echo.HandlerFunc) echo.HandlerFunc { +func (m *Middleware) QuestionnaireAdministratorAuthenticate(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { - m := NewMiddleware() userID, err := getUserID(c) if err != nil { @@ -188,9 +186,8 @@ func QuestionnaireAdministratorAuthenticate(next echo.HandlerFunc) echo.HandlerF } // ResponseReadAuthenticate 回答閲覧権限があるかの認証 -func ResponseReadAuthenticate(next echo.HandlerFunc) echo.HandlerFunc { +func (m *Middleware) ResponseReadAuthenticate(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { - m := NewMiddleware() userID, err := getUserID(c) if err != nil { @@ -255,9 +252,8 @@ func ResponseReadAuthenticate(next echo.HandlerFunc) echo.HandlerFunc { } // RespondentAuthenticate 回答者かどうかの認証 -func RespondentAuthenticate(next echo.HandlerFunc) echo.HandlerFunc { +func (m *Middleware) RespondentAuthenticate(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { - m := NewMiddleware() userID, err := getUserID(c) if err != nil { diff --git a/main.go b/main.go index 3778b60b..8668f1ef 100644 --- a/main.go +++ b/main.go @@ -61,37 +61,38 @@ func main() { controller.Wg.Add(1) go func() { e := echo.New() - swagger, err := openapi.GetSwagger() - if err != nil { - panic(err) - } - e.Use(oapiMiddleware.OapiRequestValidator(swagger)) - e.Use(handler.SetUserIDMiddleware) - e.Use(middleware.Logger()) - e.Use(middleware.Recover()) - - mws := NewMiddlewareSwitcher() - mws.AddGroupConfig("", handler.TraPMemberAuthenticate) - - mws.AddRouteConfig("/questionnaires", http.MethodGet, handler.TrapRateLimitMiddlewareFunc()) - mws.AddRouteConfig("/questionnaires/:questionnaireID", http.MethodGet, handler.QuestionnaireReadAuthenticate) - mws.AddRouteConfig("/questionnaires/:questionnaireID", http.MethodPatch, handler.QuestionnaireAdministratorAuthenticate) - mws.AddRouteConfig("/questionnaires/:questionnaireID", http.MethodDelete, handler.QuestionnaireAdministratorAuthenticate) - - mws.AddRouteConfig("/responses/:responseID", http.MethodGet, handler.ResponseReadAuthenticate) - mws.AddRouteConfig("/responses/:responseID", http.MethodPatch, handler.RespondentAuthenticate) - mws.AddRouteConfig("/responses/:responseID", http.MethodDelete, handler.RespondentAuthenticate) - - openapi.RegisterHandlers(e, handler.Handler{}) - - e.Use(mws.ApplyMiddlewares) - e.Logger.Fatal(e.Start(port)) - + swagger, err := openapi.GetSwagger() + if err != nil { + panic(err) + } + api := InjectAPIServer() + e.Use(oapiMiddleware.OapiRequestValidator(swagger)) + e.Use(api.SetUserIDMiddleware) + e.Use(middleware.Logger()) + e.Use(middleware.Recover()) + + mws := NewMiddlewareSwitcher() + mws.AddGroupConfig("", api.TraPMemberAuthenticate) + + mws.AddRouteConfig("/questionnaires", http.MethodGet, api.TrapRateLimitMiddlewareFunc()) + mws.AddRouteConfig("/questionnaires/:questionnaireID", http.MethodGet, api.QuestionnaireReadAuthenticate) + mws.AddRouteConfig("/questionnaires/:questionnaireID", http.MethodPatch, api.QuestionnaireAdministratorAuthenticate) + mws.AddRouteConfig("/questionnaires/:questionnaireID", http.MethodDelete, api.QuestionnaireAdministratorAuthenticate) + + mws.AddRouteConfig("/responses/:responseID", http.MethodGet, api.ResponseReadAuthenticate) + mws.AddRouteConfig("/responses/:responseID", http.MethodPatch, api.RespondentAuthenticate) + mws.AddRouteConfig("/responses/:responseID", http.MethodDelete, api.RespondentAuthenticate) + + openapi.RegisterHandlers(e, handler.Handler{}) + + e.Use(mws.ApplyMiddlewares) + e.Logger.Fatal(e.Start(port)) + controller.Wg.Done() }() controller.Wg.Add(1) - go func () { + go func() { controller.ReminderInit() controller.Wg.Done() }() diff --git a/router.go b/router.go index 389a4d19..ab4860d6 100644 --- a/router.go +++ b/router.go @@ -1,83 +1,83 @@ package main -import ( - "github.com/labstack/echo-contrib/prometheus" - "github.com/labstack/echo/v4" - "github.com/labstack/echo/v4/middleware" -) +// import ( +// "github.com/labstack/echo-contrib/prometheus" +// "github.com/labstack/echo/v4" +// "github.com/labstack/echo/v4/middleware" +// ) -// SetRouting ルーティングの設定 -func SetRouting(port string) { - e := echo.New() +// // SetRouting ルーティングの設定 +// func SetRouting(port string) { +// e := echo.New() - // Middleware - e.Use(middleware.Recover()) - e.Use(middleware.Logger()) - p := prometheus.NewPrometheus("echo", nil) - p.Use(e) +// // Middleware +// e.Use(middleware.Recover()) +// e.Use(middleware.Logger()) +// p := prometheus.NewPrometheus("echo", nil) +// p.Use(e) - api := InjectAPIServer() +// api := InjectAPIServer() - // Static Files - e.Static("/", "client/dist") - e.Static("/js", "client/dist/js") - e.Static("/img", "client/dist/img") - e.Static("/fonts", "client/dist/fonts") - e.Static("/css", "client/dist/css") +// // Static Files +// e.Static("/", "client/dist") +// e.Static("/js", "client/dist/js") +// e.Static("/img", "client/dist/img") +// e.Static("/fonts", "client/dist/fonts") +// e.Static("/css", "client/dist/css") - e.File("/app.js", "client/dist/app.js") - e.File("/favicon.ico", "client/dist/favicon.ico") - e.File("*", "client/dist/index.html") +// e.File("/app.js", "client/dist/app.js") +// e.File("/favicon.ico", "client/dist/favicon.ico") +// e.File("*", "client/dist/index.html") - echoAPI := e.Group("/api", api.SetValidatorMiddleware, api.SetUserIDMiddleware, api.TraPMemberAuthenticate) - { - apiQuestionnnaires := echoAPI.Group("/questionnaires") - { - apiQuestionnnaires.GET("", api.GetQuestionnaires, api.TrapRateLimitMiddlewareFunc()) - apiQuestionnnaires.POST("", api.PostQuestionnaire) - apiQuestionnnaires.GET("/:questionnaireID", api.GetQuestionnaire) - apiQuestionnnaires.PATCH("/:questionnaireID", api.EditQuestionnaire, api.QuestionnaireAdministratorAuthenticate) - apiQuestionnnaires.DELETE("/:questionnaireID", api.DeleteQuestionnaire, api.QuestionnaireAdministratorAuthenticate) - apiQuestionnnaires.GET("/:questionnaireID/questions", api.GetQuestions) - apiQuestionnnaires.POST("/:questionnaireID/questions", api.PostQuestionByQuestionnaireID) - } +// echoAPI := e.Group("/api", api.SetValidatorMiddleware, api.SetUserIDMiddleware, api.TraPMemberAuthenticate) +// { +// apiQuestionnnaires := echoAPI.Group("/questionnaires") +// { +// apiQuestionnnaires.GET("", api.GetQuestionnaires, api.TrapRateLimitMiddlewareFunc()) +// apiQuestionnnaires.POST("", api.PostQuestionnaire) +// apiQuestionnnaires.GET("/:questionnaireID", api.GetQuestionnaire) +// apiQuestionnnaires.PATCH("/:questionnaireID", api.EditQuestionnaire, api.QuestionnaireAdministratorAuthenticate) +// apiQuestionnnaires.DELETE("/:questionnaireID", api.DeleteQuestionnaire, api.QuestionnaireAdministratorAuthenticate) +// apiQuestionnnaires.GET("/:questionnaireID/questions", api.GetQuestions) +// apiQuestionnnaires.POST("/:questionnaireID/questions", api.PostQuestionByQuestionnaireID) +// } - apiQuestions := echoAPI.Group("/questions") - { - apiQuestions.PATCH("/:questionID", api.EditQuestion, api.QuestionAdministratorAuthenticate) - apiQuestions.DELETE("/:questionID", api.DeleteQuestion, api.QuestionAdministratorAuthenticate) - } +// apiQuestions := echoAPI.Group("/questions") +// { +// apiQuestions.PATCH("/:questionID", api.EditQuestion, api.QuestionAdministratorAuthenticate) +// apiQuestions.DELETE("/:questionID", api.DeleteQuestion, api.QuestionAdministratorAuthenticate) +// } - apiResponses := echoAPI.Group("/responses") - { - apiResponses.POST("", api.PostResponse) - apiResponses.GET("/:responseID", api.GetResponse, api.ResponseReadAuthenticate) - apiResponses.PATCH("/:responseID", api.EditResponse, api.RespondentAuthenticate) - apiResponses.DELETE("/:responseID", api.DeleteResponse, api.RespondentAuthenticate) - } +// apiResponses := echoAPI.Group("/responses") +// { +// apiResponses.POST("", api.PostResponse) +// apiResponses.GET("/:responseID", api.GetResponse, api.ResponseReadAuthenticate) +// apiResponses.PATCH("/:responseID", api.EditResponse, api.RespondentAuthenticate) +// apiResponses.DELETE("/:responseID", api.DeleteResponse, api.RespondentAuthenticate) +// } - apiUsers := echoAPI.Group("/users") - { - /* - TODO - apiUsers.GET("") - */ - apiUsersMe := apiUsers.Group("/me") - { - apiUsersMe.GET("", api.GetUsersMe) - apiUsersMe.GET("/responses", api.GetMyResponses) - apiUsersMe.GET("/responses/:questionnaireID", api.GetMyResponsesByID) - apiUsersMe.GET("/targeted", api.GetTargetedQuestionnaire) - apiUsersMe.GET("/administrates", api.GetMyQuestionnaire) - } - apiUsers.GET("/:traQID/targeted", api.GetTargettedQuestionnairesBytraQID) - } +// apiUsers := echoAPI.Group("/users") +// { +// /* +// TODO +// apiUsers.GET("") +// */ +// apiUsersMe := apiUsers.Group("/me") +// { +// apiUsersMe.GET("", api.GetUsersMe) +// apiUsersMe.GET("/responses", api.GetMyResponses) +// apiUsersMe.GET("/responses/:questionnaireID", api.GetMyResponsesByID) +// apiUsersMe.GET("/targeted", api.GetTargetedQuestionnaire) +// apiUsersMe.GET("/administrates", api.GetMyQuestionnaire) +// } +// apiUsers.GET("/:traQID/targeted", api.GetTargettedQuestionnairesBytraQID) +// } - apiResults := echoAPI.Group("/results") - { - apiResults.GET("/:questionnaireID", api.GetResults, api.ResultAuthenticate) - } - } +// apiResults := echoAPI.Group("/results") +// { +// apiResults.GET("/:questionnaireID", api.GetResults, api.ResultAuthenticate) +// } +// } - e.Logger.Fatal(e.Start(port)) -} +// e.Logger.Fatal(e.Start(port)) +// } diff --git a/wire.go b/wire.go index c32e951f..9a35d84a 100644 --- a/wire.go +++ b/wire.go @@ -5,8 +5,9 @@ package main import ( "github.com/google/wire" + "github.com/traPtitech/anke-to/controller" + "github.com/traPtitech/anke-to/handler" "github.com/traPtitech/anke-to/model" - "github.com/traPtitech/anke-to/router" "github.com/traPtitech/anke-to/traq" ) @@ -25,15 +26,11 @@ var ( webhookBind = wire.Bind(new(traq.IWebhook), new(*traq.Webhook)) ) -func InjectAPIServer() *router.API { +func InjectAPIServer() *handler.Middleware { wire.Build( - router.NewAPI, - router.NewMiddleware, - router.NewQuestionnaire, - router.NewQuestion, - router.NewResponse, - router.NewResult, - router.NewUser, + handler.NewMiddleware, + controller.NewResponse, + controller.NewQuestionnaire, model.NewAdministrator, model.NewOption, model.NewQuestionnaire,