Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] dynamic routing does not take effect #2024

Closed
PalaChen opened this issue Dec 21, 2022 · 5 comments
Closed

[QUESTION] dynamic routing does not take effect #2024

PalaChen opened this issue Dec 21, 2022 · 5 comments

Comments

@PalaChen
Copy link

PalaChen commented Dec 21, 2022

Describe the bug
regularly extract integers in URLs

To Reproduce
Steps to reproduce the behavior:

func App() *iris.Application {
	app := iris.New()
	template := iris.Django("./template", ".html")
	// 始终动态加载
	template.Reload(true)
	app.RegisterView(template)
        app.Get("/category/aaa/{uid:uint}", adminController.CategoryAdd)
        app.Get("/category/bbb/{uid:string regexp([0-9]{1,20})}.html", adminController.CategoryAdd)
        app.Get("/category/ccc/{uid:uint}.html", adminController.CategoryAdd)
        app.Get("/category/ddd/{uid:uint regexp(\\d+)}.html", adminController.CategoryAdd)

        uidExpr := "^[0-9]{1,20}?$"
	        uidRegex, err := regexp.Compile(uidExpr)
	        if err != nil {
		        panic(err)
	        }
	app.Macros().Get("string").RegisterFunc("getUid", uidRegex.MatchString)
	app.Get("/category/eee/{uid:string getUid() else 502}.html", adminController.CategoryAdd)
}

view

func CategoryAdd(ctx iris.Context) {
	uid := ctx.Params().GetUintDefault("uid", 0)
	fmt.Printf("uid:%v\n", uid)
        ctx.View("admin/category_add.html")
}

Expected behavior
url :

/category/aaa/1111    uid=111   working
/category/bbb/222    uid=0     working
/category/ccc/3333.html        404   return not found
/category/ddd/444.html          404   return not found
/category/eee/444.html          404   return not found

i want to

/category/aaa/1111    uid=111   working
/category/bbb/222    uid=222     working
/category/ccc/3333.html          uid=3333 working
/category/ddd/444.html           uid=444   working
/category/eee/555.html            uid=555   working

how to solve?

Desktop (please complete the following information):
mac

iris.Version
v12

Please make sure the bug is reproducible over the master branch:

@kataras
Copy link
Owner

kataras commented Dec 22, 2022

Hello @PalaChen, try to put the .html thing inside the path parameter (your regexp) and not the /path instead.

@PalaChen
Copy link
Author

i try it but get is string not uint,

/category/eee/555.html get 555.html but i want to get 555

@kataras
Copy link
Owner

kataras commented Dec 24, 2022

Hello @PalaChen here is a working example:

package main

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

func main() {
	app := iris.New()
	app.Get("/{uid:string regexp(^[0-9]{1,20}.html$)}", iris.TrimParamFilePart, handler)
	app.Listen(":8080")
}

func handler(ctx iris.Context) {
	//
	// The above line is useless now that we've registered the TrimFilePart middleware:
	// uid := ctx.Params().GetTrimFileUint64("uid")
	// TrimParamFilePart can be registered as a middleware, like the rest, to a group of routes too (Party).

	uid := ctx.Params().GetUint64Default("uid", 0)
	ctx.Writef("Param value: %d\n", uid)
}

The TrimParamFilePart is a new middleware that you can use to trim the last ".html" part of a parameter in order to keep using the GetXXX methods of the ctx.Params() as you used to. That middleware is just a small addition to the framework because of your question here but you can copy-paste it by yourself without updating Iris module:

func TrimParamFilePart(ctx iris.Context) {
	params := ctx.Params()

	for i, param := range params.Store {
		if value, ok := param.ValueRaw.(string); ok {
			if idx := strings.LastIndexByte(value, '.'); idx > 1 /* at least .h */ {
				value = value[0:idx]
				param.ValueRaw = value
			}
		}

		params.Store[i] = param
	}

	ctx.Next()
}

I've added an example of your case and an error message to protect others from doing the same.

kataras added a commit that referenced this issue Dec 24, 2022
kataras added a commit that referenced this issue Dec 24, 2022
@PalaChen
Copy link
Author

thanks

@kataras
Copy link
Owner

kataras commented Jan 3, 2023

You're welcome @PalaChen !

@kataras kataras changed the title [BUG]dynamic routing does not take effect [QUESTION] dynamic routing does not take effect Jan 3, 2023
@kataras kataras closed this as completed Mar 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants