Skip to content

Match API #2786

Open
Open
@pcfreak30

Description

@pcfreak30

I would like to see something like this in echo. If the maintainers are ok with it, I can submit a PR. I am in need to know if a router has a route blindly, from the outside. I am using it as part of higher abstractions.

package echo

import (
	"net/http"
)

// Match finds a route for the given request without executing the handler or middleware.
// It returns true if a route is found, false otherwise.
// It returns an error if the request is invalid or other issues occur during the matching process.
func (e *Echo) Match(req *http.Request) (bool, error) {
	// Acquire a context from the pool.
	c := e.pool.Get().(*context)
	defer e.pool.Put(c) // Ensure the context is released back to the pool

	// Reset the context with the provided request and a nil ResponseWriter.
	// This allows Echo's internal logic to use the Host, URL, Method, etc.
	c.Reset(req, nil) // Reset with the actual request and nil response writer

	// Find the appropriate router based on the request's Host using the existing method.
	router := e.findRouter(req.Host)

	// Call the Find method on the selected router.
	// Use the existing GetPath function to get the clean path.
	router.Find(req.Method, GetPath(req), c)

	// Check if the context's handler was set by the Find method.
	// If c.handler is not nil, it means Find found a matching route and handler.
	return c.Handler() != nil, nil
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions