From 9e06618e52b0d35528b2d40e8a622f327f79a817 Mon Sep 17 00:00:00 2001 From: Skyenought Date: Mon, 9 Oct 2023 22:34:31 +0800 Subject: [PATCH 1/4] feat: add sse with reverse proxy --- go.mod | 1 + go.sum | 6 ++++ reverseproxy/README.md | 4 +-- reverseproxy/sse/main.go | 71 ++++++++++++++++++++++++++++++++++++++ reverseproxy/sse/readme.md | 52 ++++++++++++++++++++++++++++ 5 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 reverseproxy/sse/main.go create mode 100644 reverseproxy/sse/readme.md diff --git a/go.mod b/go.mod index e4939b3e..389e2796 100644 --- a/go.mod +++ b/go.mod @@ -24,6 +24,7 @@ require ( github.com/hertz-contrib/requestid v1.1.0 github.com/hertz-contrib/reverseproxy v0.0.0-20220907134658-6a05798e1cc5 github.com/hertz-contrib/sessions v1.0.2 + github.com/hertz-contrib/sse v0.0.1 github.com/hertz-contrib/tracer v0.0.0-20220601062646-788b1565bdab github.com/kitex-contrib/obs-opentelemetry v0.2.3 github.com/kitex-contrib/obs-opentelemetry/logging/logrus v0.0.0-20230530060140-c76e27f58391 diff --git a/go.sum b/go.sum index 905ea5b5..9c19647f 100644 --- a/go.sum +++ b/go.sum @@ -423,6 +423,8 @@ github.com/hertz-contrib/reverseproxy v0.0.0-20220907134658-6a05798e1cc5 h1:kK7l github.com/hertz-contrib/reverseproxy v0.0.0-20220907134658-6a05798e1cc5/go.mod h1:uq9j5/W/pFmkYZ4a5Lvn0xzVlO9mhZAtlcddRMGm7kc= github.com/hertz-contrib/sessions v1.0.2 h1:HKwL5Mo6zkdLfP2LGROuFREsZzEjF/+LHDO7txIM1ZU= github.com/hertz-contrib/sessions v1.0.2/go.mod h1:EpGBrsiNwLP64wuZG1dn2KnbXQRdwu8+UHb6mYeU7aw= +github.com/hertz-contrib/sse v0.0.1 h1:eP3YB/Sd20YBKPYIukDt2akHVRLYq/XTYsjiv0w417I= +github.com/hertz-contrib/sse v0.0.1/go.mod h1:hCL17JP8wGf4l3zvbkSdwtYV+3Ikdu3VvpTdeOKM2uE= github.com/hertz-contrib/tracer v0.0.0-20220601062646-788b1565bdab h1:Cp4ka30wJ+yOnBKNyGZSQp+G83NSOyE1jtTA3J7u0Ws= github.com/hertz-contrib/tracer v0.0.0-20220601062646-788b1565bdab/go.mod h1:DDjNWpsO4B1fsYscviQEAhET8PzHOpowaPbuQfNpLmY= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= @@ -606,6 +608,8 @@ github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1 github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5mo= github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4= +github.com/r3labs/sse/v2 v2.10.0 h1:hFEkLLFY4LDifoHdiCN/LlGBAdVJYsANaLqNYa1l/v0= +github.com/r3labs/sse/v2 v2.10.0/go.mod h1:Igau6Whc+F17QUgML1fYe1VPZzTV6EMCnYktEmkNJ7I= github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc= github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= @@ -1163,6 +1167,8 @@ google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= +gopkg.in/cenkalti/backoff.v1 v1.1.0 h1:Arh75ttbsvlpVA7WtVpH4u9h6Zl46xuptxqLxPiSo4Y= +gopkg.in/cenkalti/backoff.v1 v1.1.0/go.mod h1:J6Vskwqd+OMVJl8C33mmtxTBs2gyzfv7UDAkHu8BrjI= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/reverseproxy/README.md b/reverseproxy/README.md index c5f590ec..496f1146 100644 --- a/reverseproxy/README.md +++ b/reverseproxy/README.md @@ -7,5 +7,5 @@ You can learn about how to custom reverseproxy for hertz: * [modify_response](./modify_response): how to modify server response * [discovery](./discovery): use nacos as example and more information refer to [registry](https://github.com/hertz-contrib/registry) * [tls](./tls): how to config reverseproxy for tls protocol server -* [use_middleware](./use_middleware): how to use middware with reverseproxy - +* [use_middleware](./use_middleware): how to use middleware with reverseproxy +* [sse](./sse): how to use sse with reverseproxy diff --git a/reverseproxy/sse/main.go b/reverseproxy/sse/main.go new file mode 100644 index 00000000..9f2cbaf8 --- /dev/null +++ b/reverseproxy/sse/main.go @@ -0,0 +1,71 @@ +/* + * Copyright 2022 CloudWeGo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package main + +import ( + "context" + "github.com/cloudwego/hertz/pkg/app" + "github.com/cloudwego/hertz/pkg/app/client" + "github.com/cloudwego/hertz/pkg/app/server" + "github.com/hertz-contrib/reverseproxy" + "github.com/hertz-contrib/sse" + "net/http" + "time" +) + +var proxy *reverseproxy.ReverseProxy + +func main() { + proxy, _ = reverseproxy.NewSingleHostReverseProxy("http://127.0.0.1:8001", + client.WithResponseBodyStream(true), + ) + + h := server.Default(server.WithHostPorts("127.0.0.1:8080")) + h.GET("/proxy", proxyToServer2) + h.GET("/proxy/:path", proxyToServer2) + + h2 := server.Default(server.WithHostPorts("127.0.0.1:8001")) + h2.GET("/sse", sseHandler) + + go h.Spin() + h2.Spin() +} + +func proxyToServer2(ctx context.Context, c *app.RequestContext) { + path := c.Param("path") + c.Request.Header.Set("Secret", "123456") + c.Request.URI().SetPath(path) + proxy.ServeHTTP(ctx, c) + c.Abort() +} + +func sseHandler(ctx context.Context, c *app.RequestContext) { + // you must set status code and response headers before first render call + c.Response.Header.Set("X-Accel-Buffering", "no") + c.SetStatusCode(http.StatusOK) + s := sse.NewStream(c) + for i := 0; i < 10; i++ { + event := &sse.Event{ + Event: "timestamp", + Data: []byte(time.Now().Format(time.RFC3339)), + } + err := s.Publish(event) + if err != nil { + return + } + time.Sleep(200 * time.Millisecond) + } +} diff --git a/reverseproxy/sse/readme.md b/reverseproxy/sse/readme.md new file mode 100644 index 00000000..846f360b --- /dev/null +++ b/reverseproxy/sse/readme.md @@ -0,0 +1,52 @@ +## sse + +### Introduction + +This example shows how to use sse with reverseproxy. + +### Usage + +* Start server + +```bash +go run main.go +``` + +* Test +```bash +curl -N --location http://localhost:8080/proxy/sse +``` + +Result: +```bash +> curl -N --location http://localhost:8080/proxy/sse +event:timestamp +data:2023-10-09T22:32:14+08:00 + +event:timestamp +data:2023-10-09T22:32:14+08:00 + +event:timestamp +data:2023-10-09T22:32:14+08:00 + +event:timestamp +data:2023-10-09T22:32:14+08:00 + +event:timestamp +data:2023-10-09T22:32:14+08:00 + +event:timestamp +data:2023-10-09T22:32:15+08:00 + +event:timestamp +data:2023-10-09T22:32:15+08:00 + +event:timestamp +data:2023-10-09T22:32:15+08:00 + +event:timestamp +data:2023-10-09T22:32:15+08:00 + +event:timestamp +data:2023-10-09T22:32:15+08:00 +``` From 50f3030a1efcdfd319f196f5ee6a6bd569bda111 Mon Sep 17 00:00:00 2001 From: Skyenought Date: Mon, 9 Oct 2023 22:36:36 +0800 Subject: [PATCH 2/4] update --- reverseproxy/sse/main.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/reverseproxy/sse/main.go b/reverseproxy/sse/main.go index 9f2cbaf8..62c6e600 100644 --- a/reverseproxy/sse/main.go +++ b/reverseproxy/sse/main.go @@ -17,13 +17,14 @@ package main import ( "context" + "net/http" + "time" + "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/app/client" "github.com/cloudwego/hertz/pkg/app/server" "github.com/hertz-contrib/reverseproxy" "github.com/hertz-contrib/sse" - "net/http" - "time" ) var proxy *reverseproxy.ReverseProxy From 63375a1135e7915f36b6ede69d299d01a6637857 Mon Sep 17 00:00:00 2001 From: Skyenought Date: Tue, 10 Oct 2023 00:39:28 +0800 Subject: [PATCH 3/4] update --- reverseproxy/sse/main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/reverseproxy/sse/main.go b/reverseproxy/sse/main.go index 62c6e600..2df13529 100644 --- a/reverseproxy/sse/main.go +++ b/reverseproxy/sse/main.go @@ -35,8 +35,8 @@ func main() { ) h := server.Default(server.WithHostPorts("127.0.0.1:8080")) - h.GET("/proxy", proxyToServer2) - h.GET("/proxy/:path", proxyToServer2) + h.GET("/proxy", proxyToServer) + h.GET("/proxy/:path", proxyToServer) h2 := server.Default(server.WithHostPorts("127.0.0.1:8001")) h2.GET("/sse", sseHandler) @@ -45,7 +45,7 @@ func main() { h2.Spin() } -func proxyToServer2(ctx context.Context, c *app.RequestContext) { +func proxyToServer(ctx context.Context, c *app.RequestContext) { path := c.Param("path") c.Request.Header.Set("Secret", "123456") c.Request.URI().SetPath(path) From 57a31e1ae06936c7e769ddb9e25c189606addcc1 Mon Sep 17 00:00:00 2001 From: Skyenought Date: Tue, 10 Oct 2023 11:48:20 +0800 Subject: [PATCH 4/4] update --- reverseproxy/sse/main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/reverseproxy/sse/main.go b/reverseproxy/sse/main.go index 2df13529..ad196ca2 100644 --- a/reverseproxy/sse/main.go +++ b/reverseproxy/sse/main.go @@ -50,7 +50,6 @@ func proxyToServer(ctx context.Context, c *app.RequestContext) { c.Request.Header.Set("Secret", "123456") c.Request.URI().SetPath(path) proxy.ServeHTTP(ctx, c) - c.Abort() } func sseHandler(ctx context.Context, c *app.RequestContext) {