Skip to content

Commit

Permalink
wip introduce wire
Browse files Browse the repository at this point in the history
  • Loading branch information
kaitoyama committed Dec 6, 2024
1 parent 02243ee commit 55a0dc5
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 117 deletions.
18 changes: 7 additions & 11 deletions handler/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
55 changes: 28 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}()
Expand Down
140 changes: 70 additions & 70 deletions router.go
Original file line number Diff line number Diff line change
@@ -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))
// }
15 changes: 6 additions & 9 deletions wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
Expand Down

0 comments on commit 55a0dc5

Please sign in to comment.