diff --git a/api/models/error.go b/api/models/error.go index 43162fc87..dbbe2edb2 100644 --- a/api/models/error.go +++ b/api/models/error.go @@ -144,10 +144,6 @@ var ( code: http.StatusBadRequest, error: errors.New("Negative idle timeout"), } - ErrNoSpecialHandlerFound = err{ - code: http.StatusNotFound, - error: errors.New("Path not found"), - } ErrCallNotFound = err{ code: http.StatusNotFound, error: errors.New("Call not found"), diff --git a/api/server/runner.go b/api/server/runner.go index fa5825242..747cc8c48 100644 --- a/api/server/runner.go +++ b/api/server/runner.go @@ -28,31 +28,6 @@ type runnerResponse struct { Error *models.ErrorBody `json:"error,omitempty"` } -func (s *Server) handleSpecial(c *gin.Context) { - ctx := c.Request.Context() - - ctx = context.WithValue(ctx, api.AppName, "") - c.Set(api.AppName, "") - ctx = context.WithValue(ctx, api.Path, c.Request.URL.Path) - c.Set(api.Path, c.Request.URL.Path) - - r, err := s.UseSpecialHandlers(c.Writer, c.Request) - if err != nil { - handleErrorResponse(c, err) - return - } - - c.Request = r - c.Set(api.AppName, r.Context().Value(api.AppName).(string)) - if c.MustGet(api.AppName).(string) == "" { - handleErrorResponse(c, models.ErrRoutesNotFound) - return - } - - // now call the normal runner call - s.handleRequest(c, nil) -} - func toEnvName(envtype, name string) string { name = strings.ToUpper(strings.Replace(name, "-", "_", -1)) if envtype == "" { diff --git a/api/server/server.go b/api/server/server.go index 88f02d54f..5b10e86dc 100644 --- a/api/server/server.go +++ b/api/server/server.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "io/ioutil" "net" @@ -50,7 +51,6 @@ type Server struct { apiURL string - specialHandlers []SpecialHandler appListeners []AppListener middlewares []Middleware runnerListeners []RunnerListener @@ -385,8 +385,10 @@ func (s *Server) bindHandlers(ctx context.Context) { engine.Any("/r/:app", s.handleRunnerRequest) engine.Any("/r/:app/*route", s.handleRunnerRequest) - // This final route is used for extensions, see Server.Add - engine.NoRoute(s.handleSpecial) + engine.NoRoute(func(c *gin.Context) { + logrus.Debugln("not found", c.Request.URL.Path) + c.JSON(http.StatusNotFound, simpleError(errors.New("Path not found"))) + }) } type appResponse struct { diff --git a/api/server/special_handler.go b/api/server/special_handler.go deleted file mode 100644 index e19f09d7f..000000000 --- a/api/server/special_handler.go +++ /dev/null @@ -1,36 +0,0 @@ -package server - -import ( - "net/http" - - "github.com/fnproject/fn/api/models" -) - -// SpecialHandler verysimilar to a handler but since it is not used as middle ware no way -// to get context without returning it. So we just return a request which could have newly made -// contexts. -type SpecialHandler interface { - Handle(w http.ResponseWriter, r *http.Request) (*http.Request, error) -} - -// AddSpecialHandler adds the SpecialHandler to the specialHandlers list. -func (s *Server) AddSpecialHandler(handler SpecialHandler) { - s.specialHandlers = append(s.specialHandlers, handler) -} - -// UseSpecialHandlers execute all special handlers -func (s *Server) UseSpecialHandlers(resp http.ResponseWriter, req *http.Request) (*http.Request, error) { - if len(s.specialHandlers) == 0 { - return req, models.ErrNoSpecialHandlerFound - } - var r *http.Request - var err error - - for _, l := range s.specialHandlers { - r, err = l.Handle(resp, req) - if err != nil { - return nil, err - } - } - return r, nil -} diff --git a/api/server/special_handler_test.go b/api/server/special_handler_test.go deleted file mode 100644 index b6989ad11..000000000 --- a/api/server/special_handler_test.go +++ /dev/null @@ -1,52 +0,0 @@ -package server - -import ( - "net/http" - "testing" -) - -type testSpecialHandler struct{} - -func (h *testSpecialHandler) Handle(w http.ResponseWriter, r *http.Request) (*http.Request, error) { - // r = r.WithContext(context.WithValue(r.Context(), api.AppName, "test")) - return r, nil -} - -func TestSpecialHandlerSet(t *testing.T) { - // todo: temporarily commented as we may remove special handlers - // ctx := context.Background() - - // tasks := make(chan task.Request) - // ctx, cancel := context.WithCancel(context.Background()) - // defer cancel() - - // rnr, cancelrnr := testRunner(t) - // defer cancelrnr() - - // s := &Server{ - // Runner: rnr, - // Router: gin.New(), - // Datastore: &datastore.Mock{ - // Apps: []*models.App{ - // {Name: "test"}, - // }, - // Routes: []*models.Route{ - // {Path: "/test", Image: "funcy/hello", AppName: "test"}, - // }, - // }, - // MQ: &mqs.Mock{}, - // tasks: tasks, - // Enqueue: DefaultEnqueue, - // } - - // router := s.Router - // router.Use(prepareMiddleware(ctx)) - // s.bindHandlers() - // s.AddSpecialHandler(&testSpecialHandler{}) - - // _, rec := routerRequest(t, router, "GET", "/test", nil) - // if rec.Code != 200 { - // dump, _ := httputil.DumpResponse(rec.Result(), true) - // t.Fatalf("Test SpecialHandler: expected special handler to run functions successfully. Response:\n%s", dump) - // } -}