summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2021-04-28 14:10:27 +0300
committerValery Piashchynski <[email protected]>2021-04-28 14:10:27 +0300
commit30c25f17fa7d6386e33a4894c812f7ca5db990ad (patch)
tree03f53535addf71a81eca4b9a1d3ba29d4ebf4984 /plugins
parent4cb2247f909d02c922edb6f8e3d3741cc5a2c077 (diff)
- Fix middleware order
- Update tests - Move worker handler into a separate folder with separate package name Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'plugins')
-rw-r--r--plugins/gzip/plugin.go6
-rw-r--r--plugins/headers/plugin.go6
-rw-r--r--plugins/http/plugin.go27
-rw-r--r--plugins/http/static/static.go (renamed from plugins/http/static.go)6
-rw-r--r--plugins/http/worker_handler/constants.go (renamed from plugins/http/constants.go)2
-rw-r--r--plugins/http/worker_handler/errors.go (renamed from plugins/http/errors.go)2
-rw-r--r--plugins/http/worker_handler/errors_windows.go (renamed from plugins/http/errors_windows.go)2
-rw-r--r--plugins/http/worker_handler/handler.go (renamed from plugins/http/handler.go)2
-rw-r--r--plugins/http/worker_handler/parse.go (renamed from plugins/http/parse.go)2
-rw-r--r--plugins/http/worker_handler/request.go (renamed from plugins/http/request.go)2
-rw-r--r--plugins/http/worker_handler/response.go (renamed from plugins/http/response.go)2
-rw-r--r--plugins/http/worker_handler/uploads.go (renamed from plugins/http/uploads.go)2
12 files changed, 31 insertions, 30 deletions
diff --git a/plugins/gzip/plugin.go b/plugins/gzip/plugin.go
index 949c6888..18ee7b88 100644
--- a/plugins/gzip/plugin.go
+++ b/plugins/gzip/plugin.go
@@ -15,10 +15,10 @@ func (g *Plugin) Init() error {
return nil
}
-func (g *Plugin) Middleware(next http.Handler) http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
+func (g *Plugin) Middleware(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gziphandler.GzipHandler(next).ServeHTTP(w, r)
- }
+ })
}
func (g *Plugin) Name() string {
diff --git a/plugins/headers/plugin.go b/plugins/headers/plugin.go
index a5ee702f..dea0d127 100644
--- a/plugins/headers/plugin.go
+++ b/plugins/headers/plugin.go
@@ -38,9 +38,9 @@ func (s *Plugin) Init(cfg config.Configurer) error {
}
// middleware must return true if request/response pair is handled within the middleware.
-func (s *Plugin) Middleware(next http.Handler) http.HandlerFunc {
+func (s *Plugin) Middleware(next http.Handler) http.Handler {
// Define the http.HandlerFunc
- return func(w http.ResponseWriter, r *http.Request) {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if s.cfg.Headers.Request != nil {
for k, v := range s.cfg.Headers.Request {
r.Header.Add(k, v)
@@ -62,7 +62,7 @@ func (s *Plugin) Middleware(next http.Handler) http.HandlerFunc {
}
next.ServeHTTP(w, r)
- }
+ })
}
func (s *Plugin) Name() string {
diff --git a/plugins/http/plugin.go b/plugins/http/plugin.go
index dcfb7ddb..33efaf37 100644
--- a/plugins/http/plugin.go
+++ b/plugins/http/plugin.go
@@ -22,6 +22,8 @@ import (
"github.com/spiral/roadrunner/v2/plugins/config"
"github.com/spiral/roadrunner/v2/plugins/http/attributes"
httpConfig "github.com/spiral/roadrunner/v2/plugins/http/config"
+ "github.com/spiral/roadrunner/v2/plugins/http/static"
+ handler "github.com/spiral/roadrunner/v2/plugins/http/worker_handler"
"github.com/spiral/roadrunner/v2/plugins/logger"
"github.com/spiral/roadrunner/v2/plugins/server"
"github.com/spiral/roadrunner/v2/plugins/status"
@@ -35,16 +37,15 @@ const (
// PluginName declares plugin name.
PluginName = "http"
- // RR_HTTP env variable key (internal) if the HTTP presents
- RR_MODE = "RR_MODE" //nolint:golint,stylecheck
+ // RrMode RR_HTTP env variable key (internal) if the HTTP presents
+ RrMode = "RR_MODE"
- // HTTPS_SCHEME
- HTTPS_SCHEME = "https" //nolint:golint,stylecheck
+ HTTPSScheme = "https"
)
// Middleware interface
type Middleware interface {
- Middleware(f http.Handler) http.HandlerFunc
+ Middleware(f http.Handler) http.Handler
}
type middleware map[string]Middleware
@@ -69,7 +70,7 @@ type Plugin struct {
pool pool.Pool
// servers RR handler
- handler *Handler
+ handler *handler.Handler
// servers
http *http.Server
@@ -111,14 +112,14 @@ func (s *Plugin) Init(cfg config.Configurer, rrLogger logger.Logger, server serv
s.cfg.Env = make(map[string]string)
}
- s.cfg.Env[RR_MODE] = "http"
+ s.cfg.Env[RrMode] = "http"
s.server = server
return nil
}
func (s *Plugin) logCallback(event interface{}) {
- if ev, ok := event.(ResponseEvent); ok {
+ if ev, ok := event.(handler.ResponseEvent); ok {
s.log.Debug(fmt.Sprintf("%d %s %s", ev.Response.Status, ev.Request.Method, ev.Request.URI),
"remote", ev.Request.RemoteAddr,
"elapsed", ev.Elapsed().String(),
@@ -156,7 +157,7 @@ func (s *Plugin) serve(errCh chan error) { //nolint:gocognit
return
}
- s.handler, err = NewHandler(
+ s.handler, err = handler.NewHandler(
s.cfg.MaxRequestSize,
*s.cfg.Uploads,
s.cfg.Cidrs,
@@ -174,7 +175,7 @@ func (s *Plugin) serve(errCh chan error) { //nolint:gocognit
// if we have static, handler here, create a fileserver
if s.cfg.Static != nil {
- h := http.FileServer(StaticFilesHandler(s.cfg.Static))
+ h := http.FileServer(static.FS(s.cfg.Static))
// Static files handler
mux.HandleFunc(s.cfg.Static.Pattern, func(w http.ResponseWriter, r *http.Request) {
if s.cfg.Static.Request != nil {
@@ -429,7 +430,7 @@ func (s *Plugin) Reset() error {
s.log.Info("HTTP workers Pool successfully restarted")
- s.handler, err = NewHandler(
+ s.handler, err = handler.NewHandler(
s.cfg.MaxRequestSize,
*s.cfg.Uploads,
s.cfg.Cidrs,
@@ -500,7 +501,7 @@ func (s *Plugin) Ready() status.Status {
func (s *Plugin) redirect(w http.ResponseWriter, r *http.Request) {
target := &url.URL{
- Scheme: HTTPS_SCHEME,
+ Scheme: HTTPSScheme,
// host or host:port
Host: s.tlsAddr(r.Host, false),
Path: r.URL.Path,
@@ -641,7 +642,7 @@ func (s *Plugin) tlsAddr(host string, forcePort bool) string {
}
func applyMiddlewares(server *http.Server, middlewares map[string]Middleware, order []string, log logger.Logger) {
- for i := 0; i < len(order); i++ {
+ for i := len(order) - 1; i >= 0; i-- {
if mdwr, ok := middlewares[order[i]]; ok {
server.Handler = mdwr.Middleware(server.Handler)
} else {
diff --git a/plugins/http/static.go b/plugins/http/static/static.go
index be977fb3..d0278466 100644
--- a/plugins/http/static.go
+++ b/plugins/http/static/static.go
@@ -1,4 +1,4 @@
-package http
+package static
import (
"io/fs"
@@ -82,7 +82,7 @@ func (f FileSystem) Open(name string) (http.File, error) {
return nil, fs.ErrNotExist
}
-// StaticFilesHandler is a constructor for the http.FileSystem
-func StaticFilesHandler(config *httpConfig.Static) http.FileSystem {
+// FS is a constructor for the http.FileSystem
+func FS(config *httpConfig.Static) http.FileSystem {
return FileSystem{NewExtensionFilter(config.Allow, config.Forbid), http.Dir(config.Dir)}
}
diff --git a/plugins/http/constants.go b/plugins/http/worker_handler/constants.go
index c3d5c589..3355d9c2 100644
--- a/plugins/http/constants.go
+++ b/plugins/http/worker_handler/constants.go
@@ -1,4 +1,4 @@
-package http
+package handler
import "net/http"
diff --git a/plugins/http/errors.go b/plugins/http/worker_handler/errors.go
index 5889aa76..5fa8e64e 100644
--- a/plugins/http/errors.go
+++ b/plugins/http/worker_handler/errors.go
@@ -1,6 +1,6 @@
// +build !windows
-package http
+package handler
import (
"errors"
diff --git a/plugins/http/errors_windows.go b/plugins/http/worker_handler/errors_windows.go
index 3d0ba04c..390cc7d1 100644
--- a/plugins/http/errors_windows.go
+++ b/plugins/http/worker_handler/errors_windows.go
@@ -1,6 +1,6 @@
// +build windows
-package http
+package handler
import (
"errors"
diff --git a/plugins/http/handler.go b/plugins/http/worker_handler/handler.go
index d3c928aa..be53fc12 100644
--- a/plugins/http/handler.go
+++ b/plugins/http/worker_handler/handler.go
@@ -1,4 +1,4 @@
-package http
+package handler
import (
"net"
diff --git a/plugins/http/parse.go b/plugins/http/worker_handler/parse.go
index 780e1279..2790da2a 100644
--- a/plugins/http/parse.go
+++ b/plugins/http/worker_handler/parse.go
@@ -1,4 +1,4 @@
-package http
+package handler
import (
"net/http"
diff --git a/plugins/http/request.go b/plugins/http/worker_handler/request.go
index a1398819..178bc827 100644
--- a/plugins/http/request.go
+++ b/plugins/http/worker_handler/request.go
@@ -1,4 +1,4 @@
-package http
+package handler
import (
"fmt"
diff --git a/plugins/http/response.go b/plugins/http/worker_handler/response.go
index 17049ce1..1763d304 100644
--- a/plugins/http/response.go
+++ b/plugins/http/worker_handler/response.go
@@ -1,4 +1,4 @@
-package http
+package handler
import (
"io"
diff --git a/plugins/http/uploads.go b/plugins/http/worker_handler/uploads.go
index f9f8e1c8..e695000e 100644
--- a/plugins/http/uploads.go
+++ b/plugins/http/worker_handler/uploads.go
@@ -1,4 +1,4 @@
-package http
+package handler
import (
"github.com/spiral/roadrunner/v2/plugins/http/config"