mirror of
https://github.com/openziti/zrok.git
synced 2025-08-16 19:01:16 +02:00
integrate tui requests handler (#392)
This commit is contained in:
73
endpoints/proxy/caddyRequests.go
Normal file
73
endpoints/proxy/caddyRequests.go
Normal file
@ -0,0 +1,73 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/openziti/zrok/endpoints"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
)
|
||||
|
||||
var middlewareRequests chan *endpoints.Request
|
||||
|
||||
func init() {
|
||||
caddy.RegisterModule(ZrokRequestsMiddleware{})
|
||||
httpcaddyfile.RegisterHandlerDirective("zrok_requests", parseCaddyfile)
|
||||
}
|
||||
|
||||
type ZrokRequestsMiddleware struct{}
|
||||
|
||||
// CaddyModule returns the Caddy module information.
|
||||
func (ZrokRequestsMiddleware) CaddyModule() caddy.ModuleInfo {
|
||||
return caddy.ModuleInfo{
|
||||
ID: "http.handlers.zrok_requests",
|
||||
New: func() caddy.Module { return new(ZrokRequestsMiddleware) },
|
||||
}
|
||||
}
|
||||
|
||||
// Provision implements caddy.Provisioner.
|
||||
func (m *ZrokRequestsMiddleware) Provision(ctx caddy.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate implements caddy.Validator.
|
||||
func (m ZrokRequestsMiddleware) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ServeHTTP implements caddyhttp.MiddlewareHandler.
|
||||
func (m ZrokRequestsMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
||||
if middlewareRequests != nil {
|
||||
middlewareRequests <- &endpoints.Request{
|
||||
Stamp: time.Now(),
|
||||
RemoteAddr: fmt.Sprintf("%v", r.Header["X-Real-Ip"]),
|
||||
Method: r.Method,
|
||||
Path: r.URL.String(),
|
||||
}
|
||||
}
|
||||
return next.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
|
||||
func (m *ZrokRequestsMiddleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseCaddyfile unmarshals tokens from h into a new ZrokRequestsMiddleware.
|
||||
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||
var m ZrokRequestsMiddleware
|
||||
err := m.UnmarshalCaddyfile(h.Dispenser)
|
||||
return m, err
|
||||
}
|
||||
|
||||
// Interface guards
|
||||
var (
|
||||
_ caddy.Provisioner = (*ZrokRequestsMiddleware)(nil)
|
||||
_ caddy.Validator = (*ZrokRequestsMiddleware)(nil)
|
||||
_ caddyhttp.MiddlewareHandler = (*ZrokRequestsMiddleware)(nil)
|
||||
_ caddyfile.Unmarshaler = (*ZrokRequestsMiddleware)(nil)
|
||||
)
|
@ -8,6 +8,7 @@ import (
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver"
|
||||
"github.com/openziti/zrok/endpoints"
|
||||
"go.uber.org/zap"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -28,6 +29,8 @@ func NewCaddyWebBackend(cfg *CaddyWebBackendConfig) (*CaddyWebBackend, error) {
|
||||
handler.Browse = new(fileserver.Browse)
|
||||
|
||||
var handlers []json.RawMessage
|
||||
middlewareRequests = cfg.Requests
|
||||
handlers = append(handlers, caddyconfig.JSONModuleObject(&ZrokRequestsMiddleware{}, "handler", "zrok_requests", nil))
|
||||
handlers = append(handlers, caddyconfig.JSONModuleObject(handler, "handler", "file_server", nil))
|
||||
|
||||
route := caddyhttp.Route{HandlersRaw: handlers}
|
||||
@ -55,6 +58,15 @@ func NewCaddyWebBackend(cfg *CaddyWebBackendConfig) (*CaddyWebBackend, error) {
|
||||
AppsRaw: caddy.ModuleMap{
|
||||
"http": caddyconfig.JSON(httpApp, nil),
|
||||
},
|
||||
Logging: &caddy.Logging{
|
||||
Logs: map[string]*caddy.CustomLog{
|
||||
"default": {
|
||||
BaseLog: caddy.BaseLog{
|
||||
Level: zap.ErrorLevel.CapitalString(),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return &CaddyWebBackend{cfg: cfg, caddyCfg: caddyCfg}, nil
|
||||
|
Reference in New Issue
Block a user