mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 02:31:04 +01:00
add CustomPathWordFunc
This commit is contained in:
parent
7272c76847
commit
8f3e6f7bbf
|
@ -2,10 +2,10 @@ package main
|
|||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/mvc"
|
||||
|
||||
"github.com/kataras/iris/v12/middleware/logger"
|
||||
"github.com/kataras/iris/v12/middleware/recover"
|
||||
"github.com/kataras/iris/v12/mvc"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// This example is equivalent to the
|
||||
|
@ -40,6 +40,16 @@ func newApp() *iris.Application {
|
|||
|
||||
// Serve a controller based on the root Router, "/".
|
||||
mvc.New(app).Handle(new(ExampleController))
|
||||
// Add custom path func
|
||||
mvc.New(app).SetCustomPathWordFunc(func(path, w string, wordIndex int) string {
|
||||
|
||||
if wordIndex == 0 {
|
||||
w = strings.ToLower(w)
|
||||
}
|
||||
path += w
|
||||
return path
|
||||
|
||||
}).Handle(new(ExampleControllerCustomPath))
|
||||
return app
|
||||
}
|
||||
|
||||
|
@ -80,6 +90,13 @@ func (c *ExampleController) GetHello() interface{} {
|
|||
return map[string]string{"message": "Hello Iris!"}
|
||||
}
|
||||
|
||||
// GetHelloWorld serves
|
||||
// Method: GET
|
||||
// Resource: http://localhost:8080/hello/world
|
||||
func (c *ExampleController) GetHelloWorld() interface{} {
|
||||
return map[string]string{"message": "Hello Iris! DefaultPath"}
|
||||
}
|
||||
|
||||
// BeforeActivation called once, before the controller adapted to the main application
|
||||
// and of course before the server ran.
|
||||
// After version 9 you can also add custom routes for a specific controller's methods.
|
||||
|
@ -105,6 +122,16 @@ func (c *ExampleController) CustomHandlerWithoutFollowingTheNamingGuide() string
|
|||
return "hello from the custom handler without following the naming guide"
|
||||
}
|
||||
|
||||
type ExampleControllerCustomPath struct{}
|
||||
|
||||
// GetHelloWorld serves
|
||||
// Method: GET
|
||||
// Resource: http://localhost:8080/helloWorld
|
||||
func (c *ExampleControllerCustomPath) GetHelloWorld() interface{} {
|
||||
return map[string]string{"message": "Hello Iris! CustomPath"}
|
||||
}
|
||||
|
||||
|
||||
// GetUserBy serves
|
||||
// Method: GET
|
||||
// Resource: http://localhost:8080/user/{username:string}
|
||||
|
|
|
@ -318,7 +318,7 @@ func (c *ControllerActivator) parseMethods() {
|
|||
}
|
||||
|
||||
func (c *ControllerActivator) parseMethod(m reflect.Method) {
|
||||
httpMethod, httpPath, err := parseMethod(c.app.Router.Macros(), m, c.isReservedMethod)
|
||||
httpMethod, httpPath, err := parseMethod(c.app.Router.Macros(), m, c.isReservedMethod,c.app.customPathWordFunc)
|
||||
if err != nil {
|
||||
if err != errSkip {
|
||||
c.logErrorf("MVC: fail to parse the route path and HTTP method for '%s.%s': %v", c.fullName, m.Name, err)
|
||||
|
|
|
@ -95,9 +95,10 @@ type methodParser struct {
|
|||
lexer *methodLexer
|
||||
fn reflect.Method
|
||||
macros *macro.Macros
|
||||
customPathWordFunc CustomPathWordFunc
|
||||
}
|
||||
|
||||
func parseMethod(macros *macro.Macros, fn reflect.Method, skipper func(string) bool) (method, path string, err error) {
|
||||
func parseMethod(macros *macro.Macros, fn reflect.Method, skipper func(string) bool,wordFunc CustomPathWordFunc) (method, path string, err error) {
|
||||
if skipper(fn.Name) {
|
||||
return "", "", errSkip
|
||||
}
|
||||
|
@ -106,6 +107,7 @@ func parseMethod(macros *macro.Macros, fn reflect.Method, skipper func(string) b
|
|||
fn: fn,
|
||||
lexer: newMethodLexer(fn.Name),
|
||||
macros: macros,
|
||||
customPathWordFunc: wordFunc,
|
||||
}
|
||||
return p.parse()
|
||||
}
|
||||
|
@ -119,6 +121,8 @@ var errSkip = errors.New("skip")
|
|||
|
||||
var allMethods = append(router.AllMethods[0:], []string{"ALL", "ANY"}...)
|
||||
|
||||
type CustomPathWordFunc func(path, w string,wordIndex int) string
|
||||
|
||||
func addPathWord(path, w string) string {
|
||||
if path[len(path)-1] != '/' {
|
||||
path += "/"
|
||||
|
@ -147,6 +151,7 @@ func (p *methodParser) parse() (method, path string, err error) {
|
|||
return "", "", errSkip
|
||||
}
|
||||
|
||||
wordIndex:=0
|
||||
for {
|
||||
w := p.lexer.next()
|
||||
if w == "" {
|
||||
|
@ -171,8 +176,15 @@ func (p *methodParser) parse() (method, path string, err error) {
|
|||
|
||||
continue
|
||||
}
|
||||
// static path.
|
||||
path = addPathWord(path, w)
|
||||
|
||||
// custom static path.
|
||||
if p.customPathWordFunc!=nil {
|
||||
path = p.customPathWordFunc(path, w,wordIndex)
|
||||
}else{
|
||||
// default static path.
|
||||
path = addPathWord(path, w)
|
||||
}
|
||||
wordIndex++
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -37,6 +37,9 @@ type Application struct {
|
|||
// Disables verbose logging for controllers under this and its children mvc apps.
|
||||
// Defaults to false.
|
||||
controllersNoLog bool
|
||||
|
||||
// Set custom path
|
||||
customPathWordFunc CustomPathWordFunc
|
||||
}
|
||||
|
||||
func newApp(subRouter router.Party, container *hero.Container) *Application {
|
||||
|
@ -119,6 +122,11 @@ func (app *Application) SetName(appName string) *Application {
|
|||
return app
|
||||
}
|
||||
|
||||
func (app *Application) SetCustomPathWordFunc(wordFunc CustomPathWordFunc) *Application {
|
||||
app.customPathWordFunc = wordFunc
|
||||
return app
|
||||
}
|
||||
|
||||
// SetControllersNoLog disables verbose logging for next registered controllers
|
||||
// under this App and its children of `Application.Party` or `Application.Clone`.
|
||||
//
|
||||
|
|
Loading…
Reference in New Issue
Block a user