2023-04-23 10:24:11 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"one-api/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Relay(c *gin.Context) {
|
|
|
|
channelType := c.GetInt("channel")
|
2023-04-23 11:19:43 +00:00
|
|
|
baseURL := common.ChannelBaseURLs[channelType]
|
|
|
|
req, err := http.NewRequest(c.Request.Method, fmt.Sprintf("%s%s", baseURL, c.Request.URL.String()), c.Request.Body)
|
2023-04-23 10:24:11 +00:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"error": gin.H{
|
|
|
|
"message": err.Error(),
|
|
|
|
"type": "one_api_error",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.Header = c.Request.Header.Clone()
|
2023-04-23 11:19:43 +00:00
|
|
|
// Fix HTTP Decompression failed
|
|
|
|
// https://github.com/stoplightio/prism/issues/1064#issuecomment-824682360
|
|
|
|
req.Header.Del("Accept-Encoding")
|
2023-04-23 10:24:11 +00:00
|
|
|
client := &http.Client{}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"error": gin.H{
|
|
|
|
"message": err.Error(),
|
|
|
|
"type": "one_api_error",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for k, v := range resp.Header {
|
|
|
|
c.Writer.Header().Set(k, v[0])
|
|
|
|
}
|
|
|
|
_, err = io.Copy(c.Writer, resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"error": gin.H{
|
|
|
|
"message": err.Error(),
|
|
|
|
"type": "one_api_error",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|