Skip to content

Commit

Permalink
add readme for sse
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyenought committed Oct 9, 2023
1 parent a9beebb commit 5fa2415
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ require (
github.com/gorilla/securecookie v1.1.1 // indirect
github.com/gorilla/sessions v1.2.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 // indirect
github.com/hertz-contrib/sse v0.0.1 // indirect
github.com/iancoleman/strcase v0.2.0 // indirect
github.com/jhump/protoreflect v1.8.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
2 changes: 2 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
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
93 changes: 93 additions & 0 deletions reverseproxy/sse/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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/cloudwego/hertz/pkg/common/hlog"
"github.com/hertz-contrib/reverseproxy"
"github.com/hertz-contrib/sse"
)

var proxy *reverseproxy.ReverseProxy

func main() {
h := server.Default(server.WithHostPorts("127.0.0.1:8000"))
h2 := server.Default(server.WithHostPorts("127.0.0.1:8001"))

proxy, _ = reverseproxy.NewSingleHostReverseProxy("http://127.0.0.1:8001",
client.WithResponseBodyStream(true),
)

// Use http://127.0.0.1:8080 access this handler
h.GET("/", func(ctx context.Context, c *app.RequestContext) {
hlog.Info(c.Request.Header.Get("Last-Event-ID"))
c.JSON(200, map[string]string{
"message": "pong",
})
})

h.GET("/proxy", proxyToServer2)
h.GET("/proxy/:path", proxyToServer2)

// Use http://127.0.0.1:8080/proxy/sse access this handler
h2.GET("/sse", func(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 t := range time.NewTicker(1 * time.Second).C {
event := &sse.Event{
Event: "timestamp",
Data: []byte(t.Format(time.RFC3339)),
}
err := s.Publish(event)
if err != nil {
return
}
}
})

// Use http://127.0.0.1:8080/proxy access this handler
h2.GET("/", func(ctx context.Context, c *app.RequestContext) {
if c.Request.Header.Get("Secret") != "123456" {
c.Abort()
return
}

c.JSON(200, map[string]string{
"message": "proxy success",
})
})

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

func proxyToServer2(ctx context.Context, c *app.RequestContext) {
path := c.Param("path")
hlog.Info("path: ", path)
c.Request.Header.Set("Secret", "123456")
c.Request.URI().SetPath(path)
proxy.ServeHTTP(ctx, c)
c.Abort()
}

0 comments on commit 5fa2415

Please sign in to comment.