Skip to content

Commit

Permalink
v1
Browse files Browse the repository at this point in the history
  • Loading branch information
ch0ngsheng committed Mar 20, 2023
1 parent 156ed44 commit 953d94b
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
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访问。
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module openapi-scf-proxy-go

go 1.19
37 changes: 37 additions & 0 deletions main.go
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))
}
57 changes: 57 additions & 0 deletions proxy/proxy.go
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)
}
}

0 comments on commit 953d94b

Please sign in to comment.