use interceptor in SHMux
This commit is contained in:
parent
ae083e9d9d
commit
8fff0e8888
18
gosh.go
18
gosh.go
|
|
@ -3,8 +3,8 @@ package gosh
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"git.kingecg.top/kingecg/gosh/interceptor"
|
||||||
"github.com/mattn/anko/env"
|
"github.com/mattn/anko/env"
|
||||||
"github.com/mattn/anko/vm"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -26,15 +26,17 @@ type SHMux struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSHMux() *SHMux {
|
func NewSHMux() *SHMux {
|
||||||
return &SHMux{
|
s := &SHMux{
|
||||||
ServeMux: http.NewServeMux(),
|
ServeMux: http.NewServeMux(),
|
||||||
ankoEnv: env.NewEnv(),
|
ankoEnv: env.NewEnv(),
|
||||||
}
|
}
|
||||||
|
//s.executor = interceptor.NewAnkointerceptor(s.ankoEnv)
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SHMux) RegistFunction(name string, function interface{}) {
|
func (s *SHMux) RegistFunction(name string, function interface{}) {
|
||||||
if s.ankoEnv != nil {
|
if s.ankoEnv != nil {
|
||||||
s.ankoEnv.Define(name, function)
|
_ = s.ankoEnv.Define(name, function)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -111,10 +113,12 @@ func (s *SHMux) handle(w http.ResponseWriter, r *http.Request, script string) {
|
||||||
resHelper := &ResponseHelper{
|
resHelper := &ResponseHelper{
|
||||||
w: w,
|
w: w,
|
||||||
}
|
}
|
||||||
env := s.ankoEnv.Copy()
|
renv := s.ankoEnv.Copy()
|
||||||
env.Define("Req", reqHelper)
|
_ = renv.Define("Req", reqHelper)
|
||||||
env.Define("Res", resHelper)
|
_ = renv.Define("Res", resHelper)
|
||||||
_, err := vm.Execute(env, nil, script)
|
rinterceptor := interceptor.NewAnkointerceptor(renv)
|
||||||
|
|
||||||
|
_, err := rinterceptor.ExecContent(script)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -85,7 +85,7 @@ func (a *Ankointerceptor) loadLibrary(libPath string, libFuncMap map[string]func
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Load library error")
|
panic("Load library error")
|
||||||
}
|
}
|
||||||
a.libhabdles = append(a.libhabdles, lib)
|
a.liberalises = append(a.liberalises, lib)
|
||||||
fnmap := make(map[string]interface{})
|
fnmap := make(map[string]interface{})
|
||||||
fileds := make([]reflect.StructField, 0, len(fnmap))
|
fileds := make([]reflect.StructField, 0, len(fnmap))
|
||||||
for name, types := range libFuncMap {
|
for name, types := range libFuncMap {
|
||||||
|
|
@ -122,7 +122,7 @@ func (a *Ankointerceptor) loadLibrary(libPath string, libFuncMap map[string]func
|
||||||
// Close 关闭所有已加载的动态库句柄,释放资源。
|
// Close 关闭所有已加载的动态库句柄,释放资源。
|
||||||
// 应在不再需要使用动态库时调用此方法。
|
// 应在不再需要使用动态库时调用此方法。
|
||||||
func (a *Ankointerceptor) Close() {
|
func (a *Ankointerceptor) Close() {
|
||||||
for _, lib := range a.libhabdles {
|
for _, lib := range a.liberalises {
|
||||||
purego.Dlclose(lib)
|
_ = purego.Dlclose(lib)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,10 +10,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Ankointerceptor struct {
|
type Ankointerceptor struct {
|
||||||
importMap map[string]interface{}
|
importMap map[string]interface{}
|
||||||
libMap map[string]interface{}
|
libMap map[string]interface{}
|
||||||
libhabdles []uintptr
|
liberalises []uintptr
|
||||||
ankoEnv *env.Env
|
ankoEnv *env.Env
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAnkointerceptor(ankoEnv *env.Env) *Ankointerceptor {
|
func NewAnkointerceptor(ankoEnv *env.Env) *Ankointerceptor {
|
||||||
|
|
@ -21,13 +21,13 @@ func NewAnkointerceptor(ankoEnv *env.Env) *Ankointerceptor {
|
||||||
ankoEnv = env.NewEnv()
|
ankoEnv = env.NewEnv()
|
||||||
}
|
}
|
||||||
a := &Ankointerceptor{
|
a := &Ankointerceptor{
|
||||||
importMap: make(map[string]interface{}),
|
importMap: make(map[string]interface{}),
|
||||||
libMap: make(map[string]interface{}),
|
libMap: make(map[string]interface{}),
|
||||||
libhabdles: make([]uintptr, 0),
|
liberalises: make([]uintptr, 0),
|
||||||
ankoEnv: ankoEnv,
|
ankoEnv: ankoEnv,
|
||||||
}
|
}
|
||||||
a.ankoEnv.Define("println", fmt.Println)
|
_ = a.ankoEnv.Define("println", fmt.Println)
|
||||||
a.ankoEnv.Define("loadLibrary", func(libPath string, libFuncMap map[string]funcType) interface{} {
|
_ = a.ankoEnv.Define("loadLibrary", func(libPath string, libFuncMap map[string]funcType) interface{} {
|
||||||
return a.loadLibrary(libPath, libFuncMap)
|
return a.loadLibrary(libPath, libFuncMap)
|
||||||
})
|
})
|
||||||
a.importMap["fs"] = &FileModule{}
|
a.importMap["fs"] = &FileModule{}
|
||||||
|
|
@ -96,9 +96,9 @@ func (a *Ankointerceptor) exec(scriptContent string, scriptPath string) (interfa
|
||||||
cdir, _ := os.Getwd()
|
cdir, _ := os.Getwd()
|
||||||
scriptPath = filepath.Join(cdir, "tmp")
|
scriptPath = filepath.Join(cdir, "tmp")
|
||||||
}
|
}
|
||||||
e.Define("Require", a.genRequireMethod(scriptPath))
|
_ = e.Define("Require", a.genRequireMethod(scriptPath))
|
||||||
e.Define("__filename", scriptPath)
|
_ = e.Define("__filename", scriptPath)
|
||||||
e.Define("__dirname", filepath.Dir(scriptPath))
|
_ = e.Define("__dirname", filepath.Dir(scriptPath))
|
||||||
|
|
||||||
// Execute the script code in the prepared environment
|
// Execute the script code in the prepared environment
|
||||||
result, err := vm.Execute(e, nil, scriptContent)
|
result, err := vm.Execute(e, nil, scriptContent)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue