mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Updated README and simplified/cleaned up some code.
This commit is contained in:
@@ -27,7 +27,8 @@ func setLogBuffer() *bytes.Buffer {
|
||||
func TestAppCreate(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
s := New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
|
||||
router := testRouter(s)
|
||||
|
||||
for i, test := range []struct {
|
||||
path string
|
||||
@@ -70,8 +71,8 @@ func TestAppCreate(t *testing.T) {
|
||||
|
||||
func TestAppDelete(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
s := New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
|
||||
router := testRouter(s)
|
||||
|
||||
for i, test := range []struct {
|
||||
path string
|
||||
@@ -104,8 +105,8 @@ func TestAppDelete(t *testing.T) {
|
||||
|
||||
func TestAppList(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
s := New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
|
||||
router := testRouter(s)
|
||||
|
||||
for i, test := range []struct {
|
||||
path string
|
||||
@@ -137,8 +138,8 @@ func TestAppList(t *testing.T) {
|
||||
|
||||
func TestAppGet(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
s := New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
|
||||
router := testRouter(s)
|
||||
|
||||
for i, test := range []struct {
|
||||
path string
|
||||
@@ -170,8 +171,8 @@ func TestAppGet(t *testing.T) {
|
||||
|
||||
func TestAppUpdate(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
s := New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
|
||||
router := testRouter(s)
|
||||
|
||||
for i, test := range []struct {
|
||||
path string
|
||||
|
||||
90
api/server/helpers.go
Normal file
90
api/server/helpers.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package server
|
||||
|
||||
// TODO: this whole file shouldn't be in a non test file
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/iron-io/functions/api/models"
|
||||
"github.com/iron-io/functions/api/runner"
|
||||
"github.com/iron-io/runner/common"
|
||||
)
|
||||
|
||||
type appResponse struct {
|
||||
Message string `json:"message"`
|
||||
App *models.App `json:"app"`
|
||||
}
|
||||
|
||||
type appsResponse struct {
|
||||
Message string `json:"message"`
|
||||
Apps models.Apps `json:"apps"`
|
||||
}
|
||||
|
||||
type routeResponse struct {
|
||||
Message string `json:"message"`
|
||||
Route *models.Route `json:"route"`
|
||||
}
|
||||
|
||||
type routesResponse struct {
|
||||
Message string `json:"message"`
|
||||
Routes models.Routes `json:"routes"`
|
||||
}
|
||||
|
||||
type tasksResponse struct {
|
||||
Message string `json:"message"`
|
||||
Task models.Task `json:"tasksResponse"`
|
||||
}
|
||||
|
||||
func testRouter(s *Server) *gin.Engine {
|
||||
r := gin.Default()
|
||||
ctx := context.Background()
|
||||
r.Use(func(c *gin.Context) {
|
||||
ctx, _ := common.LoggerWithFields(ctx, extractFields(c))
|
||||
c.Set("ctx", ctx)
|
||||
c.Next()
|
||||
})
|
||||
s.bindHandlers()
|
||||
return r
|
||||
}
|
||||
|
||||
func testRunner(t *testing.T) *runner.Runner {
|
||||
r, err := runner.New(runner.NewMetricLogger())
|
||||
if err != nil {
|
||||
t.Fatal("Test: failed to create new runner")
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func routerRequest(t *testing.T, router *gin.Engine, method, path string, body io.Reader) (*http.Request, *httptest.ResponseRecorder) {
|
||||
req, err := http.NewRequest(method, "http://localhost:8080"+path, body)
|
||||
if err != nil {
|
||||
t.Fatalf("Test: Could not create %s request to %s: %v", method, path, err)
|
||||
}
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
router.ServeHTTP(rec, req)
|
||||
|
||||
return req, rec
|
||||
}
|
||||
|
||||
func getErrorResponse(t *testing.T, rec *httptest.ResponseRecorder) models.Error {
|
||||
respBody, err := ioutil.ReadAll(rec.Body)
|
||||
if err != nil {
|
||||
t.Error("Test: Expected not empty response body")
|
||||
}
|
||||
|
||||
var errResp models.Error
|
||||
err = json.Unmarshal(respBody, &errResp)
|
||||
if err != nil {
|
||||
t.Error("Test: Expected response body to be a valid models.Error object")
|
||||
}
|
||||
|
||||
return errResp
|
||||
}
|
||||
@@ -42,12 +42,11 @@ func handleRequest(c *gin.Context, enqueue models.Enqueue) {
|
||||
}
|
||||
|
||||
ctx := c.MustGet("ctx").(context.Context)
|
||||
log := common.Logger(ctx)
|
||||
|
||||
reqID := uuid.NewV5(uuid.Nil, fmt.Sprintf("%s%s%d", c.Request.RemoteAddr, c.Request.URL.Path, time.Now().Unix())).String()
|
||||
c.Set("reqID", reqID) // todo: put this in the ctx instead of gin's
|
||||
|
||||
ctx, log = common.LoggerWithFields(ctx, logrus.Fields{"call_id": reqID})
|
||||
ctx, log := common.LoggerWithFields(ctx, logrus.Fields{"call_id": reqID})
|
||||
|
||||
var err error
|
||||
var payload io.Reader
|
||||
@@ -164,7 +163,8 @@ func handleRequest(c *gin.Context, enqueue models.Enqueue) {
|
||||
task := &models.Task{}
|
||||
task.Image = &cfg.Image
|
||||
task.ID = cfg.ID
|
||||
task.RouteName = cfg.AppName
|
||||
task.Path = el.Path
|
||||
task.AppName = cfg.AppName
|
||||
task.Priority = &priority
|
||||
task.EnvVars = cfg.Env
|
||||
task.Payload = string(pl)
|
||||
|
||||
@@ -131,7 +131,6 @@ func extractFields(c *gin.Context) logrus.Fields {
|
||||
for _, param := range c.Params {
|
||||
fields[param.Key] = param.Value
|
||||
}
|
||||
|
||||
return fields
|
||||
}
|
||||
|
||||
@@ -142,7 +141,7 @@ func (s *Server) Run(ctx context.Context) {
|
||||
c.Next()
|
||||
})
|
||||
|
||||
bindHandlers(s.Router, s.handleRunnerRequest, s.handleTaskRequest)
|
||||
s.bindHandlers()
|
||||
|
||||
// By default it serves on :8080 unless a
|
||||
// PORT environment variable was defined.
|
||||
@@ -150,8 +149,9 @@ func (s *Server) Run(ctx context.Context) {
|
||||
<-ctx.Done()
|
||||
}
|
||||
|
||||
func bindHandlers(engine *gin.Engine, reqHandler func(ginC *gin.Context), taskHandler func(ginC *gin.Context)) {
|
||||
engine.Use(gin.Logger())
|
||||
func (s *Server) bindHandlers() {
|
||||
|
||||
engine := s.Router
|
||||
|
||||
engine.GET("/", handlePing)
|
||||
engine.GET("/version", handleVersion)
|
||||
@@ -177,9 +177,9 @@ func bindHandlers(engine *gin.Engine, reqHandler func(ginC *gin.Context), taskHa
|
||||
}
|
||||
}
|
||||
|
||||
engine.DELETE("/tasks", taskHandler)
|
||||
engine.GET("/tasks", taskHandler)
|
||||
engine.Any("/r/:app/*route", reqHandler)
|
||||
engine.DELETE("/tasks", s.handleTaskRequest)
|
||||
engine.GET("/tasks", s.handleTaskRequest)
|
||||
engine.Any("/r/:app/*route", s.handleRunnerRequest)
|
||||
|
||||
// This final route is used for extensions, see Server.Add
|
||||
engine.NoRoute(handleSpecial)
|
||||
|
||||
Reference in New Issue
Block a user