Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-06-17 21:03:18 +02:00
|
|
|
"io"
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12/cache/cfg"
|
|
|
|
"github.com/kataras/iris/v12/cache/client/rule"
|
|
|
|
"github.com/kataras/iris/v12/cache/uri"
|
|
|
|
"github.com/kataras/iris/v12/context"
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ClientHandler is the client-side handler
|
|
|
|
// for each of the cached route paths's response
|
|
|
|
// register one client handler per route.
|
|
|
|
//
|
|
|
|
// it's just calls a remote cache service server/handler,
|
|
|
|
//
|
2022-06-17 21:03:18 +02:00
|
|
|
// which lives on other, external machine.
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
type ClientHandler struct {
|
|
|
|
// bodyHandler the original route's handler
|
|
|
|
bodyHandler context.Handler
|
|
|
|
|
|
|
|
// Rule optional validators for pre cache and post cache actions
|
|
|
|
//
|
|
|
|
// See more at ruleset.go
|
|
|
|
rule rule.Rule
|
|
|
|
|
|
|
|
life time.Duration
|
|
|
|
|
|
|
|
remoteHandlerURL string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClientHandler returns a new remote client handler
|
|
|
|
// which asks the remote handler the cached entry's response
|
|
|
|
// with a GET request, or add a response with POST request
|
|
|
|
// these all are done automatically, users can use this
|
|
|
|
// handler as they use the local.go/NewHandler
|
|
|
|
//
|
|
|
|
// the ClientHandler is useful when user
|
|
|
|
// wants to apply horizontal scaling to the app and
|
|
|
|
// has a central http server which handles
|
|
|
|
func NewClientHandler(bodyHandler context.Handler, life time.Duration, remote string) *ClientHandler {
|
|
|
|
return &ClientHandler{
|
|
|
|
bodyHandler: bodyHandler,
|
|
|
|
rule: DefaultRuleSet,
|
|
|
|
life: life,
|
|
|
|
remoteHandlerURL: remote,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rule sets the ruleset for this handler,
|
|
|
|
// see internal/net/http/ruleset.go for more information.
|
|
|
|
//
|
|
|
|
// returns itself.
|
|
|
|
func (h *ClientHandler) Rule(r rule.Rule) *ClientHandler {
|
|
|
|
if r == nil {
|
|
|
|
// if nothing passed then use the allow-everything rule
|
|
|
|
r = rule.Satisfied()
|
|
|
|
}
|
|
|
|
h.rule = r
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddRule adds a rule in the chain, the default rules are executed first.
|
|
|
|
//
|
|
|
|
// returns itself.
|
|
|
|
func (h *ClientHandler) AddRule(r rule.Rule) *ClientHandler {
|
|
|
|
if r == nil {
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
h.rule = rule.Chained(h.rule, r)
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client is used inside the global Request function
|
|
|
|
// this client is an exported to give you a freedom of change its Transport, Timeout and so on(in case of ssl)
|
|
|
|
var Client = &http.Client{Timeout: cfg.RequestCacheTimeout}
|
|
|
|
|
|
|
|
const (
|
|
|
|
methodGet = "GET"
|
|
|
|
methodPost = "POST"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ServeHTTP , or remote cache client whatever you like, it's the client-side function of the ServeHTTP
|
|
|
|
// sends a request to the server-side remote cache Service and sends the cached response to the frontend client
|
|
|
|
// it is used only when you achieved something like horizontal scaling (separate machines)
|
|
|
|
// look ../remote/remote.ServeHTTP for more
|
|
|
|
//
|
|
|
|
// if cache din't find then it sends a POST request and save the bodyHandler's body to the remote cache.
|
|
|
|
//
|
|
|
|
// It takes 3 parameters
|
|
|
|
// the first is the remote address (it's the address you started your http server which handled by the Service.ServeHTTP)
|
|
|
|
// the second is the handler (or the mux) you want to cache
|
|
|
|
// and the third is the, optionally, cache expiration,
|
|
|
|
// which is used to set cache duration of this specific cache entry to the remote cache service
|
|
|
|
// if <=minimumAllowedCacheDuration then the server will try to parse from "cache-control" header
|
|
|
|
//
|
|
|
|
// client-side function
|
2020-07-10 22:21:09 +02:00
|
|
|
func (h *ClientHandler) ServeHTTP(ctx *context.Context) {
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
// check for deniers, if at least one of them return true
|
|
|
|
// for this specific request, then skip the whole cache
|
|
|
|
if !h.rule.Claim(ctx) {
|
|
|
|
h.bodyHandler(ctx)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
uri := &uri.URIBuilder{}
|
|
|
|
uri.ServerAddr(h.remoteHandlerURL).ClientURI(ctx.Request().URL.RequestURI()).ClientMethod(ctx.Request().Method)
|
|
|
|
|
|
|
|
// set the full url here because below we have other issues, probably net/http bugs
|
|
|
|
request, err := http.NewRequest(methodGet, uri.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
//// println("error when requesting to the remote service: " + err.Error())
|
|
|
|
// somehing very bad happens, just execute the user's handler and return
|
|
|
|
h.bodyHandler(ctx)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// println("GET Do to the remote cache service with the url: " + request.URL.String())
|
|
|
|
response, err := Client.Do(request)
|
|
|
|
|
|
|
|
if err != nil || response.StatusCode == cfg.FailStatus {
|
|
|
|
|
|
|
|
// if not found on cache, then execute the handler and save the cache to the remote server
|
|
|
|
recorder := ctx.Recorder()
|
|
|
|
h.bodyHandler(ctx)
|
|
|
|
|
|
|
|
// check if it's a valid response, if it's not then just return.
|
|
|
|
if !h.rule.Valid(ctx) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// save to the remote cache
|
|
|
|
// we re-create the request for any case
|
|
|
|
body := recorder.Body()[0:]
|
|
|
|
if len(body) == 0 {
|
|
|
|
//// println("Request: len body is zero, do nothing")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
uri.StatusCode(recorder.StatusCode())
|
|
|
|
uri.Lifetime(h.life)
|
|
|
|
uri.ContentType(recorder.Header().Get(cfg.ContentTypeHeader))
|
|
|
|
|
|
|
|
request, err = http.NewRequest(methodPost, uri.String(), bytes.NewBuffer(body)) // yes new buffer every time
|
|
|
|
|
|
|
|
// println("POST Do to the remote cache service with the url: " + request.URL.String())
|
|
|
|
if err != nil {
|
|
|
|
//// println("Request: error on method Post of request to the remote: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// go Client.Do(request)
|
2020-06-21 16:15:28 +02:00
|
|
|
resp, err := Client.Do(request)
|
2020-02-02 15:29:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-06-21 16:15:28 +02:00
|
|
|
resp.Body.Close()
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
} else {
|
|
|
|
// get the status code , content type and the write the response body
|
|
|
|
ctx.ContentType(response.Header.Get(cfg.ContentTypeHeader))
|
|
|
|
ctx.StatusCode(response.StatusCode)
|
2022-06-17 21:03:18 +02:00
|
|
|
responseBody, err := io.ReadAll(response.Body)
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
response.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-02-02 15:29:06 +01:00
|
|
|
_, _ = ctx.Write(responseBody)
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
}
|
|
|
|
}
|