diff --git a/_examples/mvc/hello-world/main.go b/_examples/mvc/hello-world/main.go index 10a4797c..4b3ce6bd 100644 --- a/_examples/mvc/hello-world/main.go +++ b/_examples/mvc/hello-world/main.go @@ -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} diff --git a/mvc/controller.go b/mvc/controller.go index 55e29f47..44ce0c9e 100644 --- a/mvc/controller.go +++ b/mvc/controller.go @@ -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) diff --git a/mvc/controller_method_parser.go b/mvc/controller_method_parser.go index c5fcb7ba..57a3b8bd 100644 --- a/mvc/controller_method_parser.go +++ b/mvc/controller_method_parser.go @@ -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 } diff --git a/mvc/mvc.go b/mvc/mvc.go index 204c8fb7..6f0c8ad5 100644 --- a/mvc/mvc.go +++ b/mvc/mvc.go @@ -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`. //