reproxy/examples/plugin/main.go
Umputun 7139c57766
RPC plugins support (#85)
* wip

* resolve merge artifacts

* full coverage for conductor

* wire plugin conductor to main and proxy

* wip, with separate match handler

* split matching logic with another handler, add initial docs

* move parts of proxy to handlers, add tests

* add headers in to be sent to proxied url

* merged from master

* add example with docker compose

* supress excesive debug reporting 0-9 disabled in docker

* add plugin tests

* randomize test port

* lint: minor warns

* lint: err shadow
2021-06-01 02:56:39 -05:00

51 lines
1.4 KiB
Go

package main
import (
"context"
"log"
"net/http"
"github.com/umputun/reproxy/lib"
)
func main() {
// create demo plugin on port 1234 with two methods: HeaderThing and ErrorThing
// both called via RPC from reproxy core with fully formed lib.Request
plugin := lib.Plugin{
Name: "TestPlugin",
Address: "plugin-example:1234",
Methods: []string{"HeaderThing", "ErrorThing"},
}
log.Printf("start demo plugin")
// Do starts the plugin listener and register with reproxy plugin conductor
if err := plugin.Do(context.TODO(), "http://reproxy:8081", new(Handler)); err != nil {
log.Fatal(err)
}
}
// Handler is an example of middleware handler altering headers and stastus
type Handler struct{}
// HeaderThing adds key:val header to the response
func (h *Handler) HeaderThing(req lib.Request, res *lib.Response) (err error) {
log.Printf("req: %+v", req)
res.HeadersOut = http.Header{}
res.HeadersOut.Add("key", "val")
res.HeadersIn = http.Header{}
res.HeadersIn.Add("token", "something")
res.StatusCode = 200 // each handler has to set status code
return
}
// ErrorThing returns status 500 on "/fail" url. This terminated processing chain on reproxy side immediately
func (h *Handler) ErrorThing(req lib.Request, res *lib.Response) (err error) {
log.Printf("req: %+v", req)
if req.URL == "/fail" {
res.StatusCode = 500
return
}
res.StatusCode = 200
return
}