Delete proxy.go
Browse files
proxy.go
DELETED
|
@@ -1,75 +0,0 @@
|
|
| 1 |
-
package main
|
| 2 |
-
|
| 3 |
-
import (
|
| 4 |
-
"fmt"
|
| 5 |
-
"io"
|
| 6 |
-
"log"
|
| 7 |
-
"net/http"
|
| 8 |
-
"os"
|
| 9 |
-
"os/exec"
|
| 10 |
-
)
|
| 11 |
-
|
| 12 |
-
func main() {
|
| 13 |
-
port := os.Getenv("PORT")
|
| 14 |
-
if port == "" {
|
| 15 |
-
port = "7860"
|
| 16 |
-
}
|
| 17 |
-
|
| 18 |
-
// 启动原始服务器
|
| 19 |
-
cmd := exec.Command("./server", "--port", port)
|
| 20 |
-
cmd.Stdout = os.Stdout
|
| 21 |
-
cmd.Stderr = os.Stderr
|
| 22 |
-
if err := cmd.Start(); err != nil {
|
| 23 |
-
log.Fatalf("Failed to start server: %v", err)
|
| 24 |
-
}
|
| 25 |
-
|
| 26 |
-
// 设置代理处理程序
|
| 27 |
-
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
| 28 |
-
if r.URL.Path == "/proxies/v1/api/generate" {
|
| 29 |
-
r.URL.Path = "/proxies/v1/chat/completions"
|
| 30 |
-
}
|
| 31 |
-
proxyRequest(w, r)
|
| 32 |
-
})
|
| 33 |
-
|
| 34 |
-
// 启动代理服务器
|
| 35 |
-
fmt.Printf("Proxy server listening on :%s\n", port)
|
| 36 |
-
log.Fatal(http.ListenAndServe(":"+port, nil))
|
| 37 |
-
}
|
| 38 |
-
|
| 39 |
-
func proxyRequest(w http.ResponseWriter, r *http.Request) {
|
| 40 |
-
// 创建新的请求
|
| 41 |
-
proxyReq, err := http.NewRequest(r.Method, "http://localhost:"+os.Getenv("PORT")+r.URL.String(), r.Body)
|
| 42 |
-
if err != nil {
|
| 43 |
-
http.Error(w, err.Error(), http.StatusInternalServerError)
|
| 44 |
-
return
|
| 45 |
-
}
|
| 46 |
-
|
| 47 |
-
// 复制原始请求的 header
|
| 48 |
-
for name, values := range r.Header {
|
| 49 |
-
for _, value := range values {
|
| 50 |
-
proxyReq.Header.Add(name, value)
|
| 51 |
-
}
|
| 52 |
-
}
|
| 53 |
-
|
| 54 |
-
// 发送请求
|
| 55 |
-
client := &http.Client{}
|
| 56 |
-
resp, err := client.Do(proxyReq)
|
| 57 |
-
if err != nil {
|
| 58 |
-
http.Error(w, err.Error(), http.StatusBadGateway)
|
| 59 |
-
return
|
| 60 |
-
}
|
| 61 |
-
defer resp.Body.Close()
|
| 62 |
-
|
| 63 |
-
// 复制响应 header
|
| 64 |
-
for name, values := range resp.Header {
|
| 65 |
-
for _, value := range values {
|
| 66 |
-
w.Header().Add(name, value)
|
| 67 |
-
}
|
| 68 |
-
}
|
| 69 |
-
|
| 70 |
-
// 设置状态码
|
| 71 |
-
w.WriteHeader(resp.StatusCode)
|
| 72 |
-
|
| 73 |
-
// 复制响应体
|
| 74 |
-
io.Copy(w, resp.Body)
|
| 75 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|