-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9beebb
commit 5fa2415
Showing
4 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |