From 22707edcbf7063ac7d64a9d3faac02e40f672dc3 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Sat, 28 Mar 2015 16:52:43 -0600 Subject: [PATCH] Refactored fastcgi middleware to return errors --- middleware/fastcgi/fastcgi.go | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/middleware/fastcgi/fastcgi.go b/middleware/fastcgi/fastcgi.go index 6a9f71d4..281867f1 100644 --- a/middleware/fastcgi/fastcgi.go +++ b/middleware/fastcgi/fastcgi.go @@ -6,7 +6,6 @@ package fastcgi import ( "io" "io/ioutil" - "log" "net/http" "path/filepath" "strings" @@ -27,8 +26,8 @@ func New(c middleware.Controller) (middleware.Middleware, error) { rules = append(rules, rule) } - return func(next http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { + return func(next middleware.HandlerFunc) middleware.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) (int, error) { servedFcgi := false for _, rule := range rules { @@ -38,15 +37,13 @@ func New(c middleware.Controller) (middleware.Middleware, error) { // Get absolute file paths absPath, err := filepath.Abs(root + r.URL.Path) if err != nil { - // TODO! - log.Fatal(err) + return http.StatusInternalServerError, err } - // Get absolute file paths + // Get absolute file path to website root absRootPath, err := filepath.Abs(root) if err != nil { - // TODO! - log.Fatal(err) + return http.StatusInternalServerError, err } // Separate remote IP and port @@ -73,17 +70,17 @@ func New(c middleware.Controller) (middleware.Middleware, error) { fcgi, err := Dial("tcp", rule.address) if err != nil { - // TODO! + return http.StatusBadGateway, err } resp, err := fcgi.Get(env) if err != nil && err != io.EOF { - // TODO! + return http.StatusBadGateway, err } body, err := ioutil.ReadAll(resp.Body) if err != nil { - // TODO! + return http.StatusBadGateway, err } for key, vals := range resp.Header { @@ -100,8 +97,10 @@ func New(c middleware.Controller) (middleware.Middleware, error) { } if !servedFcgi { - next(w, r) + return next(w, r) } + + return 0, nil } }, nil }