You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2022/06/22 01:44:05 UTC

[apisix-go-plugin-runner] branch master updated: feat: add DefaultPlugin (#92)

This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-go-plugin-runner.git


The following commit(s) were added to refs/heads/master by this push:
     new ee55902  feat: add DefaultPlugin (#92)
ee55902 is described below

commit ee55902d87d8da5d50c4e765f95df3ec54105661
Author: soulbird <zh...@outlook.com>
AuthorDate: Wed Jun 22 09:43:59 2022 +0800

    feat: add DefaultPlugin (#92)
---
 cmd/go-runner/plugins/fault_injection.go  | 7 +++----
 cmd/go-runner/plugins/limit_req.go        | 7 +++----
 cmd/go-runner/plugins/response_rewrite.go | 7 +++----
 cmd/go-runner/plugins/say.go              | 7 +++----
 pkg/plugin/plugin.go                      | 6 ++++++
 5 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/cmd/go-runner/plugins/fault_injection.go b/cmd/go-runner/plugins/fault_injection.go
index eee3f0c..1d729f6 100644
--- a/cmd/go-runner/plugins/fault_injection.go
+++ b/cmd/go-runner/plugins/fault_injection.go
@@ -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 {
@@ -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) {
-
-}
diff --git a/cmd/go-runner/plugins/limit_req.go b/cmd/go-runner/plugins/limit_req.go
index 34c8c29..6632da3 100644
--- a/cmd/go-runner/plugins/limit_req.go
+++ b/cmd/go-runner/plugins/limit_req.go
@@ -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 {
@@ -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) {
-
-}
diff --git a/cmd/go-runner/plugins/response_rewrite.go b/cmd/go-runner/plugins/response_rewrite.go
index d14cec3..0af1ba8 100644
--- a/cmd/go-runner/plugins/response_rewrite.go
+++ b/cmd/go-runner/plugins/response_rewrite.go
@@ -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"
@@ -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 {
@@ -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 {
diff --git a/cmd/go-runner/plugins/say.go b/cmd/go-runner/plugins/say.go
index 49a41ea..e4b5540 100644
--- a/cmd/go-runner/plugins/say.go
+++ b/cmd/go-runner/plugins/say.go
@@ -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 {
@@ -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) {
-
-}
diff --git a/pkg/plugin/plugin.go b/pkg/plugin/plugin.go
index 14540bc..177240f 100644
--- a/pkg/plugin/plugin.go
+++ b/pkg/plugin/plugin.go
@@ -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)                    {}