stop riding the short bus, no clue why this stuff is here. only adds confusion, removing (#1)

server exposes Router field
This commit is contained in:
Reed Allman
2017-07-30 16:31:31 -07:00
committed by Travis Reeder
parent 85f7a53cc0
commit 53cbe2d5a4
5 changed files with 5 additions and 120 deletions

View File

@@ -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 == "" {

View File

@@ -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 {

View File

@@ -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
}

View File

@@ -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)
// }
}