dxc před 1 rokem
rodič
revize
cd52746666

+ 1 - 0
.gitignore

@@ -1,3 +1,4 @@
 /pkg/silk2audio/silk/libSKP_SILK_SDK.a
 /transcoder.db
 /transcoder.db.lock
+/logs/

+ 7 - 1
etc/transcoder.yaml

@@ -1,8 +1,14 @@
-Name: transcoder
+Name: &Name transcoder
 Host: 0.0.0.0
 Port: 8888
 Timeout: 60000
 
+Log:
+  ServiceName: *Name
+  Mode: "file"
+  Rotation: "daily"
+  Encoding: "plain"
+
 QiNiuConf:
   AccessKey: "B81Gsvry2StqKVE3txS-7v9GBBfqykC9zhebmxnW"
   SecretKey: "YEZJuYcdeF7vRvzffxpopAVR-jMPZg9pZ-4IKTVW"

+ 5 - 0
internal/handler/routes.go

@@ -22,6 +22,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 				Path:    "/v1/transcoder/callback",
 				Handler: TranscoderCallbackHandler(serverCtx),
 			},
+			{
+				Method:  http.MethodPost,
+				Path:    "/v1/transcoder/test",
+				Handler: TestCallbackHandler(serverCtx),
+			},
 		},
 	)
 }

+ 28 - 0
internal/handler/testcallbackhandler.go

@@ -0,0 +1,28 @@
+package handler
+
+import (
+	"net/http"
+
+	"audio_transcoder/internal/logic"
+	"audio_transcoder/internal/svc"
+	"audio_transcoder/internal/types"
+	"github.com/zeromicro/go-zero/rest/httpx"
+)
+
+func TestCallbackHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.OriginDataResponse
+		if err := httpx.Parse(r, &req); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := logic.NewTestCallbackLogic(r.Context(), svcCtx)
+		err := l.TestCallback(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.Ok(w)
+		}
+	}
+}

+ 30 - 0
internal/logic/testcallbacklogic.go

@@ -0,0 +1,30 @@
+package logic
+
+import (
+	"context"
+
+	"audio_transcoder/internal/svc"
+	"audio_transcoder/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type TestCallbackLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewTestCallbackLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TestCallbackLogic {
+	return &TestCallbackLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx,
+	}
+}
+
+func (l *TestCallbackLogic) TestCallback(req *types.OriginDataResponse) error {
+	//测试接收回调数据接口
+	logx.Info("接收数据: ", req)
+	return nil
+}

+ 9 - 2
internal/logic/transcodercallbacklogic.go

@@ -7,6 +7,7 @@ import (
 	"encoding/json"
 	"errors"
 	"github.com/google/uuid"
+	"regexp"
 
 	"github.com/zeromicro/go-zero/core/logx"
 )
@@ -26,8 +27,14 @@ func NewTranscoderCallbackLogic(ctx context.Context, svcCtx *svc.ServiceContext)
 }
 
 func (l *TranscoderCallbackLogic) TranscoderCallback(req *types.CallbackRequest) (resp *types.CallbackResponse, err error) {
-	if req.Path == "" {
-		err = errors.New("path is empty")
+	if req.Path == "" || req.CallbackUrl == "" {
+		err = errors.New("path or callback_url is empty")
+		return
+	}
+	//校验req.Path是否符合url的正则格式
+	var urlRegexp = regexp.MustCompile(`^(?:(http|https|ftp):\/\/)?((|[\w-]+\.)+[a-z0-9]+)(?:(\/[^/?#]+)*)?(\?[^#]+)?(#.+)?$`)
+	if !urlRegexp.MatchString(req.Path) || urlRegexp.MatchString(req.CallbackUrl) {
+		err = errors.New("path or callback_url is not url")
 		return
 	}
 	bts, err := json.Marshal(req)

+ 1 - 1
job/job.go

@@ -38,6 +38,7 @@ func AutoTranscoder(ctx *svc.ServiceContext) {
 		}
 		//业务处理
 		resp, err := transcoder(ctx, &req.CallbackRequest)
+		logx.Info("转码结果 data:", resp)
 		if err != nil {
 			logx.Error(err)
 			_ = sdk.Delete(key)
@@ -125,7 +126,6 @@ func transcoder(svcCtx *svc.ServiceContext, req *types.CallbackRequest) (resp *t
 	var wg sync.WaitGroup
 	wg.Add(2)
 	//构建返回数据
-	resp = &types.OriginDataResponse{}
 	var err1, err2 error
 	//上传录音
 	go func() {

+ 3 - 0
transcoder.api

@@ -28,4 +28,7 @@ service transcoder {
 
 	@handler TranscoderCallbackHandler
 	post /v1/transcoder/callback(Request) returns (CallbackResponse)
+
+	@handler TestCallbackHandler
+	post /v1/transcoder/test(OriginDataResponse)
 }