Skip to content

Commit

Permalink
feat: add sse with reverseproxy example (#121)
Browse files Browse the repository at this point in the history
* feat: add sse with reverse proxy

* update

* update

* update
  • Loading branch information
Skyenought authored Oct 10, 2023
1 parent a9beebb commit 4c998c1
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions reverseproxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
71 changes: 71 additions & 0 deletions reverseproxy/sse/main.go
Original file line number Diff line number Diff line change
@@ -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"
"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"
)

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", proxyToServer)
h.GET("/proxy/:path", proxyToServer)

h2 := server.Default(server.WithHostPorts("127.0.0.1:8001"))
h2.GET("/sse", sseHandler)

go h.Spin()
h2.Spin()
}

func proxyToServer(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)
}

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)
}
}
52 changes: 52 additions & 0 deletions reverseproxy/sse/readme.md
Original file line number Diff line number Diff line change
@@ -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
```

0 comments on commit 4c998c1

Please sign in to comment.