Introduction to the Pixiu Filter System

Introduction to the Pixiu Filter System

How to Write a Filter

For more detailed information, please refer to the Blog "Talking About Pixiu's Filter"

Let’s try writing a simple Filter that will have a basic configuration, log the request Body during the Decode phase, and return the reversed string as a Mock response. Finally, in the Encode phase, it will log the return value based on the configuration.

  1. First, create a Filter
  1. type DemoFilter struct {
  2. logPrefix string
  3. }
  4. // Decode phase, occurs before calling Upstream
  5. func (f *DemoFilter) Decode(ctx *contexthttp.HttpContext) filter.FilterStatus {
  6. body, _ := ioutil.ReadAll(ctx.Request.Body)
  7. logger.Infof("request body: %s", body)
  8. //reverse res str
  9. runes := []rune(string(body))
  10. for i := 0; i < len(runes)/2; i += 1 {
  11. runes[i], runes[len(runes)-1-i] = runes[len(runes)-1-i], runes[i]
  12. }
  13. reverse := string(runes)
  14. //mock response
  15. ctx.SendLocalReply(200, []byte(reverse))
  16. return filter.Stop
  17. }
  18. // Encode phase, at this point the Upstream Response can be accessed
  19. func (f *DemoFilter) Encode(ctx *contexthttp.HttpContext) filter.FilterStatus {
  20. res := ctx.SourceResp.(string)
  21. logger.Infof("%s: %s", f.logPrefix, res)
  22. return filter.Continue
  23. }
  1. Create a Filter Factory
  1. type (
  2. DemoFilterFactory struct {
  3. conf *Config
  4. }
  5. // Config describe the config of Filter
  6. Config struct {
  7. LogPrefix string `yaml:"logPrefix,omitempty"`
  8. }
  9. )
  10. func (f *DemoFilterFactory) PrepareFilterChain(ctx *contexthttp.HttpContext, chain filter.FilterChain) error {
  11. demo := &DemoFilter{logPrefix: f.conf.LogPrefix}
  12. chain.AppendDecodeFilters(demo)
  13. chain.AppendEncodeFilters(demo)
  14. return nil
  15. }
  16. func (f *DemoFilterFactory) Config() interface{} {
  17. return f.conf
  18. }
  19. func (f *DemoFilterFactory) Apply() error {
  20. return nil
  21. }
  1. Create a Filter Plugin and register it
  1. //important
  2. func init() {
  3. filter.RegisterHttpFilter(&Plugin{})
  4. }
  5. type Plugin struct {
  6. }
  7. func (p *Plugin) Kind() string {
  8. return "dgp.filters.demo"
  9. }
  10. func (p *Plugin) CreateFilterFactory() (filter.HttpFilterFactory, error) {
  11. return &DemoFilterFactory{conf: &Config{}}, nil
  12. }
  1. Configure this Filter in the configuration file and start Pixiu
  1. static_resources:
  2. listeners:
  3. - name: "net/http"
  4. protocol_type: "HTTP"
  5. address:
  6. socket_address:
  7. address: "0.0.0.0"
  8. port: 8888
  9. filter_chains:
  10. filters:
  11. - name: dgp.filter.httpconnectionmanager
  12. config:
  13. route_config:
  14. routes:
  15. - match:
  16. prefix: "/"
  17. http_filters:
  18. - name: dgp.filters.demo
  19. config:
  1. Access and check the logs and results
  1. curl localhost:8888/demo -d "eiv al tse’c"
  2. cest la vie%

Logs

  1. 2022-02-19T20:20:11.900+0800 INFO demo/demo.go:62 request body: eiv al tsec
  2. 2022-02-19T20:20:11.900+0800 INFO demo/demo.go:71 : eiv al tsec

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)