From 446f624a37bb8a72670ef1f538c9bd342e46c6ff Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Wed, 6 Oct 2021 16:13:40 +0800 Subject: [PATCH] feat(http): accept onebot v12 style endpoint disabled currently, enabled in v1.1.0 --- internal/base/feature.go | 6 ++++++ server/http.go | 19 ++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/internal/base/feature.go b/internal/base/feature.go index f27c0a0..810e189 100644 --- a/internal/base/feature.go +++ b/internal/base/feature.go @@ -29,3 +29,9 @@ var ( func nocheck(_ io.ReadSeeker) (bool, string) { return true, "" } + +// todo: enable in v1.1.0 +// onebot v12 feature +const ( + AcceptOneBotV12HTTPEndPoint = false +) diff --git a/server/http.go b/server/http.go index 7eda02e..5f7be02 100644 --- a/server/http.go +++ b/server/http.go @@ -22,6 +22,8 @@ import ( "gopkg.in/yaml.v3" "github.com/Mrs4s/go-cqhttp/coolq" + "github.com/Mrs4s/go-cqhttp/global" + "github.com/Mrs4s/go-cqhttp/internal/base" "github.com/Mrs4s/go-cqhttp/modules/config" ) @@ -115,14 +117,21 @@ func (s *httpServer) ServeHTTP(writer http.ResponseWriter, request *http.Request return } - action := strings.TrimPrefix(request.URL.Path, "/") - action = strings.TrimSuffix(action, "_async") - log.Debugf("HTTPServer接收到API调用: %v", action) - ret := s.api.callAPI(action, &ctx) + var response global.MSG + if base.AcceptOneBotV12HTTPEndPoint && request.URL.Path == "/" { + action := strings.TrimSuffix(ctx.Get("action").Str, "_async") + log.Debugf("HTTPServer接收到API调用: %v", action) + response = s.api.callAPI(action, ctx.Get("params")) + } else { + action := strings.TrimPrefix(request.URL.Path, "/") + action = strings.TrimSuffix(action, "_async") + log.Debugf("HTTPServer接收到API调用: %v", action) + response = s.api.callAPI(action, &ctx) + } writer.Header().Set("Content-Type", "application/json; charset=utf-8") writer.WriteHeader(http.StatusOK) - _ = json.NewEncoder(writer).Encode(ret) + _ = json.NewEncoder(writer).Encode(response) } func checkAuth(req *http.Request, token string) int {