Exemplo n.º 1
0
// WillHandle checks whether this handler will be used to handle the specified
// request or not.
func (p *PathMatchHandler) WillHandle(c context.Context) (bool, error) {

	// check each matcher func
	matcherFuncMatches := true
	matcherFuncDecisionMade := false
	for _, matcherFunc := range p.MatcherFuncs {
		decision, matcherFuncErr := matcherFunc(c)

		if matcherFuncErr != nil {
			return false, matcherFuncErr
		}

		switch decision {
		case NoMatch:
			matcherFuncMatches = false
			matcherFuncDecisionMade = true
			break
		case Match:
			matcherFuncMatches = true
			matcherFuncDecisionMade = true
			break
		}

		if matcherFuncDecisionMade {
			break
		}

	}

	// check HTTP methods
	var httpMethodMatch bool = false

	if len(p.HttpMethods) == 0 {

		// no specific HTTP methods
		httpMethodMatch = true

	} else {

		for _, httpMethod := range p.HttpMethods {
			if httpMethod == c.MethodString() {
				httpMethodMatch = true
				break
			}
		}

	}

	// cancel early if we didn't get an HTTP Method match
	if !httpMethodMatch {
		return false, nil
	}

	// check path match

	pathMatch := p.PathPattern.GetPathMatch(c.Path())

	var allMatch bool

	if matcherFuncDecisionMade {
		allMatch = matcherFuncMatches
	} else {
		allMatch = pathMatch.Matches
	}

	if allMatch {

		// save the match parameters for later
		c.Data().Set(context.DataKeyPathParameters, pathMatch.Parameters)

	}

	return allMatch, nil
}
Exemplo n.º 2
0
func afterHandler(c context.Context) (err error) {
	logger.Infof("[access] /%s", c.Path().RawPath)
	return nil
}