antpathmatcher

package module
v0.0.0-...-19d9ea9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2023 License: MIT Imports: 6 Imported by: 0

README

go-antpathmatcher

Go Report Card Run Tests GoDoc

🌌Usage

Start using it

Download and install it:

go get github.com/georgeJobs/go-antpathmatcher

Import it in your code:

import "github.com/georgeJobs/go-antpathmatcher"

🌰Example

See the example.

package main

import (
	"github.com/georgeJobs/go-antpathmatcher"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

func main() {
	e := gin.New()
	e.Use(Auth())
	e.GET("/test/12.html",func(c *gin.Context){
		c.File("./12.html")
	})
	e.GET("/hello/x", func(c *gin.Context) {
		c.JSON(200,gin.H{"message":"x success"})
	})
	e.GET("/hello/y",func(c *gin.Context){
		c.JSON(200,gin.H{"message":"y success"})
	})
	e.Run(":8080")
}

var pathMatch *antpathmatcher.AntPathMatcher

func init() {
	pathMatch = antpathmatcher.NewAntPathMatcher()
}

func Auth() func(c *gin.Context) {
	return func(c *gin.Context) {
		pathSlice := []string{"/test/*.html", "/hello/*"}
		for k := range pathSlice {
			if pathMatch.Match(pathSlice[k], c.Request.URL.Path) {
				goto JUMP
			}
		}
		c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "Forbidden", "status": 403, "message": "Forbidden", "timestamp": time.Now().Format("2006-01-02 15:04:05"), "path": c.Request.URL.Path})
		return
	JUMP:
		c.Next()
	}
}

📝 License

go-antpathmatcher is released under the MIT License. Check out the LICENSE for more information.

If you have any questions, feel free to contact us at:xx@jobs.com 📧

Documentation

Index

Constants

View Source
const CACHE_TURNOFF_THRESHOLD = 65536
View Source
const DEFAULT_PATH_SEPARATOR = "/"
View Source
const DEFAULT_VARIABLE_PATTERN = "((?s).*)"

const DEFAULT_VARIABLE_PATTERN="(.*)"

Variables

View Source
var VARIABLE_PATTERN = regexp.MustCompile("\\{[^/]+?\\}")
View Source
var WILDCARD_CHARS = [3]byte{'*', '?', '{'}

Functions

func NewPatternInfo

func NewPatternInfo(pattern string) *patternInfo

Types

type AntPathMatcher

type AntPathMatcher struct {
	// contains filtered or unexported fields
}

func NewAntPathMatcher

func NewAntPathMatcher() *AntPathMatcher

func NewAntPathMatcherWithPathSeparator

func NewAntPathMatcherWithPathSeparator(pathSeparator string) *AntPathMatcher

func (*AntPathMatcher) Combine

func (a *AntPathMatcher) Combine(pattern1, pattern2 string) string

func (*AntPathMatcher) DeactivatePatternCache

func (a *AntPathMatcher) DeactivatePatternCache()

func (*AntPathMatcher) ExtractPathWithinPattern

func (a *AntPathMatcher) ExtractPathWithinPattern(pattern, path string) string

func (*AntPathMatcher) ExtractUriTemplateVariables

func (a *AntPathMatcher) ExtractUriTemplateVariables(pattern, path string) map[string]string

func (*AntPathMatcher) GetPatternComparator

func (a *AntPathMatcher) GetPatternComparator(path string) Comparator

func (*AntPathMatcher) IsPattern

func (a *AntPathMatcher) IsPattern(path string) bool

func (*AntPathMatcher) Match

func (a *AntPathMatcher) Match(pattern, path string) bool

func (*AntPathMatcher) MatchStart

func (a *AntPathMatcher) MatchStart(pattern, path string) bool

func (*AntPathMatcher) SetCachePatterns

func (a *AntPathMatcher) SetCachePatterns(cachePatterns bool)

func (*AntPathMatcher) SetPathSeparator

func (a *AntPathMatcher) SetPathSeparator(pathSeparator string)

type AntPathStringMatcher

type AntPathStringMatcher struct {
	// contains filtered or unexported fields
}

func NewAntPathStringMatcher

func NewAntPathStringMatcher(pattern string) *AntPathStringMatcher

func NewAntPathStringMatcherWithCaseSensitive

func NewAntPathStringMatcherWithCaseSensitive(pattern string, caseSensitive bool) *AntPathStringMatcher

type AntPatternComparator

type AntPatternComparator struct {
	// contains filtered or unexported fields
}

func NewAntPatternComparator

func NewAntPatternComparator(path string) *AntPatternComparator

func (*AntPatternComparator) Compare

func (a *AntPatternComparator) Compare(pattern1, pattern2 string) int

type Comparator

type Comparator interface {
	Compare(string, string) int
}

type PathMatcher

type PathMatcher interface {
	IsPattern(string) bool
	Match(string, string) bool
	MatchStart(string, string) bool
	ExtractPathWithinPattern(string, string) string
	ExtractUriTemplateVariables(string, string) map[string]string
	GetPatternComparator(string) Comparator
	Combine(string, string) string
}

type PathSeparatorPatternCache

type PathSeparatorPatternCache struct {
	// contains filtered or unexported fields
}

func NewPathSeparatorPatternCache

func NewPathSeparatorPatternCache(pathSeparator string) *PathSeparatorPatternCache

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL