2024-10-17 13:39:35 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"apigo.cc/gojs"
|
|
|
|
"apigo.cc/gojs/goja"
|
|
|
|
"github.com/ssgo/s"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
resp *s.Response
|
|
|
|
endCh chan bool
|
|
|
|
result any
|
2024-10-18 17:54:37 +08:00
|
|
|
Id string
|
2024-10-17 13:39:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) End(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(argsIn.Arguments) > 0 {
|
|
|
|
r.result = argsIn.Arguments[0].Export()
|
|
|
|
}
|
|
|
|
if r.endCh != nil {
|
|
|
|
r.endCh <- true
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) SetStatus(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|
|
|
r.resp.WriteHeader(args.Int(0))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) SetCookie(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(2)
|
|
|
|
path := "/"
|
|
|
|
domain := ""
|
|
|
|
expires := time.Time{}
|
|
|
|
maxAge := 0
|
|
|
|
secure := false
|
|
|
|
httpOnly := false
|
|
|
|
obj := args.Obj(2)
|
|
|
|
if obj != nil {
|
|
|
|
path = obj.Str("path")
|
|
|
|
domain = obj.Str("domain")
|
|
|
|
maxAge = obj.Int("maxAge")
|
|
|
|
secure = obj.Bool("secure")
|
|
|
|
httpOnly = obj.Bool("httpOnly")
|
|
|
|
if expiresV := obj.Get("expires"); expiresV != nil {
|
|
|
|
expiresK := expiresV.ExportType().Kind()
|
|
|
|
if expiresK == reflect.String {
|
|
|
|
expires, _ = time.Parse("2006-01-02 15:04:05", expiresV.String())
|
|
|
|
} else if expiresK == reflect.Int64 || expiresK == reflect.Int {
|
|
|
|
expires = time.Now().Add(time.Duration(expiresV.ToInteger()) * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
cookie := http.Cookie{
|
|
|
|
Name: args.Str(0),
|
|
|
|
Value: args.Str(1),
|
|
|
|
Path: path,
|
|
|
|
Domain: domain,
|
|
|
|
Expires: expires,
|
|
|
|
MaxAge: maxAge,
|
|
|
|
Secure: secure,
|
|
|
|
HttpOnly: httpOnly,
|
|
|
|
}
|
|
|
|
http.SetCookie(r.resp, &cookie)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) SetHeader(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(2)
|
|
|
|
r.resp.Header().Set(args.Str(0), args.Str(1))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) AddHeader(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(2)
|
|
|
|
r.resp.Header().Add(args.Str(0), args.Str(1))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) GetHeader(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|
|
|
return vm.ToValue(r.resp.Header().Get(args.Str(0)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) Write(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|
|
|
n, err := r.resp.Write(args.Bytes(0))
|
|
|
|
if err != nil {
|
|
|
|
panic(vm.NewGoError(err))
|
|
|
|
}
|
|
|
|
return vm.ToValue(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) Flush(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
r.resp.Flush()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) SendFile(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(2)
|
|
|
|
r.resp.SendFile(args.Str(0), args.Str(1))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) DownloadFile(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(3)
|
|
|
|
r.resp.DownloadFile(args.Str(0), args.Str(1), args.Bytes(1))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) Location(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|
|
|
r.resp.Location(args.Str(0))
|
|
|
|
return nil
|
|
|
|
}
|