This commit is contained in:
Zheng Kai
2023-03-31 10:12:18 +08:00
parent af429a393a
commit 8e72c62281
17 changed files with 229 additions and 61 deletions

46
server/src/core/fetch.go Normal file
View File

@@ -0,0 +1,46 @@
package core
import (
"bytes"
"io"
"net/http"
"net/url"
"project/config"
"project/pb"
"project/zj"
"time"
)
func fetchRemote(r *pb.Req) (ab []byte, err error) {
u, err := url.Parse(config.RemoteAPI)
if err != nil {
zj.W(`url fail`, config.RemoteAPI, err)
return
}
u.Path = r.Path
req, err := http.NewRequest(r.Method, u.String(), bytes.NewReader(r.Body))
if err != nil {
return
}
req.Header.Set(`Content-Type`, `application/json`)
req.Header.Set(`Authorization`, `Bearer `+config.OpenAIKey)
client := &http.Client{
Timeout: 30 * time.Second,
}
rsp, err := client.Do(req)
if err != nil {
return
}
for k, v := range rsp.Header {
zj.J(k, v)
}
ab, err = io.ReadAll(rsp.Body)
rsp.Body.Close()
return
}