Skip to content

Commit

Permalink
Update to version 12.1.7
Browse files Browse the repository at this point in the history
Former-commit-id: 3e214ab6b6da4d1c6e4a66180a4ccfa61c0879ae
  • Loading branch information
kataras committed Feb 10, 2020
1 parent ea589b1 commit 10f280a
Show file tree
Hide file tree
Showing 15 changed files with 362 additions and 128 deletions.
9 changes: 9 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ Developers are not forced to upgrade if they don't really need it. Upgrade whene

**How to upgrade**: Open your command-line and execute this command: `go get github.com/kataras/iris/v12@latest`.

# Mo, 10 February 2020 | v12.1.7

Implement **new** `SetRegisterRule(iris.RouteOverride, RouteSkip, RouteError)` to resolve: https://github.com/kataras/iris/issues/1448

New Examples:

- [_examples/Docker](_examples/Docker)
- [_examples/routing/route-register-rule](_examples/routing/route-register-rule)

# We, 05 February 2020 | v12.1.6

Fixes:
Expand Down
4 changes: 2 additions & 2 deletions HISTORY_ES.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ Los desarrolladores no están obligados a actualizar si realmente no lo necesita

**Cómo actualizar**: Abra su línea de comandos y ejecute este comando: `go get github.com/kataras/iris/v12@latest`.

# We, 05 February 2020 | v12.1.6
# Mo, 10 February 2020 | v12.1.7

