feat: add metric feature support in goframe (#3138)

This commit is contained in:
John Guo
2024-03-24 21:18:30 +08:00
committed by GitHub
parent 313d9d138f
commit 8669512f42
103 changed files with 6056 additions and 751 deletions

View File

@ -16,28 +16,28 @@ import (
)
const (
PortOfServer1 = 8198
PortOfServer2 = 8199
UpStream = "http://127.0.0.1:8198"
PortOfServerBackend = 8198
PortOfServerProxy = 8199
UpStream = "http://127.0.0.1:8198"
)
// StartServer1 starts Server1: A simple http server for demo.
func StartServer1() {
s := g.Server(1)
// 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 1")
r.Response.Write("response from server backend")
})
s.BindHandler("/user/1", func(r *ghttp.Request) {
r.Response.Write("user info from server 1")
r.Response.Write("user info from server backend")
})
s.SetPort(PortOfServer1)
s.SetPort(PortOfServerBackend)
s.Run()
}
// StartServer2 starts Server2:
// All requests to Server2 are directly redirected to Server1.
func StartServer2() {
s := g.Server(2)
// 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) {
@ -49,15 +49,15 @@ func StartServer2() {
proxyToPath = "/" + r.Get("url").String()
)
r.Request.URL.Path = proxyToPath
g.Log().Infof(r.Context(), `server2:"%s" -> server1:"%s"`, originalPath, proxyToPath)
g.Log().Infof(r.Context(), `proxy:"%s" -> backend:"%s"`, originalPath, proxyToPath)
r.MakeBodyRepeatableRead(false)
proxy.ServeHTTP(r.Response.Writer.RawWriter(), r.Request)
proxy.ServeHTTP(r.Response.Writer, r.Request)
})
s.SetPort(PortOfServer2)
s.SetPort(PortOfServerProxy)
s.Run()
}
func main() {
go StartServer1()
StartServer2()
go StartServerBackend()
StartServerProxy()
}