refactor runner using titan

This commit is contained in:
Pedro Nasser
2016-07-24 17:46:08 -03:00
parent a8e984f834
commit 14cc57fd9c
12 changed files with 339 additions and 369 deletions

View File

@@ -1,15 +0,0 @@
package server
type Config struct {
DatabaseURL string `json:"db"`
Logging struct {
To string `json:"to"`
Level string `json:"level"`
Prefix string `json:"prefix"`
}
}
func (c *Config) Validate() error {
// TODO:
return nil
}

View File

@@ -266,6 +266,10 @@ func buildFilterQuery(filter *models.RouteFilter) string {
filterQuery := ""
filterQueries := []string{}
if filter.Path != "" {
filterQueries = append(filterQueries, fmt.Sprintf("path = '%s'", filter.Path))
}
if filter.AppName != "" {
filterQueries = append(filterQueries, fmt.Sprintf("app_name = '%s'", filter.AppName))
}

View File

@@ -29,7 +29,8 @@ func Start(engine *gin.Engine) {
}
engine.GET("/r/:app/*route", handleRunner)
engine.Any("/r/:app/*route", handleRunner)
engine.NoRoute(handleRunner)
}
func simpleError(err error) *models.Error {

View File

@@ -1,19 +1,84 @@
package router
import (
"bytes"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/iron-io/functions/api/models"
"github.com/iron-io/functions/api/runner"
)
func handleRunner(c *gin.Context) {
log := c.MustGet("log").(logrus.FieldLogger)
store := c.MustGet("store").(models.Datastore)
config := c.MustGet("config").(*models.Config)
err := runner.Run(c)
if err != nil {
log.Debug(err)
c.JSON(http.StatusInternalServerError, simpleError(err))
var err error
var payload []byte
if c.Request.Method == "POST" || c.Request.Method == "PUT" {
payload, err = ioutil.ReadAll(c.Request.Body)
} else if c.Request.Method == "GET" {
qPL := c.Request.URL.Query()["payload"]
if len(qPL) > 0 {
payload = []byte(qPL[0])
}
}
log.WithField("payload", string(payload)).Debug("Got payload")
appName := c.Param("app")
if appName == "" {
host := strings.Split(c.Request.Header.Get("Host"), ":")[0]
appName = strings.Split(host, ".")[0]
}
route := c.Param("route")
if route == "" {
route = c.Request.URL.Path
}
filter := &models.RouteFilter{
Path: route,
AppName: appName,
}
log.WithFields(logrus.Fields{"app": appName, "path": route}).Debug("Finding route on datastore")
routes, err := store.GetRoutes(filter)
if err != nil {
log.WithError(err).Error(models.ErrRoutesList)
c.JSON(http.StatusInternalServerError, simpleError(models.ErrRoutesList))
}
log.WithField("routes", routes).Debug("Got routes from datastore")
for _, el := range routes {
if el.Path == route {
titanJob := runner.CreateTitanJob(&runner.RouteRunner{
Route: el,
Endpoint: config.API,
Payload: string(payload),
Timeout: 30 * time.Second,
})
if err := titanJob.Wait(); err != nil {
log.WithError(err).Error(models.ErrRunnerRunRoute)
c.JSON(http.StatusInternalServerError, simpleError(models.ErrRunnerRunRoute))
} else {
for k, v := range el.Headers {
c.Header(k, v[0])
}
c.Data(http.StatusOK, "", bytes.Trim(titanJob.Result(), "\x00"))
}
return
}
}
}

View File

@@ -7,16 +7,17 @@ import (
"github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/iron-io/functions/api/models"
"github.com/iron-io/functions/api/server/datastore"
"github.com/iron-io/functions/api/server/router"
)
type Server struct {
router *gin.Engine
cfg *Config
cfg *models.Config
}
func New(config *Config) *Server {
func New(config *models.Config) *Server {
return &Server{
router: gin.Default(),
cfg: config,
@@ -37,6 +38,10 @@ func (s *Server) Start() {
s.cfg.DatabaseURL = fmt.Sprintf("bolt://%s/bolt.db?bucket=funcs", cwd)
}
if s.cfg.API == "" {
s.cfg.API = "http://localhost:8080"
}
ds, err := datastore.New(s.cfg.DatabaseURL)
if err != nil {
logrus.WithError(err).Fatalln("Invalid DB url.")
@@ -46,6 +51,7 @@ func (s *Server) Start() {
logrus.SetLevel(logrus.DebugLevel)
s.router.Use(func(c *gin.Context) {
c.Set("config", s.cfg)
c.Set("store", ds)
c.Set("log", logrus.WithFields(extractFields(c)))
c.Next()