Not translated yet, please navigate to the [english version](HISTORY.md#we-05-february-2020--v1216) instead.
Not translated yet, please navigate to the [english version](HISTORY.md#mo-10-february-2020--v1217) instead.

# Sábado, 26 de octubre 2019 | v12.0.0

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# News

![](https://iris-go.com/images/release.png) Iris version **12.1.6** has been [released](HISTORY.md#we-05-february-2020--v1216)!
![](https://iris-go.com/images/release.png) Iris version **12.1.7** has been [released](HISTORY.md#mo-10-february-2020--v1217)!

![](https://iris-go.com/images/cli.png) The official [Iris Command Line Interface](https://github.com/kataras/iris-cli) will soon be near you in 2020!

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12.1.6:https://github.com/kataras/iris/releases/tag/v12.1.6
12.1.7:https://github.com/kataras/iris/releases/tag/v12.1.7
1 change: 1 addition & 0 deletions _examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ Navigate through examples for a better understanding.
- [Writing a middleware](routing/writing-a-middleware)
* [per-route](routing/writing-a-middleware/per-route/main.go)
* [globally](routing/writing-a-middleware/globally/main.go)
- [Route Register Rule](routing/route-register-rule/main.go) **NEW**

### Versioning

Expand Down
2 changes: 1 addition & 1 deletion _examples/docker/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module app
go 1.13

require (
github.com/kataras/iris/v12 v12.1.6
github.com/kataras/iris/v12 v12.1.7
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
)
41 changes: 41 additions & 0 deletions _examples/routing/route-register-rule/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import "github.com/kataras/iris/v12"

func main() {
app := newApp()
// Navigate through https://github.com/kataras/iris/issues/1448 for details.
//
// GET: http://localhost:8080
// POST, PUT, DELETE, CONNECT, HEAD, PATCH, OPTIONS, TRACE : http://localhost:8080
app.Listen(":8080")
}

func newApp() *iris.Application {
app := iris.New()
// Skip and do NOT override existing regitered route, continue normally.
// Applies to a Party and its children, in this case the whole application's routes.
app.SetRegisterRule(iris.RouteSkip)

/* Read also:
// The default behavior, will override the getHandler to anyHandler on `app.Any` call.
app.SetRegistRule(iris.RouteOverride)
// Stops the execution and fires an error before server boot.
app.SetRegisterRule(iris.RouteError)
*/

app.Get("/", getHandler)
// app.Any does NOT override the previous GET route because of `iris.RouteSkip` rule.
app.Any("/", anyHandler)

return app
}

func getHandler(ctx iris.Context) {
ctx.Writef("From %s", ctx.GetCurrentRoute().Trace())
}

func anyHandler(ctx iris.Context) {
ctx.Writef("From %s", ctx.GetCurrentRoute().Trace())
}
22 changes: 22 additions & 0 deletions _examples/routing/route-register-rule/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"testing"

"github.com/kataras/iris/v12/core/router"
"github.com/kataras/iris/v12/httptest"
)

func TestRouteRegisterRuleExample(t *testing.T) {
app := newApp()
e := httptest.New(t, app)

for _, method := range router.AllMethods {
tt := e.Request(method, "/").Expect().Status(httptest.StatusOK).Body()
if method == "GET" {
tt.Equal("From [./main.go:28] GET: / -> github.com/kataras/iris/v12/_examples/routing/route-register-rule.getHandler()")
} else {
tt.Equal("From [./main.go:30] " + method + ": / -> github.com/kataras/iris/v12/_examples/routing/route-register-rule.anyHandler()")
}
}
}
80 changes: 55 additions & 25 deletions _examples/view/template_html_3/templates/page.html
Original file line number Diff line number Diff line change
@@ -1,25 +1,55 @@
<a href="{{urlpath "my-page1"}}">/mypath</a>
<br />
<br />

<a href="{{urlpath "my-page2" "theParam1" "theParam2"}}">/mypath2/{paramfirst}/{paramsecond}</a>
<br />
<br />

<a href="{{urlpath "my-page3" "theParam1" "theParam2AfterStatic"}}">/mypath3/{paramfirst}/statichere/{paramsecond}</a>
<br />
<br />

<a href="{{urlpath "my-page4" "theParam1" "theparam2AfterStatic" "otherParam" "matchAnything"}}">
/mypath4/{paramfirst}/statichere/{paramsecond}/{otherparam}/{something:path}</a>
<br />
<br />

<a href="{{urlpath "my-page5" "theParam1" "theParam2Afterstatichere" "otherParam" "matchAnythingAfterStatic"}}">
/mypath5/{paramfirst}/statichere/{paramsecond}/{otherparam}/anything/{anything:path}</a>
<br />
<br />

<a href={{urlpath "my-page6" .ParamsAsArray }}>
/mypath6/{paramfirst}/{paramsecond}/statichere/{paramThirdAfterStatic}
</a>
<html>

<head>
<title>template_html_3</title>
<style>
a {
color: #0f7afc;
border-bottom-color: rgba(15, 122, 252, 0.2);
text-decoration: none
}

a:hover {
color: #cf0000;
border-bottom-color: rgba(208, 64, 0, 0.2);
text-decoration: none
}

a:visited {
color: #800080;
border-bottom-color: rgba(128, 0, 128, 0.2);
text-decoration: none
}
</style>
</head>

<body>

<a href="{{urlpath "my-page1"}}">/mypath</a>
<br />
<br />

<a href="{{urlpath "my-page2" "theParam1" "theParam2"}}">/mypath2/{paramfirst}/{paramsecond}</a>
<br />
<br />

<a href="{{urlpath "my-page3" "theParam1" "theParam2AfterStatic"}}">/mypath3/{paramfirst}/statichere/{paramsecond}</a>
<br />
<br />

<a href="{{urlpath "my-page4" "theParam1" "theparam2AfterStatic" "otherParam" "matchAnything"}}">
/mypath4/{paramfirst}/statichere/{paramsecond}/{otherparam}/{something:path}</a>
<br />
<br />

<a href="{{urlpath "my-page5" "theParam1" "theParam2Afterstatichere" "otherParam" "matchAnythingAfterStatic"}}">
/mypath5/{paramfirst}/statichere/{paramsecond}/{otherparam}/anything/{anything:path}</a>
<br />
<br />

<a href={{urlpath "my-page6" .ParamsAsArray }}>
/mypath6/{paramfirst}/{paramsecond}/statichere/{paramThirdAfterStatic}
</a>
</body>

</html>
3 changes: 3 additions & 0 deletions context/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ type RouteReadOnly interface {

// ResolvePath returns the formatted path's %v replaced with the args.
ResolvePath(args ...string) string
// Trace returns some debug infos as a string sentence.
// Should be called after Build.
Trace() string

// Tmpl returns the path template,
// it contains the parsed template
Expand Down
57 changes: 48 additions & 9 deletions core/router/api_builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package router

import (
"fmt"
"net/http"
"os"
"path"
Expand Down Expand Up @@ -80,13 +81,20 @@ func (repo *repository) getAll() []*Route {
return repo.routes
}

func (repo *repository) register(route *Route) {
func (repo *repository) register(route *Route, rule RouteRegisterRule) (*Route, error) {
for i, r := range repo.routes {
// 14 August 2019 allow register same path pattern with different macro functions,
// see #1058
if route.DeepEqual(r) {
// replace existing with the latest one.
repo.routes = append(repo.routes[:i], repo.routes[i+1:]...)
if rule == RouteSkip {
return r, nil
} else if rule == RouteError {
return nil, fmt.Errorf("new route: %s conflicts with an already registered one: %s route", route.String(), r.String())
} else {
// replace existing with the latest one, the default behavior.
repo.routes = append(repo.routes[:i], repo.routes[i+1:]...)
}

continue
}
}
Expand All @@ -97,6 +105,7 @@ func (repo *repository) register(route *Route) {
}

repo.pos[route.tmpl.Src] = len(repo.routes) - 1
return route, nil
}

// APIBuilder the visible API for constructing the router
Expand Down Expand Up @@ -140,6 +149,8 @@ type APIBuilder struct {

// the per-party (and its children) execution rules for begin, main and done handlers.
handlerExecutionRules ExecutionRules
// the per-party (and its children) route registration rule, see `SetRegisterRule`.
routeRegisterRule RouteRegisterRule
}

var _ Party = (*APIBuilder)(nil)
Expand Down Expand Up @@ -210,15 +221,35 @@ func (api *APIBuilder) SetExecutionRules(executionRules ExecutionRules) Party {
return api
}

// RouteRegisterRule is a type of uint8.
// Defines the register rule for new routes that already exists.
// Available values are: RouteOverride, RouteSkip and RouteError.
//
// See `Party#SetRegisterRule`.
type RouteRegisterRule uint8

const (
// RouteOverride an existing route with the new one, the default rule.
RouteOverride RouteRegisterRule = iota
// RouteSkip registering a new route twice.
RouteSkip
// RouteError log when a route already exists, shown after the `Build` state,
// server never starts.
RouteError
)

// SetRegisterRule sets a `RouteRegisterRule` for this Party and its children.
// Available values are: RouteOverride (the default one), RouteSkip and RouteError.
func (api *APIBuilder) SetRegisterRule(rule RouteRegisterRule) Party {
api.routeRegisterRule = rule
return api
}

// CreateRoutes returns a list of Party-based Routes.
// It does NOT registers the route. Use `Handle, Get...` methods instead.
// This method can be used for third-parties Iris helpers packages and tools
// that want a more detailed view of Party-based Routes before take the decision to register them.
func (api *APIBuilder) CreateRoutes(methods []string, relativePath string, handlers ...context.Handler) []*Route {
// if relativePath[0] != '/' {
// return nil, errors.New("path should start with slash and should not be empty")
// }

if len(methods) == 0 || methods[0] == "ALL" || methods[0] == "ANY" { // then use like it was .Any
return api.Any(relativePath, handlers...)
}
Expand Down Expand Up @@ -327,14 +358,18 @@ func (api *APIBuilder) Handle(method string, relativePath string, handlers ...co
routes := api.CreateRoutes([]string{method}, relativePath, handlers...)

var route *Route // the last one is returned.
var err error
for _, route = range routes {
if route == nil {
break
}
// global

route.topLink = api.routes.getRelative(route)
api.routes.register(route)
if route, err = api.routes.register(route, api.routeRegisterRule); err != nil {
api.errors.Add(err)
break
}
}

return route
Expand Down Expand Up @@ -441,7 +476,10 @@ func (api *APIBuilder) HandleDir(requestPath, directory string, opts ...DirOptio

for _, route := range routes {
route.MainHandlerName = `HandleDir(directory: "` + directory + `")`
api.routes.register(route)
if _, err := api.routes.register(route, api.routeRegisterRule); err != nil {
api.errors.Add(err)
break
}
}

return getRoute
Expand Down Expand Up @@ -496,6 +534,7 @@ func (api *APIBuilder) Party(relativePath string, handlers ...context.Handler) P
relativePath: fullpath,
allowMethods: allowMethods,
handlerExecutionRules: api.handlerExecutionRules,
routeRegisterRule: api.routeRegisterRule,
}
}

Expand Down
3 changes: 3 additions & 0 deletions core/router/party.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ type Party interface {
//
// Example: https://github.com/kataras/iris/tree/master/_examples/mvc/middleware/without-ctx-next
SetExecutionRules(executionRules ExecutionRules) Party
// SetRegisterRule sets a `RouteRegisterRule` for this Party and its children.
// Available values are: RouteOverride (the default one), RouteSkip and RouteError.
SetRegisterRule(rule RouteRegisterRule) Party
// Handle registers a route to the server's router.
// if empty method is passed then handler(s) are being registered to all methods, same as .Any.
//
Expand Down
Loading

0 comments on commit 10f280a

Please sign in to comment.