Ai
1 Star 0 Fork 0

Clannad/webkit

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 1.71 KB
一键复制 编辑 原始数据 按行查看 历史
yokaqa 提交于 2024-10-18 10:22 +08:00 . 111
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package main
import (
"net/http"
"net/http/httputil"
"net/url"
"gitee.com/clannad_sk/webkit/v2/frame/g"
"gitee.com/clannad_sk/webkit/v2/net/ghttp"
)
const (
PortOfServerBackend = 8198
PortOfServerProxy = 8199
UpStream = "http://127.0.0.1:8198"
)
// StartServerBackend starts `backend`: A simple http server for demo.
func StartServerBackend() {
s := g.Server("backend")
s.BindHandler("/*", func(r *ghttp.Request) {
r.Response.Write("response from server backend")
})
s.BindHandler("/user/1", func(r *ghttp.Request) {
r.Response.Write("user info from server backend")
})
s.SetPort(PortOfServerBackend)
s.Run()
}
// StartServerProxy starts `proxy`:
// All requests to `proxy` of route `/proxy/*` are directly redirected to `backend`.
func StartServerProxy() {
s := g.Server("proxy")
u, _ := url.Parse(UpStream)
proxy := httputil.NewSingleHostReverseProxy(u)
proxy.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, e error) {
writer.WriteHeader(http.StatusBadGateway)
}
s.BindHandler("/proxy/*url", func(r *ghttp.Request) {
var (
originalPath = r.Request.URL.Path
proxyToPath = "/" + r.Get("url").String()
)
r.Request.URL.Path = proxyToPath
g.Log().Infof(r.Context(), `proxy:"%s" -> backend:"%s"`, originalPath, proxyToPath)
r.MakeBodyRepeatableRead(false)
proxy.ServeHTTP(r.Response.Writer, r.Request)
})
s.SetPort(PortOfServerProxy)
s.Run()
}
func main() {
go StartServerBackend()
StartServerProxy()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/clannad_sk/webkit.git
git@gitee.com:clannad_sk/webkit.git
clannad_sk
webkit
webkit
997199055094

搜索帮助