1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-09 13:05:56 +00:00

Add a better example for the recaptcha middleware as requested at: https://github.com/kataras/iris/issues/799

Former-commit-id: 85c3286a9d6be5cf47631e7608f70f3790934e64
This commit is contained in:
kataras
2017-11-04 02:59:21 +02:00
parent 7cd78df9e3
commit ef41e07d4e
11 changed files with 153 additions and 59 deletions

View File

@@ -10,7 +10,7 @@ import (
"github.com/kataras/iris/core/netutil"
)
const host = "http://live.iris-go.com"
const host = "https://live.iris-go.com"
// PostForm performs the PostForm with a secure client.
func PostForm(p string, data url.Values) (*http.Response, error) {

View File

@@ -23,15 +23,17 @@ func CheckForUpdates() {
updateAvailale := v.Compare(Version) == version.Smaller
if updateAvailale {
has, ft := hasInternetConnection()
canUpdate := (has && ft && ask()) || !has || !ft
if canUpdate {
installVersion(v)
if confirmUpdate(v) {
has, ft := hasInternetConnection()
canUpdate := (has && ft && ask()) || !has || !ft
if canUpdate {
installVersion()
}
}
}
}
func installVersion(v version.Version) {
func confirmUpdate(v version.Version) bool {
// on help? when asking for installing the new update
// and when answering "No".
ignoreUpdatesMsg := "Would you like to ignore future updates? Disable the version checker via:\napp.Run(..., iris.WithoutVersionChecker)"
@@ -47,25 +49,23 @@ func installVersion(v version.Version) {
Message: shouldUpdateNowMsg,
Help: ignoreUpdatesMsg,
}, &confirmUpdate, nil)
return confirmUpdate // it's true only when update was available and user typed "yes".
}
// run the updater last, so the user can star the repo and at the same time
// the app will update her/his local iris.
if confirmUpdate { // it's true only when update was available and user typed "yes".
repo := "github.com/kataras/iris/..."
cmd := exec.Command("go", "get", "-u", "-v", repo)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
func installVersion() {
golog.Infof("Downloading...\n")
repo := "github.com/kataras/iris/..."
cmd := exec.Command("go", "get", "-u", "-v", repo)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
if err := cmd.Run(); err != nil {
golog.Warnf("unexpected message while trying to go get,\nif you edited the original source code then you've to remove the whole $GOPATH/src/github.com/kataras folder and execute `go get -u github.com/kataras/iris/...` manually\n%v", err)
return
}
golog.Infof("Update process finished.\nManual rebuild and restart is required to apply the changes...\n")
if err := cmd.Run(); err != nil {
golog.Warnf("unexpected message while trying to go get,\nif you edited the original source code then you've to remove the whole $GOPATH/src/github.com/kataras folder and execute `go get -u github.com/kataras/iris/...` manually\n%v", err)
return
}
// if update was available but chosen not to update then just continue...
golog.Infof("Update process finished.\nManual rebuild and restart is required to apply the changes...\n")
return
}
/* Author's note:

View File

@@ -146,6 +146,13 @@ func (api *APIBuilder) Handle(method string, relativePath string, handlers ...co
}
fullpath := api.relativePath + relativePath // for now, keep the last "/" if any, "/xyz/"
if len(handlers) == 0 {
api.reporter.Add("missing handlers for route %s: %s", method, fullpath)
return nil
}
// before join the middleware + handlers + done handlers.
possibleMainHandlerName := context.HandlerName(handlers[0])
// global begin handlers -> middleware that are registered before route registration
// -> handlers that are passed to this Handle function.
@@ -158,7 +165,7 @@ func (api *APIBuilder) Handle(method string, relativePath string, handlers ...co
// here we separate the subdomain and relative path
subdomain, path := splitSubdomainAndPath(fullpath)
r, err := NewRoute(method, subdomain, path, routeHandlers, api.macros)
r, err := NewRoute(method, subdomain, path, possibleMainHandlerName, routeHandlers, api.macros)
if err != nil { // template path parser errors:
api.reporter.Add("%v -> %s:%s:%s", err, method, subdomain, path)
return nil

View File

@@ -1,6 +1,7 @@
package router
import (
"github.com/kataras/golog"
"html"
"net/http"
"sort"
@@ -136,7 +137,9 @@ func (h *routerHandler) Build(provider RoutesProvider) error {
if err := h.addRoute(r); err != nil {
// node errors:
rp.Add("%v -> %s", err, r.String())
continue
}
golog.Debugf(r.Trace())
}
return rp.Return()

View File

@@ -22,7 +22,8 @@ type Route struct {
beginHandlers context.Handlers
// Handlers are the main route's handlers, executed by order.
// Cannot be empty.
Handlers context.Handlers
Handlers context.Handlers
mainHandlerName string
// temp storage, they're appended to the Handlers on build.
// Execution happens after Begin and main Handler(s), can be empty.
doneHandlers context.Handlers
@@ -36,7 +37,7 @@ type Route struct {
// handlers and the macro container which all routes should share.
// It parses the path based on the "macros",
// handlers are being changed to validate the macros at serve time, if needed.
func NewRoute(method, subdomain, unparsedPath string,
func NewRoute(method, subdomain, unparsedPath, mainHandlerName string,
handlers context.Handlers, macros *macro.Map) (*Route, error) {
tmpl, err := macro.Parse(unparsedPath, macros)
@@ -54,13 +55,14 @@ func NewRoute(method, subdomain, unparsedPath string,
formattedPath := formatPath(path)
route := &Route{
Name: defaultName,
Method: method,
Subdomain: subdomain,
tmpl: tmpl,
Path: path,
Handlers: handlers,
FormattedPath: formattedPath,
Name: defaultName,
Method: method,
Subdomain: subdomain,
tmpl: tmpl,
Path: path,
Handlers: handlers,
mainHandlerName: mainHandlerName,
FormattedPath: formattedPath,
}
return route, nil
}
@@ -203,6 +205,27 @@ func (r Route) ResolvePath(args ...string) string {
return formattedPath
}
// Trace returns some debug infos as a string sentence.
// Should be called after Build.
func (r Route) Trace() string {
printfmt := fmt.Sprintf("%s:", r.Method)
if r.Subdomain != "" {
printfmt += fmt.Sprintf(" %s", r.Subdomain)
}
printfmt += fmt.Sprintf(" %s ", r.Tmpl().Src)
if l := len(r.Handlers); l > 1 {
printfmt += fmt.Sprintf("-> %s() and %d more", r.mainHandlerName, l-1)
} else {
printfmt += fmt.Sprintf("-> %s()", r.mainHandlerName)
}
// printfmt := fmt.Sprintf("%s: %s >> %s", r.Method, r.Subdomain+r.Tmpl().Src, r.mainHandlerName)
// if l := len(r.Handlers); l > 0 {
// printfmt += fmt.Sprintf(" and %d more", l)
// }
return printfmt // without new line.
}
type routeReadOnlyWrapper struct {
*Route
}
@@ -222,3 +245,7 @@ func (rd routeReadOnlyWrapper) Subdomain() string {
func (rd routeReadOnlyWrapper) Path() string {
return rd.Route.tmpl.Src
}
func (rd routeReadOnlyWrapper) Trace() string {
return rd.Route.Trace()
}