1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-05 03:07:38 +00:00

add accesslog+proxy example

This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-09-19 17:47:44 +03:00
parent fc2aada3c7
commit a04a6b5011
9 changed files with 199 additions and 28 deletions

View File

@@ -82,6 +82,7 @@
* [Listen and render Logs to a Client](logging/request-logger/accesslog-broker/main.go)
* [The CSV Formatter](logging/request-logger/accesslog-csv/main.go)
* [Create your own Formatter](logging/request-logger/accesslog-formatter/main.go)
* [Root and Proxy AccessLog instances](logging/request-logger/accesslog-proxy/main.go)
<!-- * [Log Requests to a JSON File](logging/request-logger/request-logger-file-json/main.go) -->
* API Documentation
* [Yaag](apidoc/yaag/main.go)

View File

@@ -0,0 +1,86 @@
/*Package main is a proxy + accesslog example.
In this example we will make a small proxy which listens requests on "/proxy/+path".
With two accesslog instances, one for the main application and one for the /proxy/ requests.
Of cource, you could a single accesslog for the whole application, but for the sake of the example
let's log them separately.
We will make use of iris.StripPrefix and host.ProxyHandler.*/
package main
import (
"net/url"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/core/host"
"github.com/kataras/iris/v12/middleware/accesslog"
"github.com/kataras/iris/v12/middleware/recover"
)
func main() {
app := iris.New()
app.Get("/", index)
ac := accesslog.File("access.log")
defer ac.Close()
ac.Async = true
ac.RequestBody = true
ac.ResponseBody = true
ac.BytesReceived = false
ac.BytesSent = false
app.UseRouter(ac.Handler)
app.UseRouter(recover.New())
proxy := app.Party("/proxy")
{
acProxy := accesslog.File("proxy_access.log")
defer acProxy.Close()
acProxy.Async = true
acProxy.RequestBody = true
acProxy.ResponseBody = true
acProxy.BytesReceived = false
acProxy.BytesSent = false
// Unlike Use, the UseRouter method replaces any duplications automatically.
// (see UseOnce for the same behavior on Use).
// Therefore, this statement removes the parent's accesslog and registers this new one.
proxy.UseRouter(acProxy.Handler)
proxy.UseRouter(recover.New())
proxy.Use(func(ctx iris.Context) {
ctx.CompressReader(true)
ctx.Next()
})
/* Listen for specific proxy paths:
// Listen on "/proxy" for "http://localhost:9090/read-write"
proxy.Any("/", iris.StripPrefix("/proxy",
newProxyHandler("http://localhost:9090/read-write")))
*/
// You can register an access log only for proxied requests, e.g. proxy_access.log:
// proxy.UseRouter(ac2.Handler)
// Listen for any proxy path.
// Proxies the "/proxy/+$path" to "http://localhost:9090/$path".
proxy.Any("/{p:path}", iris.StripPrefix("/proxy",
newProxyHandler("http://localhost:9090")))
}
// $ go run target/main.go
// open new terminal
// $ go run main.go
app.Listen(":8080")
}
func index(ctx iris.Context) {
ctx.WriteString("OK")
}
func newProxyHandler(proxyURL string) iris.Handler {
target, err := url.Parse(proxyURL)
if err != nil {
panic(err)
}
reverseProxy := host.ProxyHandler(target)
return iris.FromStd(reverseProxy)
}

View File

@@ -0,0 +1,32 @@
package main
import "github.com/kataras/iris/v12"
// The target server, can be written using any programming language and any web framework, of course.
func main() {
app := iris.New()
app.Logger().SetLevel("debug")
// Just a test route which reads some data and responds back with json.
app.Post("/read-write", readWriteHandler)
app.Get("/get", getHandler)
// The target ip:port.
app.Listen(":9090")
}
func readWriteHandler(ctx iris.Context) {
var req interface{}
ctx.ReadBody(&req)
ctx.JSON(iris.Map{
"message": "OK",
"request": req,
})
}
func getHandler(ctx iris.Context) {
// ctx.CompressWriter(true)
ctx.WriteString("Compressed data")
}

View File

@@ -17,29 +17,35 @@ func TestReadHeaders(t *testing.T) {
headers map[string]string
code int
body string
regex bool
}{
{headers: map[string]string{
"X-Request-Id": "373713f0-6b4b-42ea-ab9f-e2e04bc38e73",
"Authentication": "Bearer my-token",
}, code: 200, body: expectedOKBody},
}, code: 200, body: expectedOKBody, regex: false},
{headers: map[string]string{
"x-request-id": "373713f0-6b4b-42ea-ab9f-e2e04bc38e73",
"authentication": "Bearer my-token",
}, code: 200, body: expectedOKBody},
}, code: 200, body: expectedOKBody, regex: false},
{headers: map[string]string{
"X-Request-ID": "373713f0-6b4b-42ea-ab9f-e2e04bc38e73",
"X-Request-Id": "373713f0-6b4b-42ea-ab9f-e2e04bc38e73",
"Authentication": "Bearer my-token",
}, code: 200, body: expectedOKBody},
}, code: 200, body: expectedOKBody, regex: false},
{headers: map[string]string{
"Authentication": "Bearer my-token",
}, code: 500, body: "X-Request-Id is empty"},
}, code: 500, body: "X-Request-Id is empty", regex: false},
{headers: map[string]string{
"X-Request-ID": "373713f0-6b4b-42ea-ab9f-e2e04bc38e73",
}, code: 500, body: "Authentication is empty"},
{headers: map[string]string{}, code: 500, body: "X-Request-Id is empty (and 1 other error)"},
"X-Request-Id": "373713f0-6b4b-42ea-ab9f-e2e04bc38e73",
}, code: 500, body: "Authentication is empty", regex: false},
{headers: map[string]string{}, code: 500, body: ".*\\(and 1 other error\\)$", regex: true},
}
for _, tt := range tests {
e.GET("/").WithHeaders(tt.headers).Expect().Status(tt.code).Body().Equal(tt.body)
te := e.GET("/").WithHeaders(tt.headers).Expect().Status(tt.code).Body()
if tt.regex {
te.Match(tt.body)
} else {
te.Equal(tt.body)
}
}
}