-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
156ed44
commit 953d94b
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,15 @@ | ||
# openapi-scf-proxy-go | ||
This repository is inspired by [Ice-Hazymoon/openai-scf-proxy](https://github.com/Ice-Hazymoon/openai-scf-proxy). | ||
|
||
## 腾讯云函数SCF服务的设置 | ||
添加环境变量: | ||
* ProxyHostURL=https://api.openai.com | ||
* ProxyListenOn=0.0.0.0:9000 | ||
|
||
编译压缩命令: | ||
```shell | ||
GOOS=linux GOARCH=amd64 go build -o main main.go | ||
zip main.zip main scf_bootstrap | ||
``` | ||
|
||
函数服务部署成功后,通过APIGW访问。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module openapi-scf-proxy-go | ||
|
||
go 1.19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"os" | ||
|
||
pp "openapi-scf-proxy-go/proxy" | ||
) | ||
|
||
var ( | ||
Target string | ||
ListenOn string | ||
) | ||
|
||
func init() { | ||
// TargetHostURL=https://api.openai.com | ||
Target = os.Getenv("ProxyHostURL") | ||
// ProxyListenOn=0.0.0.0:9000 | ||
ListenOn = os.Getenv("ProxyListenOn") | ||
} | ||
|
||
func main() { | ||
if len(Target) == 0 || len(ListenOn) == 0 { | ||
panic("ProxyHostURL or ProxyListenOn is null.") | ||
} | ||
|
||
// initialize a reverse proxy and pass the actual backend server url here | ||
proxy, err := pp.New(Target) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// handle all requests to your server using the proxy | ||
http.HandleFunc("/", pp.RequestHandler(proxy)) | ||
log.Fatal(http.ListenAndServe(ListenOn, nil)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package proxy | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"net/http/httputil" | ||
"net/url" | ||
) | ||
|
||
// New takes target host and creates a reverse proxy | ||
func New(targetHost string) (*httputil.ReverseProxy, error) { | ||
url, err := url.Parse(targetHost) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
proxy := httputil.NewSingleHostReverseProxy(url) | ||
|
||
originalDirector := proxy.Director | ||
proxy.Director = func(req *http.Request) { | ||
originalDirector(req) | ||
modifyRequest(req, url.Host) | ||
} | ||
|
||
proxy.ModifyResponse = modifyResponse() | ||
proxy.ErrorHandler = errorHandler() | ||
return proxy, nil | ||
} | ||
|
||
func modifyRequest(req *http.Request, targetHost string) { | ||
req.Host = targetHost | ||
} | ||
|
||
func errorHandler() func(http.ResponseWriter, *http.Request, error) { | ||
return func(w http.ResponseWriter, req *http.Request, err error) { | ||
if err != nil { | ||
fmt.Printf("Got error while modifying response: %v \n", err) | ||
} | ||
return | ||
} | ||
} | ||
|
||
func modifyResponse() func(*http.Response) error { | ||
return func(resp *http.Response) error { | ||
resp.Header.Set("x-proxy-by", "go") | ||
log.Println(resp.StatusCode) | ||
return nil | ||
} | ||
} | ||
|
||
// RequestHandler handles the http request using proxy | ||
func RequestHandler(proxy *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
proxy.ServeHTTP(w, r) | ||
} | ||
} |