feat: direct call download api by cgo

This commit is contained in:
liwei committed 2024-11-26 23:32:54 +08:00
1 parent 85fe92a378
commit 467ada5500
22 files changed
+1040 -709

No files matched your search

+64
View File
@@ -0,0 +1,64 @@
package bind
import (
"encoding/json"
"errors"
"github.com/GopeedLab/gopeed/pkg/api"
"github.com/GopeedLab/gopeed/pkg/api/model"
"sync"
)
type instanceArray struct {
mu sync.RWMutex
array []*api.Instance
}
// append appends an instance to the array.
// Returns the index of the instance in the array.
func (ia *instanceArray) append(instance *api.Instance) int {
ia.mu.Lock()
defer ia.mu.Unlock()
ia.array = append(ia.array, instance)
return len(ia.array) - 1
}
func (ia *instanceArray) get(index int) (*api.Instance, bool) {
ia.mu.RLock()
defer ia.mu.RUnlock()
if index < 0 || index >= len(ia.array) {
return nil, false
}
return ia.array[index], true
}
var instances = &instanceArray{
mu: sync.RWMutex{},
array: make([]*api.Instance, 0),
}
func Create(config *model.StartConfig) string {
config.ProductionMode = true
instance, err := api.Create(config)
if err != nil {
return BuildResult(err)
}
return BuildResult(instances.append(instance))
}
func Invoke(index int, request *api.Request) string {
instance, ok := instances.get(index)
if !ok {
return BuildResult(errors.New("instance not found"))
}
return BuildResult(api.Invoke(instance, request))
}
func BuildResult(data any) string {
if err, ok := data.(error); ok {
buf, _ := json.Marshal(model.NewErrorResult[any](err.Error()))
return string(buf)
}
buf, _ := json.Marshal(model.NewOkResult(data))
return string(buf)
}
+22 -14
View File
@@ -1,29 +1,37 @@
package main
/*
#include <stdlib.h>
*/
import "C"
import (
"encoding/json"
"github.com/GopeedLab/gopeed/pkg/rest"
"github.com/GopeedLab/gopeed/pkg/rest/model"
"github.com/GopeedLab/gopeed/bind"
"github.com/GopeedLab/gopeed/pkg/api"
"github.com/GopeedLab/gopeed/pkg/api/model"
"unsafe"
)
func main() {}
//export Start
func Start(cfg *C.char) (int, *C.char) {
//export Create
func Create(cfg *C.char) *C.char {
var config model.StartConfig
if err := json.Unmarshal([]byte(C.GoString(cfg)), &config); err != nil {
return 0, C.CString(err.Error())
return C.CString(bind.BuildResult(err))
}
config.ProductionMode = true
realPort, err := rest.Start(&config)
if err != nil {
return 0, C.CString(err.Error())
}
return realPort, nil
defer C.free(unsafe.Pointer(cfg))
return C.CString(bind.Create(&config))
}
//export Stop
func Stop() {
rest.Stop()
//export Invoke
func Invoke(index int, req *C.char) *C.char {
var request api.Request
if err := json.Unmarshal([]byte(C.GoString(req)), &request); err != nil {
return C.CString(bind.BuildResult(err))
}
defer C.free(unsafe.Pointer(req))
return C.CString(bind.Invoke(index, &request))
}
+7 -12
View File
@@ -3,20 +3,15 @@ package libgopeed
// #cgo LDFLAGS: -static-libstdc++
import "C"
import (
"encoding/json"
"github.com/GopeedLab/gopeed/pkg/rest"
"github.com/GopeedLab/gopeed/pkg/rest/model"
"github.com/GopeedLab/gopeed/bind"
"github.com/GopeedLab/gopeed/pkg/api"
"github.com/GopeedLab/gopeed/pkg/api/model"
)
func Start(cfg string) (int, error) {
var config model.StartConfig
if err := json.Unmarshal([]byte(cfg), &config); err != nil {
return 0, err
}
config.ProductionMode = true
return rest.Start(&config)
func Create(config *model.StartConfig) string {
return bind.Create(config)
}
func Stop() {
rest.Stop()
func Invoke(index int, request *api.Request) string {
return bind.Invoke(index, request)
}