github.com/jbking/gohan@v0.0.0-20151217002006-b41ccf1c2a96/extension/v8/v8.go (about) 1 // Copyright (C) 2015 NTT Innovation Institute, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 12 // implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 // +build v8 17 // Copyright (C) 2015 NTT Innovation Institute, Inc. 18 // 19 // Licensed under the Apache License, Version 2.0 (the "License"); 20 // you may not use this file except in compliance with the License. 21 // You may obtain a copy of the License at 22 // 23 // http://www.apache.org/licenses/LICENSE-2.0 24 // 25 // Unless required by applicable law or agreed to in writing, software 26 // distributed under the License is distributed on an "AS IS" BASIS, 27 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 28 // implied. 29 // See the License for the specific language governing permissions and 30 // limitations under the License. 31 32 package v8 33 34 import ( 35 ext "github.com/cloudwan/gohan/extension" 36 "github.com/cloudwan/gohan/schema" 37 "github.com/twinj/uuid" 38 ) 39 40 var inits = []func(env *Environment){} 41 42 //RegisterInit registers init code 43 func RegisterInit(init func(env *Environment)) { 44 inits = append(inits, init) 45 } 46 47 //Environment is a interface for extension environment 48 type Environment struct { 49 rpc *RPC 50 goCallbacks []ext.GoCallback 51 } 52 53 //NewEnvironment makes new v8 environment 54 func NewEnvironment() *Environment { 55 env := &Environment{ 56 rpc: NewRPC(), 57 goCallbacks: []ext.GoCallback{}, 58 } 59 for _, init := range inits { 60 init(env) 61 } 62 return env 63 } 64 65 //Load loads script for environment. 66 func (env *Environment) Load(source, code string) error { 67 return env.rpc.Load(source, code) 68 } 69 70 //RegistObject register method 71 func (env *Environment) RegistObject(objectID string, object interface{}) { 72 env.rpc.RegistObject(objectID, object) 73 } 74 75 //Clone returns self 76 func (env *Environment) Clone() ext.Environment { 77 return env 78 } 79 80 //LoadExtensionsForPath for returns extensions for specific path 81 func (env *Environment) LoadExtensionsForPath(extensions []*schema.Extension, path string) error { 82 var err error 83 for _, extension := range extensions { 84 if extension.Match(path) { 85 code := extension.Code 86 if extension.CodeType == "donburi" { 87 err = env.runDonburi(code) 88 if err != nil { 89 return err 90 } 91 } else if extension.CodeType == "go" { 92 callback := ext.GetGoCallback(code) 93 if callback != nil { 94 env.goCallbacks = append(env.goCallbacks, callback) 95 } 96 } else { 97 err := env.rpc.Load(extension.URL, code) 98 if err != nil { 99 return err 100 } 101 } 102 } 103 } 104 105 return nil 106 } 107 108 //HandleEvent kicks event in loaded environment 109 func (env *Environment) HandleEvent(event string, context map[string]interface{}) error { 110 contextID := uuid.NewV4().String() 111 env.rpc.contexts[contextID] = context 112 context["contextID"] = contextID 113 defer delete(env.rpc.contexts, contextID) 114 updatedContext := env.rpc.BlockCall("gohan_handler", "handle_event", []interface{}{contextID, context, event}) 115 updatedContextMap, ok := updatedContext.(map[string]interface{}) 116 if !ok { 117 return nil 118 } 119 for key, value := range updatedContextMap { 120 context[key] = value 121 } 122 return nil 123 } 124 125 //RunDonburi runs Donburi code 126 func (env *Environment) runDonburi(yamlCode string) error { 127 return nil 128 }