Skip to content

Commit

Permalink
Changed default config values slightly, tuning JS middleware now poss…
Browse files Browse the repository at this point in the history
…ible, Session lookups are really expensive
  • Loading branch information
Martin Buhr committed Jan 15, 2015
1 parent 015d1ec commit 900e565
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Config struct {
ListenPort int `json:"listen_port"`
Secret string `json:"secret"`
TemplatePath string `json:"template_path"`
TykJSPath string `json:"tyk_js_path"`
UseDBAppConfigs bool `json:"use_db_app_configs"`
AppPath string `json:"app_path"`
Storage struct {
Expand Down Expand Up @@ -45,6 +46,7 @@ func WriteDefaultConf(configStruct *Config) {
configStruct.ListenPort = 8080
configStruct.Secret = "352d20ee67be67f6340b4c0605b044b7"
configStruct.TemplatePath = "./templates"
configStruct.TykJSPath = "./js/tyk.js"
configStruct.Storage.Type = "redis"
configStruct.AppPath = "./apps/"
configStruct.Storage.Host = "localhost"
Expand Down
17 changes: 9 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/docopt/docopt.go"
"github.com/justinas/alice"
"github.com/rcrowley/goagain"
"github.com/lonelycode/tykcommon"
"html/template"
"net"
"net/http"
Expand Down Expand Up @@ -202,16 +203,16 @@ func loadApps(APISpecs []APISpec, Muxer *http.ServeMux) {

//TODO: Add a VM AND LOAD MIDDLEWARE CLASSES here (TESTING)
mwPaths := []string{}
mwPreNames := []string{}
mwPostNames := []string{}
mwPreFuncs := []tykcommon.MiddlewareDefinition{}
mwPostFuncs := []tykcommon.MiddlewareDefinition{}
for _, mwObj := range referenceSpec.APIDefinition.CustomMiddleware.Pre {
mwPaths = append(mwPaths, mwObj.Path)
mwPreNames = append(mwPreNames, mwObj.Name)
mwPreFuncs = append(mwPreFuncs, mwObj)
log.Info("Loading custom PRE-PROCESSOR middleware: ", mwObj.Name)
}
for _, mwObj := range referenceSpec.APIDefinition.CustomMiddleware.Post {
mwPaths = append(mwPaths, mwObj.Path)
mwPostNames = append(mwPostNames, mwObj.Name)
mwPostFuncs = append(mwPostFuncs, mwObj)
log.Info("Loading custom POST-PROCESSOR middleware: ", mwObj.Name)
}

Expand Down Expand Up @@ -271,16 +272,16 @@ func loadApps(APISpecs []APISpec, Muxer *http.ServeMux) {
}

// Add pre-process MW
for _, name := range mwPreNames {
chainArray = append(chainArray, CreateDynamicMiddleware(name, true, tykMiddleware))
for _, obj := range mwPreFuncs {
chainArray = append(chainArray, CreateDynamicMiddleware(obj.Name, true, obj.RequireSession, tykMiddleware))
}

for _, baseMw := range baseChainArray {
chainArray = append(chainArray, baseMw)
}

for _, name := range mwPostNames {
chainArray = append(chainArray, CreateDynamicMiddleware(name, false, tykMiddleware))
for _, obj := range mwPostFuncs {
chainArray = append(chainArray, CreateDynamicMiddleware(obj.Name, false, obj.RequireSession, tykMiddleware))
}

// Use CreateMiddleware(&ModifiedMiddleware{tykMiddleware}, tykMiddleware) to run custom middleware
Expand Down
9 changes: 7 additions & 2 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ type TykMiddlewareImplementation interface {
ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) // Handles request
}

func CreateDynamicMiddleware(MiddlewareName string, IsPre bool, tykMwSuper TykMiddleware) func(http.Handler) http.Handler {
dMiddleware := &DynamicMiddleware{tykMwSuper, MiddlewareName, IsPre}
func CreateDynamicMiddleware(MiddlewareName string, IsPre, UseSession bool, tykMwSuper TykMiddleware) func(http.Handler) http.Handler {
dMiddleware := &DynamicMiddleware{
TykMiddleware: tykMwSuper,
MiddlewareClassName: MiddlewareName,
Pre: IsPre,
UseSession: UseSession,
}

return CreateMiddleware(dMiddleware, tykMwSuper)
}
Expand Down
24 changes: 15 additions & 9 deletions plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import (
"io"
"io/ioutil"
"net/http"
)

const (
TYKJS_PATH string = "js/tyk.js"
"time"
)

// MiniRequestObject is marshalled to JSON string and pased into JSON middleware
Expand Down Expand Up @@ -44,6 +41,7 @@ type DynamicMiddleware struct {
TykMiddleware
MiddlewareClassName string
Pre bool
UseSession bool
}

type DynamicMiddlewareConfig struct {
Expand All @@ -69,6 +67,8 @@ func (d *DynamicMiddleware) GetConfig() (interface{}, error) {
// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
func (d *DynamicMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {

t1 := time.Now().UnixNano()

// Createthe proxy object
defer r.Body.Close()
originalBody, err := ioutil.ReadAll(r.Body)
Expand Down Expand Up @@ -98,8 +98,10 @@ func (d *DynamicMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Reques

// Encode the session object (if not a pre-process)
if !d.Pre {
thisSessionState = context.Get(r, SessionData).(SessionState)
authHeaderValue = context.Get(r, AuthHeaderValue).(string)
if d.UseSession {
thisSessionState = context.Get(r, SessionData).(SessionState)
authHeaderValue = context.Get(r, AuthHeaderValue).(string)
}
}

sessionAsJsonObj, sessEncErr := json.Marshal(thisSessionState)
Expand Down Expand Up @@ -152,9 +154,13 @@ func (d *DynamicMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Reques

// Save the sesison data (if modified)
if !d.Pre {
thisSessionState.MetaData = newRequestData.SessionMeta
d.Spec.SessionManager.UpdateSession(authHeaderValue, thisSessionState, 0)
if d.UseSession {
thisSessionState.MetaData = newRequestData.SessionMeta
d.Spec.SessionManager.UpdateSession(authHeaderValue, thisSessionState, 0)
}
}

log.Info("JSVM middleware execution took: (ns) ", time.Now().UnixNano() - t1)

return nil, 200
}
Expand All @@ -164,7 +170,7 @@ func (d *DynamicMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Reques
// CreateJSVM Creates a new VM object for an API to load middleware into
func CreateJSVM(middlewarePaths []string) *otto.Otto {
vm := otto.New()
coreJs, _ := ioutil.ReadFile(TYKJS_PATH)
coreJs, _ := ioutil.ReadFile(config.TykJSPath)
// run core elements

vm.Set("log", func(call otto.FunctionCall) otto.Value {
Expand Down
1 change: 1 addition & 0 deletions tyk.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"listen_port": 8080,
"secret": "352d20ee67be67f6340b4c0605b044b7",
"template_path": "/etc/tyk/templates/",
"tyk_js_path": "/etc/tyk/js/tyk.js",
"use_db_app_configs": false,
"app_path": "/etc/tyk/apps/",
"storage": {
Expand Down

0 comments on commit 900e565

Please sign in to comment.