Skip to content

Commit

Permalink
Add middlewares for cozy filtering and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgenyOvsov authored and Evgeny Osenchugov committed Aug 31, 2024
1 parent 4a09150 commit a9fc8aa
Show file tree
Hide file tree
Showing 4 changed files with 539 additions and 166 deletions.
9 changes: 9 additions & 0 deletions handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,25 @@ func (sf *Server) handleRequest(write io.Writer, req *Request) error {
if sf.userConnectHandle != nil {
return sf.userConnectHandle(ctx, write, req)
}
if len(sf.userConnectMiddlewares) != 0 {
return sf.userConnectMiddlewares.Execute(ctx, write, req, sf.handleConnect)
}
return sf.handleConnect(ctx, write, req)
case statute.CommandBind:
if sf.userBindHandle != nil {
return sf.userBindHandle(ctx, write, req)
}
if len(sf.userBindMiddlewares) != 0 {
return sf.userBindMiddlewares.Execute(ctx, write, req, sf.handleBind)
}
return sf.handleBind(ctx, write, req)
case statute.CommandAssociate:
if sf.userAssociateHandle != nil {
return sf.userAssociateHandle(ctx, write, req)
}
if len(sf.userAssociateMiddlewares) != 0 {
return sf.userAssociateMiddlewares.Execute(ctx, write, req, sf.handleAssociate)
}
return sf.handleAssociate(ctx, write, req)
default:
if err := SendReply(write, statute.RepCommandNotSupported, nil); err != nil {
Expand Down
43 changes: 43 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,46 @@ func WithAssociateHandle(h func(ctx context.Context, writer io.Writer, request *
s.userAssociateHandle = h
}
}

// Handler is used to handle a user's commands
type Handler func(ctx context.Context, writer io.Writer, request *Request) error

// WithMiddleware is used to add interceptors in chain
type Middleware func(ctx context.Context, writer io.Writer, request *Request) error

// MiddlewareChain is used to add interceptors in chain
type MiddlewareChain []Middleware

// Execute is used to add interceptors in chain
func (m MiddlewareChain) Execute(ctx context.Context, writer io.Writer, request *Request, last Handler) error {
if len(m) == 0 {
return nil
}
for i := 0; i < len(m); i++ {
if err := m[i](ctx, writer, request); err != nil {
return err
}
}
return last(ctx, writer, request)
}

// WithConnectMiddleware is used to add interceptors in chain
func WithConnectMiddleware(m Middleware) Option {
return func(s *Server) {
s.userConnectMiddlewares = append(s.userConnectMiddlewares, m)
}
}

// WithBindMiddleware is used to add interceptors in chain
func WithBindMiddleware(m Middleware) Option {
return func(s *Server) {
s.userBindMiddlewares = append(s.userBindMiddlewares, m)
}
}

// WithAssociateMiddleware is used to add interceptors in chain
func WithAssociateMiddleware(m Middleware) Option {
return func(s *Server) {
s.userAssociateMiddlewares = append(s.userAssociateMiddlewares, m)
}
}
4 changes: 4 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ type Server struct {
userConnectHandle func(ctx context.Context, writer io.Writer, request *Request) error
userBindHandle func(ctx context.Context, writer io.Writer, request *Request) error
userAssociateHandle func(ctx context.Context, writer io.Writer, request *Request) error
// user's middleware
userConnectMiddlewares MiddlewareChain
userBindMiddlewares MiddlewareChain
userAssociateMiddlewares MiddlewareChain
}

// NewServer creates a new Server
Expand Down
Loading

0 comments on commit a9fc8aa

Please sign in to comment.