mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-07 17:10:29 +00:00
feat: bt improve
This commit is contained in:
1 parent
c6aa83194f
commit
0fc133e46d
28 files changed
+1537
-260
No files matched your search
+54
-10
@@ -15,7 +15,7 @@ func Resolve(w http.ResponseWriter, r *http.Request) {
|
||||
if util.ReadJson(w, r, &req) {
|
||||
resource, err := Downloader.Resolve(&req)
|
||||
if err != nil {
|
||||
util.WriteJson(w, 500, model.NewResultWithMsg(err.Error()))
|
||||
util.WriteJson(w, http.StatusInternalServerError, model.NewResultWithMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
util.WriteJsonOk(w, model.NewResultWithData(resource))
|
||||
@@ -23,11 +23,17 @@ func Resolve(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func CreateTask(w http.ResponseWriter, r *http.Request) {
|
||||
var req model.CreateTaskReq
|
||||
var req model.CreateTask
|
||||
if util.ReadJson(w, r, &req) {
|
||||
taskId, err := Downloader.Create(req.Resource, req.Options)
|
||||
if req.Opts.Connections == 0 {
|
||||
req.Opts.Connections = getServerConfig().Connections
|
||||
}
|
||||
if req.Opts.Path == "" {
|
||||
req.Opts.Path = getServerConfig().DownloadDir
|
||||
}
|
||||
taskId, err := Downloader.Create(req.Res, req.Opts)
|
||||
if err != nil {
|
||||
util.WriteJson(w, 500, model.NewResultWithMsg(err.Error()))
|
||||
util.WriteJson(w, http.StatusInternalServerError, model.NewResultWithMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
util.WriteJsonOk(w, model.NewResultWithData(taskId))
|
||||
@@ -37,14 +43,31 @@ func CreateTask(w http.ResponseWriter, r *http.Request) {
|
||||
func PauseTask(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
taskId := vars["id"]
|
||||
Downloader.Pause(taskId)
|
||||
if err := Downloader.Pause(taskId); err != nil {
|
||||
util.WriteJson(w, http.StatusInternalServerError, model.NewResultWithMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
util.WriteJsonOk(w, nil)
|
||||
}
|
||||
|
||||
func ContinueTask(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
taskId := vars["id"]
|
||||
Downloader.Continue(taskId)
|
||||
if err := Downloader.Continue(taskId); err != nil {
|
||||
util.WriteJson(w, http.StatusInternalServerError, model.NewResultWithMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
util.WriteJsonOk(w, nil)
|
||||
}
|
||||
|
||||
func DeleteTask(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
taskId := vars["id"]
|
||||
force := r.FormValue("force")
|
||||
if err := Downloader.Delete(taskId, force == "true"); err != nil {
|
||||
util.WriteJson(w, http.StatusInternalServerError, model.NewResultWithMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
util.WriteJsonOk(w, nil)
|
||||
}
|
||||
|
||||
@@ -53,7 +76,7 @@ func GetTask(w http.ResponseWriter, r *http.Request) {
|
||||
taskId := vars["id"]
|
||||
task := Downloader.GetTask(taskId)
|
||||
if task == nil {
|
||||
util.WriteJson(w, 404, model.NewResultWithMsg("task not found"))
|
||||
util.WriteJson(w, http.StatusNotFound, model.NewResultWithMsg("task not found"))
|
||||
return
|
||||
}
|
||||
util.WriteJsonOk(w, model.NewResultWithData(task))
|
||||
@@ -62,12 +85,12 @@ func GetTask(w http.ResponseWriter, r *http.Request) {
|
||||
func GetTasks(w http.ResponseWriter, r *http.Request) {
|
||||
status := r.FormValue("status")
|
||||
if status == "" {
|
||||
util.WriteJson(w, 400, model.NewResultWithMsg("param is required: status"))
|
||||
util.WriteJson(w, http.StatusBadRequest, model.NewResultWithMsg("param is required: status"))
|
||||
return
|
||||
}
|
||||
statusArr := strings.Split(status, ",")
|
||||
tasks := Downloader.GetTasks()
|
||||
var result []*download.Task
|
||||
result := make([]*download.Task, 0)
|
||||
for _, task := range tasks {
|
||||
for _, s := range statusArr {
|
||||
if task.Status == base.Status(s) {
|
||||
@@ -75,5 +98,26 @@ func GetTasks(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
}
|
||||
util.WriteJsonOk(w, model.NewResultWithData(tasks))
|
||||
util.WriteJsonOk(w, model.NewResultWithData(result))
|
||||
}
|
||||
|
||||
func GetConfig(w http.ResponseWriter, r *http.Request) {
|
||||
util.WriteJsonOk(w, model.NewResultWithData(getServerConfig()))
|
||||
}
|
||||
|
||||
func PutConfig(w http.ResponseWriter, r *http.Request) {
|
||||
var cfg model.ServerConfig
|
||||
if util.ReadJson(w, r, &cfg) {
|
||||
if err := Downloader.PutConfig(&cfg); err != nil {
|
||||
util.WriteJson(w, http.StatusInternalServerError, model.NewResultWithMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
}
|
||||
util.WriteJsonOk(w, nil)
|
||||
}
|
||||
|
||||
func getServerConfig() *model.ServerConfig {
|
||||
var cfg model.ServerConfig
|
||||
Downloader.GetConfig(&cfg)
|
||||
return &cfg
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package model
|
||||
|
||||
type Storage string
|
||||
|
||||
const (
|
||||
StorageMem Storage = "mem"
|
||||
StorageBolt Storage = "bolt"
|
||||
)
|
||||
|
||||
type StartConfig struct {
|
||||
Network string `json:"network"`
|
||||
Address string `json:"address"`
|
||||
Storage Storage `json:"storage"`
|
||||
StorageDir string `json:"storageDir"`
|
||||
RefreshInterval int `json:"refreshInterval"`
|
||||
}
|
||||
|
||||
func (cfg *StartConfig) Init() *StartConfig {
|
||||
if cfg.Network == "" {
|
||||
cfg.Network = "tcp"
|
||||
}
|
||||
if cfg.Storage == "" {
|
||||
cfg.Storage = StorageBolt
|
||||
}
|
||||
if cfg.StorageDir == "" {
|
||||
cfg.StorageDir = "./"
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
// ServerConfig is present in the database
|
||||
type ServerConfig struct {
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
|
||||
Connections int `json:"connections"`
|
||||
DownloadDir string `json:"downloadDir"`
|
||||
|
||||
Extra map[string]any `json:"extra"`
|
||||
}
|
||||
|
||||
func (cfg *ServerConfig) Init() *ServerConfig {
|
||||
if cfg.Host == "" {
|
||||
cfg.Host = "127.0.0.1"
|
||||
}
|
||||
if cfg.Port < 0 {
|
||||
cfg.Port = 0
|
||||
}
|
||||
if cfg.Connections <= 0 {
|
||||
cfg.Connections = 16
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package model
|
||||
|
||||
import "github.com/monkeyWie/gopeed-core/pkg/base"
|
||||
|
||||
type CreateTaskReq struct {
|
||||
Resource *base.Resource `json:"resource"`
|
||||
Options *base.Options `json:"options"`
|
||||
type CreateTask struct {
|
||||
Res *base.Resource `json:"res"`
|
||||
Opts *base.Options `json:"opts"`
|
||||
}
|
||||
+64
-10
@@ -2,23 +2,49 @@ package rest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/monkeyWie/gopeed-core/internal/protocol/bt"
|
||||
fhttp "github.com/monkeyWie/gopeed-core/internal/protocol/http"
|
||||
"github.com/monkeyWie/gopeed-core/pkg/download"
|
||||
"github.com/monkeyWie/gopeed-core/pkg/rest/model"
|
||||
"github.com/monkeyWie/gopeed-core/pkg/util"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var (
|
||||
srv *http.Server
|
||||
|
||||
Downloader = download.NewDownloader(new(fhttp.FetcherBuilder), new(bt.FetcherBuilder))
|
||||
Downloader *download.Downloader
|
||||
)
|
||||
|
||||
func Start(addr string, port int) (int, error) {
|
||||
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", addr, port))
|
||||
func Start(startCfg *model.StartConfig) (int, error) {
|
||||
if startCfg == nil {
|
||||
startCfg = &model.StartConfig{}
|
||||
}
|
||||
startCfg.Init()
|
||||
|
||||
downloadCfg := &download.DownloaderConfig{}
|
||||
if startCfg.Storage == model.StorageBolt {
|
||||
downloadCfg.Storage = download.NewBoltStorage(startCfg.StorageDir)
|
||||
} else {
|
||||
downloadCfg.Storage = download.NewMemStorage()
|
||||
}
|
||||
downloadCfg.RefreshInterval = startCfg.RefreshInterval
|
||||
Downloader = download.NewDownloader(downloadCfg.Init())
|
||||
if err := Downloader.Setup(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
serverConfig := getAndPutServerConfig(startCfg)
|
||||
var address string
|
||||
if startCfg.Network == "unix" {
|
||||
address = startCfg.Address
|
||||
util.SafeRemove(address)
|
||||
} else {
|
||||
address = net.JoinHostPort(serverConfig.Host, strconv.Itoa(serverConfig.Port))
|
||||
}
|
||||
|
||||
listener, err := net.Listen(startCfg.Network, address)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -28,8 +54,11 @@ func Start(addr string, port int) (int, error) {
|
||||
r.Methods(http.MethodPost).Path("/api/v1/tasks").HandlerFunc(CreateTask)
|
||||
r.Methods(http.MethodPut).Path("/api/v1/tasks/{id}/pause").HandlerFunc(PauseTask)
|
||||
r.Methods(http.MethodPut).Path("/api/v1/tasks/{id}/continue").HandlerFunc(ContinueTask)
|
||||
r.Methods(http.MethodDelete).Path("/api/v1/tasks/{id}").HandlerFunc(DeleteTask)
|
||||
r.Methods(http.MethodGet).Path("/api/v1/tasks/{id}").HandlerFunc(GetTask)
|
||||
r.Methods(http.MethodGet).Path("/api/v1/tasks").HandlerFunc(GetTasks)
|
||||
r.Methods(http.MethodGet).Path("/api/v1/config").HandlerFunc(GetConfig)
|
||||
r.Methods(http.MethodPut).Path("/api/v1/config").HandlerFunc(PutConfig)
|
||||
|
||||
srv = &http.Server{Handler: r}
|
||||
go func() {
|
||||
@@ -38,12 +67,37 @@ func Start(addr string, port int) (int, error) {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
return listener.Addr().(*net.TCPAddr).Port, nil
|
||||
if addr, ok := listener.Addr().(*net.TCPAddr); ok {
|
||||
return addr.Port, nil
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func Stop() error {
|
||||
func Stop() {
|
||||
if srv != nil {
|
||||
return srv.Shutdown(context.TODO())
|
||||
if err := srv.Shutdown(context.TODO()); err != nil {
|
||||
// TODO log
|
||||
}
|
||||
}
|
||||
if Downloader != nil {
|
||||
if err := Downloader.Close(); err != nil {
|
||||
// TODO log
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getAndPutServerConfig(startCfg *model.StartConfig) *model.ServerConfig {
|
||||
var serverConfig model.ServerConfig
|
||||
err := Downloader.GetConfig(&serverConfig)
|
||||
// first start
|
||||
if err != nil {
|
||||
if startCfg.Network == "tcp" {
|
||||
host, port, _ := net.SplitHostPort(startCfg.Address)
|
||||
serverConfig.Host = host
|
||||
serverConfig.Port, _ = strconv.Atoi(port)
|
||||
}
|
||||
serverConfig.Init()
|
||||
Downloader.PutConfig(serverConfig)
|
||||
}
|
||||
return &serverConfig
|
||||
}
|
||||
+77
-16
@@ -3,17 +3,20 @@ package rest
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/monkeyWie/gopeed-core/internal/test"
|
||||
"github.com/monkeyWie/gopeed-core/pkg/base"
|
||||
"github.com/monkeyWie/gopeed-core/pkg/download"
|
||||
"github.com/monkeyWie/gopeed-core/pkg/rest/model"
|
||||
"github.com/monkeyWie/gopeed-core/pkg/test"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -21,6 +24,7 @@ var (
|
||||
|
||||
taskReq = &base.Request{}
|
||||
taskRes = &base.Resource{
|
||||
Name: test.BuildName,
|
||||
Req: taskReq,
|
||||
Size: test.BuildSize,
|
||||
Range: true,
|
||||
@@ -32,9 +36,9 @@ var (
|
||||
},
|
||||
},
|
||||
}
|
||||
createReq = &model.CreateTaskReq{
|
||||
Resource: taskRes,
|
||||
Options: &base.Options{
|
||||
createReq = &model.CreateTask{
|
||||
Res: taskRes,
|
||||
Opts: &base.Options{
|
||||
Path: test.Dir,
|
||||
Name: test.DownloadName,
|
||||
Connections: 2,
|
||||
@@ -130,6 +134,28 @@ func TestPauseAndContinueTask(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestDeleteTask(t *testing.T) {
|
||||
doTest(func() {
|
||||
taskId := httpRequestCheckOk[string](http.MethodPost, "/api/v1/tasks", createReq)
|
||||
httpRequestCheckOk[any](http.MethodDelete, "/api/v1/tasks/"+taskId, nil)
|
||||
code, _ := httpRequest[*download.Task](http.MethodGet, "/api/v1/tasks/"+taskId, nil)
|
||||
checkCode(code, http.StatusNotFound)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDeleteTaskForce(t *testing.T) {
|
||||
doTest(func() {
|
||||
taskId := httpRequestCheckOk[string](http.MethodPost, "/api/v1/tasks", createReq)
|
||||
time.Sleep(time.Millisecond * 500)
|
||||
httpRequestCheckOk[any](http.MethodDelete, "/api/v1/tasks/"+taskId+"?force=true", nil)
|
||||
code, _ := httpRequest[*download.Task](http.MethodGet, "/api/v1/tasks/"+taskId, nil)
|
||||
checkCode(code, http.StatusNotFound)
|
||||
if _, err := os.Stat(test.DownloadFile); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Errorf("DeleteTaskForce() got = %v, want %v", err, os.ErrNotExist)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetTasks(t *testing.T) {
|
||||
doTest(func() {
|
||||
var wg sync.WaitGroup
|
||||
@@ -140,30 +166,65 @@ func TestGetTasks(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
httpRequestCheckOk[string](http.MethodPost, "/api/v1/tasks", createReq)
|
||||
httpRequestCheckOk[string](http.MethodPost, fmt.Sprintf("/api/v1/tasks?status=%s,%s",
|
||||
base.DownloadStatusReady, base.DownloadStatusRunning), createReq)
|
||||
code, _ := httpRequest[[]*download.Task](http.MethodGet, "/api/v1/tasks", nil)
|
||||
checkCode(code, 400)
|
||||
checkCode(code, http.StatusBadRequest)
|
||||
|
||||
wg.Wait()
|
||||
r := httpRequestCheckOk[[]*download.Task](http.MethodGet, "/api/v1/tasks?status=done", nil)
|
||||
r := httpRequestCheckOk[[]*download.Task](http.MethodGet, fmt.Sprintf("/api/v1/tasks?status=%s",
|
||||
base.DownloadStatusDone), nil)
|
||||
if r[0].Status != base.DownloadStatusDone {
|
||||
t.Errorf("GetTasks() got = %v, want %v", r[0].Status, base.DownloadStatusDone)
|
||||
}
|
||||
r = httpRequestCheckOk[[]*download.Task](http.MethodGet, fmt.Sprintf("/api/v1/tasks?status=%s,%s",
|
||||
base.DownloadStatusReady, base.DownloadStatusRunning), nil)
|
||||
if len(r) > 0 {
|
||||
t.Errorf("GetTasks() got = %v, want %v", len(r), 0)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetAndPutConfig(t *testing.T) {
|
||||
doTest(func() {
|
||||
cfg := httpRequestCheckOk[*model.ServerConfig](http.MethodGet, "/api/v1/config", nil)
|
||||
|
||||
cfg.Port = 8888
|
||||
cfg.Connections = 32
|
||||
cfg.DownloadDir = "./download"
|
||||
cfg.Extra = map[string]any{
|
||||
"theme": "dark",
|
||||
}
|
||||
httpRequestCheckOk[any](http.MethodPut, "/api/v1/config", cfg)
|
||||
|
||||
newCfg := httpRequestCheckOk[*model.ServerConfig](http.MethodGet, "/api/v1/config", nil)
|
||||
if !reflect.DeepEqual(newCfg, cfg) {
|
||||
t.Errorf("GetAndPutConfig() got = %v, want %v", newCfg, cfg)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func doTest(handler func()) {
|
||||
fileListener := doStart()
|
||||
defer func() {
|
||||
fileListener.Close()
|
||||
Stop()
|
||||
}()
|
||||
taskReq.URL = "http://" + fileListener.Addr().String() + "/" + test.BuildName
|
||||
handler()
|
||||
testFunc := func(storage model.Storage) {
|
||||
fileListener := doStart(storage)
|
||||
defer func() {
|
||||
if err := fileListener.Close(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
Stop()
|
||||
}()
|
||||
defer Downloader.Clear()
|
||||
taskReq.URL = "http://" + fileListener.Addr().String() + "/" + test.BuildName
|
||||
handler()
|
||||
}
|
||||
testFunc(model.StorageMem)
|
||||
testFunc(model.StorageBolt)
|
||||
}
|
||||
|
||||
func doStart() net.Listener {
|
||||
port, err := Start("127.0.0.1", 0)
|
||||
func doStart(storage model.Storage) net.Listener {
|
||||
port, err := Start((&model.StartConfig{
|
||||
Storage: storage,
|
||||
}).Init())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user