Skip to content

Commit

Permalink
feat: add custom middlewares for init plugins (#167)
Browse files Browse the repository at this point in the history
* Add custom middlewares

Signed-off-by: zhuojie <[email protected]>

* Update api/server.go

Co-authored-by: Romain Beuque <[email protected]>
Signed-off-by: zhuojie <[email protected]>

* Update api/server.go

Co-authored-by: Romain Beuque <[email protected]>
Signed-off-by: zhuojie <[email protected]>

Co-authored-by: Romain Beuque <[email protected]>
  • Loading branch information
zhouzhuojie and rbeuque74 authored Jul 24, 2020
1 parent 70cbec9 commit a596736
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func TestMain(m *testing.M) {

srv := api.NewServer()
srv.WithAuth(dumbIdentityProvider)
srv.WithCustomMiddlewares(dummyCustomMiddleware)
srv.SetDashboardPathPrefix("")
srv.SetDashboardAPIPathPrefix("")
srv.SetDashboardSentryDSN("")
Expand All @@ -87,6 +88,10 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}

func dummyCustomMiddleware(c *gin.Context) {
c.Next()
}

func dumbIdentityProvider(r *http.Request) (string, error) {
username := r.Header.Get(usernameHeaderKey)

Expand Down
8 changes: 8 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Server struct {
dashboardSentryDSN string
editorPathPrefix string
maxBodyBytes int64
customMiddlewares []gin.HandlerFunc
}

// NewServer returns a new Server
Expand All @@ -54,6 +55,12 @@ func (s *Server) WithAuth(authProvider func(*http.Request) (string, error)) {
}
}

// WithCustomMiddlewares sets an array of customized gin middlewares.
// It helps for init plugins to include these customized middlewares in the api server
func (s *Server) WithCustomMiddlewares(customMiddlewares ...gin.HandlerFunc) {
s.customMiddlewares = append(s.customMiddlewares, customMiddlewares...)
}

// SetDashboardPathPrefix configures the custom path prefix for dashboard static files hosting.
// It doesn't change the path used by utask API to serve the files, it's only used inside UI files
// in order that dashboard can be aware of a ProxyPass configuration.
Expand Down Expand Up @@ -178,6 +185,7 @@ func (s *Server) build(ctx context.Context) {
},
})

router.Use(s.customMiddlewares...)
router.Use(ajaxHeadersMiddleware, errorLogMiddleware)

tonic.SetErrorHook(jujerr.ErrHook)
Expand Down

0 comments on commit a596736

Please sign in to comment.