Skip to content

Commit

Permalink
feat: add DefaultPlugin (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
soulbird committed Jun 22, 2022
1 parent d283ba2 commit ee55902
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
7 changes: 3 additions & 4 deletions cmd/go-runner/plugins/fault_injection.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func init() {

// FaultInjection is used in the benchmark
type FaultInjection struct {
// Embed the default plugin here,
// so that we don't need to reimplement all the methods.
plugin.DefaultPlugin
}

type FaultInjectionConf struct {
Expand Down Expand Up @@ -94,7 +97,3 @@ func (p *FaultInjection) RequestFilter(conf interface{}, w http.ResponseWriter,
log.Errorf("failed to write: %s", err)
}
}

func (p *FaultInjection) ResponseFilter(interface{}, pkgHTTP.Response) {

}
7 changes: 3 additions & 4 deletions cmd/go-runner/plugins/limit_req.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func init() {

// LimitReq is a demo for a real world plugin
type LimitReq struct {
// Embed the default plugin here,
// so that we don't need to reimplement all the methods.
plugin.DefaultPlugin
}

type LimitReqConf struct {
Expand Down Expand Up @@ -75,7 +78,3 @@ func (p *LimitReq) RequestFilter(conf interface{}, w http.ResponseWriter, r pkgH
}
time.Sleep(rs.Delay())
}

func (p *LimitReq) ResponseFilter(interface{}, pkgHTTP.Response) {

}
7 changes: 3 additions & 4 deletions cmd/go-runner/plugins/response_rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package plugins

import (
"encoding/json"
"net/http"

pkgHTTP "github.com/apache/apisix-go-plugin-runner/pkg/http"
"github.com/apache/apisix-go-plugin-runner/pkg/log"
Expand All @@ -35,6 +34,9 @@ func init() {

// ResponseRewrite is a demo to show how to rewrite response data.
type ResponseRewrite struct {
// Embed the default plugin here,
// so that we don't need to reimplement all the methods.
plugin.DefaultPlugin
}

type ResponseRewriteConf struct {
Expand All @@ -53,9 +55,6 @@ func (p *ResponseRewrite) ParseConf(in []byte) (interface{}, error) {
return conf, err
}

func (p *ResponseRewrite) RequestFilter(interface{}, http.ResponseWriter, pkgHTTP.Request) {
}

func (p *ResponseRewrite) ResponseFilter(conf interface{}, w pkgHTTP.Response) {
cfg := conf.(ResponseRewriteConf)
if cfg.Status > 0 {
Expand Down
7 changes: 3 additions & 4 deletions cmd/go-runner/plugins/say.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func init() {
// Say is a demo to show how to return data directly instead of proxying
// it to the upstream.
type Say struct {
// Embed the default plugin here,
// so that we don't need to reimplement all the methods.
plugin.DefaultPlugin
}

type SayConf struct {
Expand Down Expand Up @@ -64,7 +67,3 @@ func (p *Say) RequestFilter(conf interface{}, w http.ResponseWriter, r pkgHTTP.R
log.Errorf("failed to write: %s", err)
}
}

func (p *Say) ResponseFilter(interface{}, pkgHTTP.Response) {

}
6 changes: 6 additions & 0 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@ type Plugin interface {
func RegisterPlugin(p Plugin) error {
return plugin.RegisterPlugin(p.Name(), p.ParseConf, p.RequestFilter, p.ResponseFilter)
}

// DefaultPlugin provides the no-op implementation of the Plugin interface.
type DefaultPlugin struct{}

func (*DefaultPlugin) RequestFilter(interface{}, http.ResponseWriter, pkgHTTP.Request) {}
func (*DefaultPlugin) ResponseFilter(interface{}, pkgHTTP.Response) {}

0 comments on commit ee55902

Please sign in to comment.