Files
kannn 2cd3052da1
golangci-lint / lint (push) Failing after 1m5s
rebrand: frp -> kanhole (kanhole server, kanholec client)
2026-05-29 09:05:34 +00:00

42 lines
983 B
Go

package plugin
import (
"crypto/tls"
"encoding/json"
"io"
"net/http"
plugin "kanhole/pkg/plugin/server"
"kanhole/pkg/util/log"
"kanhole/test/e2e/mock/server/httpserver"
)
type Handler func(req *plugin.Request) *plugin.Response
type NewPluginRequest func() *plugin.Request
func NewHTTPPluginServer(port int, newFunc NewPluginRequest, handler Handler, tlsConfig *tls.Config) *httpserver.Server {
return httpserver.New(
httpserver.WithBindPort(port),
httpserver.WithTLSConfig(tlsConfig),
httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
r := newFunc()
buf, err := io.ReadAll(req.Body)
if err != nil {
w.WriteHeader(500)
return
}
log.Tracef("plugin request: %s", string(buf))
err = json.Unmarshal(buf, &r)
if err != nil {
w.WriteHeader(500)
return
}
resp := handler(r)
buf, _ = json.Marshal(resp)
log.Tracef("plugin response: %s", string(buf))
_, _ = w.Write(buf)
})),
)
}