Skip to content

Commit

Permalink
Added support for URL patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
lesichkovm committed Jan 30, 2024
1 parent e03ddfc commit 5e8ba98
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 9 deletions.
69 changes: 69 additions & 0 deletions PageFindByAlias.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cms

import (
"regexp"
"strings"

"github.com/gouniverse/entitystore"
)

Expand Down Expand Up @@ -36,5 +39,71 @@ func (cms *Cms) PageFindByAlias(alias string) (*entitystore.Entity, error) {
return page, nil
}

page, err = cms.PageFindByAliasWithPatterns(alias)

if err != nil {
return nil, err
}

if page != nil {
return page, nil
}

return nil, nil
}

// PageFindByAliasWithPatterns helper method to find a page by matching patterns
//
// =====================================================================
//
// The following patterns are supported:
// :any
// :num
// :all
// :string
// :number
// :numeric
// :alpha
//
// =====================================================================
func (cms *Cms) PageFindByAliasWithPatterns(alias string) (*entitystore.Entity, error) {
patterns := map[string]string{
":any": "([^/]+)",
":num": "([0-9]+)",
":all": "(.*)",
":string": "([a-zA-Z]+)",
":number": "([0-9]+)",
":numeric": "([0-9-.]+)",
":alpha": "([a-zA-Z0-9-_]+)",
}

attributes, err := cms.EntityStore.AttributeList(entitystore.AttributeQueryOptions{
EntityType: ENTITY_TYPE_PAGE,
AttributeKey: "alias",
})
if err != nil {
return nil, err
}

pageAliasMap := make(map[string]string, len(attributes))
for _, attribute := range attributes {
pageAliasMap[attribute.EntityID()] = attribute.AttributeValue()
}

for pageID, pageAlias := range pageAliasMap {
if !strings.Contains(pageAlias, ":") {
continue
}

for pattern, replacement := range patterns {
pageAlias = strings.ReplaceAll(pageAlias, pattern, replacement)
}

matcher := regexp.MustCompile("^" + pageAlias + "$")
if matcher.MatchString(alias) {
return cms.EntityStore.EntityFindByID(pageID)
}
}

return nil, nil
}
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,25 @@ func Routes(cmsRouter http.HandlerFunc) *chi.Mux {
}
```

### CMS URL Patterns

The following URL patterns are supported:

- :any - ([^/]+)
- :num - ([0-9]+)
- :all - (.*)
- :string - ([a-zA-Z]+)
- :number - ([0-9]+)
- :numeric - ([0-9-.]+)
- :alpha - ([a-zA-Z0-9-_]+)

Example:

```
/blog/:num/:any
/shop/product/:num/:any
```

## Development Instructions

There is a development directory that allows you to quickly start working on the project or simply to preview
Expand Down
18 changes: 9 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ require (
github.com/gouniverse/bs v0.10.1
github.com/gouniverse/cachestore v0.17.0
github.com/gouniverse/entitystore v0.61.0
github.com/gouniverse/hb v1.71.1
github.com/gouniverse/hb v1.72.0
github.com/gouniverse/logstore v1.3.0
github.com/gouniverse/responses v0.4.0
github.com/gouniverse/responses v0.6.0
github.com/gouniverse/sessionstore v0.21.0
github.com/gouniverse/settingstore v0.11.0
github.com/gouniverse/taskstore v0.8.0
github.com/gouniverse/utils v1.36.0
github.com/gouniverse/utils v1.38.0
github.com/lib/pq v1.10.9
github.com/samber/lo v1.39.0
github.com/stretchr/testify v1.8.4
Expand All @@ -30,17 +30,17 @@ require (
github.com/doug-martin/goqu/v9 v9.19.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/georgysavva/scany v1.2.1 // indirect
github.com/golang-module/carbon/v2 v2.3.4 // indirect
github.com/gouniverse/cdn v1.0.0
github.com/golang-module/carbon/v2 v2.3.7 // indirect
github.com/gouniverse/cdn v1.3.0
github.com/gouniverse/maputils v0.7.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/exp v0.0.0-20240110193028-0dcbfd608b1e // indirect
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect
)

require (
github.com/darkoatanasovski/htmltags v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gouniverse/api v1.5.0
github.com/gouniverse/icons v1.3.0
github.com/gouniverse/sb v0.3.0
Expand All @@ -56,12 +56,12 @@ require (
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.16.1 // indirect
golang.org/x/tools v0.17.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/uint128 v1.3.0 // indirect
modernc.org/cc/v3 v3.41.0 // indirect
modernc.org/ccgo/v3 v3.16.15 // indirect
modernc.org/libc v1.40.1 // indirect
modernc.org/libc v1.40.8 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.7.2 // indirect
modernc.org/opt v0.1.3 // indirect
Expand Down
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14j
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/golang-module/carbon/v2 v2.3.4 h1:us9XSzBLPR47FY7KXUimstgi3vNzDjmgGiI+RTs0jVU=
github.com/golang-module/carbon/v2 v2.3.4/go.mod h1:XDALX7KgqmHk95xyLeaqX9/LJGbfLATyruTziq68SZ8=
github.com/golang-module/carbon/v2 v2.3.7 h1:rMFEq6x04DfJ+BHQMKUt1bEmqJ0mtve2OaSzEFlzDYw=
github.com/golang-module/carbon/v2 v2.3.7/go.mod h1:XDALX7KgqmHk95xyLeaqX9/LJGbfLATyruTziq68SZ8=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
Expand All @@ -41,6 +43,8 @@ github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S3
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gouniverse/api v1.5.0 h1:3AJecTtHXQ0wToPUlEixUa0waU15/PtUdFMrLB+jveQ=
github.com/gouniverse/api v1.5.0/go.mod h1:u67bggZW1AGuWD268jBRP//POnGDxgyfP8dV8+i5G+c=
github.com/gouniverse/bs v0.10.1 h1:u15WHMtuI3XeXQbRa+O1HjiCebS0tvIC1DwAmTTMrZU=
Expand All @@ -49,10 +53,14 @@ github.com/gouniverse/cachestore v0.17.0 h1:tiQcuCXMQGcNG92W6btjA1WLIUevzY1ApdPo
github.com/gouniverse/cachestore v0.17.0/go.mod h1:8lZ/58crXTmlTvxXpCcQDnS1A4cbHcnY2wjOb6pNGMY=
github.com/gouniverse/cdn v1.0.0 h1:k092USiX2uPdEwi8MMab+kjDReuq5Zozjddl4UD76ro=
github.com/gouniverse/cdn v1.0.0/go.mod h1:AEc+jQZiEAYyRw6jaPctb+JWvj6Mh3YXCMT1vS7og1U=
github.com/gouniverse/cdn v1.3.0 h1:CcsqJ9tpWCxPHgpy/wjt+8oXr9Gvnwvf4lK777emv9w=
github.com/gouniverse/cdn v1.3.0/go.mod h1:AEc+jQZiEAYyRw6jaPctb+JWvj6Mh3YXCMT1vS7og1U=
github.com/gouniverse/entitystore v0.61.0 h1:ap7J2e34lR5HtZqUxsPqtzZgzi7OPW9FHxnCYU/peZI=
github.com/gouniverse/entitystore v0.61.0/go.mod h1:Bn8LAT2eUhibirs/h3u1t4bbR4VKxd0sBXVBzHxMw98=
github.com/gouniverse/hb v1.71.1 h1:7EZjGzeL1mrd4TZbmWkcX/ywIT3TSA4snJcpCMi2eGM=
github.com/gouniverse/hb v1.71.1/go.mod h1:oFDsiwpgM7TNBIla8YNbEbMlTYGNGs7wj1kVobb5CRI=
github.com/gouniverse/hb v1.72.0 h1:tyP5/slK6tOuOOnh51d0sar90xOiK3OVmIpI2YyJ8z8=
github.com/gouniverse/hb v1.72.0/go.mod h1:oFDsiwpgM7TNBIla8YNbEbMlTYGNGs7wj1kVobb5CRI=
github.com/gouniverse/icons v1.3.0 h1:GzKzjfxDxSdJt7qFv3Qb2vr0O3NgfVBDmH7TJy+X3tk=
github.com/gouniverse/icons v1.3.0/go.mod h1:s/8uu1UY8LL9e8hfJONQBTI0np9zG6FMqq2gbENqoZU=
github.com/gouniverse/logstore v1.3.0 h1:DAWApmXsH71N5ULtDzCSASIg0MPrjR9Gm8db2Rk0THg=
Expand All @@ -61,6 +69,8 @@ github.com/gouniverse/maputils v0.7.0 h1:qoJnY8tY5gkdyuIkwGHJYwH7It7LnCevxU+P+c4
github.com/gouniverse/maputils v0.7.0/go.mod h1:s8HbjSvEqBl+R+bFCvFd+mY07bx7EQM5YhIjDgF26Q0=
github.com/gouniverse/responses v0.4.0 h1:eupC8LBnQ7/u/xJrHCnZ989VycUZ2/x7WMe84isNi1g=
github.com/gouniverse/responses v0.4.0/go.mod h1:hszJpLrEiH5Hvv4DPBHFQYL0aPxMNgpjmajqCLeI/vE=
github.com/gouniverse/responses v0.6.0 h1:34KYyxYUqyXDslAuJMEKp5rM4CWpR23jmUwDaR0gJKo=
github.com/gouniverse/responses v0.6.0/go.mod h1:y9k4LpZHjzyvXybtGX8W9/Jb2oIDIhT0kP3Oy63rFg4=
github.com/gouniverse/sb v0.3.0 h1:wxK7RzWqsT+a0FSvv1EEB6v9zptsDX48KGzNw4qqlOg=
github.com/gouniverse/sb v0.3.0/go.mod h1:XbTMrLZBpfOKFOyXPPupEC6yD7khYp2iFpeSGljQ9GE=
github.com/gouniverse/sessionstore v0.21.0 h1:TGZajkrw/jbkP9PSzqOHHJVMGpyzp5ne478apnPisjU=
Expand All @@ -76,6 +86,8 @@ github.com/gouniverse/uid v1.5.0 h1:evyGegnY7+KeYirDhJntI9xmODf8jPMQw8DlMpQIPnM=
github.com/gouniverse/uid v1.5.0/go.mod h1:06dzYTyBLOu+iRlKZ8GxzEfgDSLyoZwgKns9Fcvt7G4=
github.com/gouniverse/utils v1.36.0 h1:S4NHQcAdubZy2MrdW87OLlzdWA6cbKYiswBEgoLHgWw=
github.com/gouniverse/utils v1.36.0/go.mod h1:yktn4RXk7++KwQ25NivzoDIebMD/EjAy6uiL2jnDrFg=
github.com/gouniverse/utils v1.38.0 h1:5gU9NibdOgeWUS1viifcvzjcZTXsMnbr7UCZQgljDMU=
github.com/gouniverse/utils v1.38.0/go.mod h1:yktn4RXk7++KwQ25NivzoDIebMD/EjAy6uiL2jnDrFg=
github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0=
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
Expand Down Expand Up @@ -235,6 +247,8 @@ golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/exp v0.0.0-20240110193028-0dcbfd608b1e h1:723BNChdd0c2Wk6WOE320qGBiPtYx0F0Bbm1kriShfE=
golang.org/x/exp v0.0.0-20240110193028-0dcbfd608b1e/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a h1:Q8/wZp0KX97QFTc2ywcOE0YRjZPVIx+MXInMzdvQqcA=
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
Expand Down Expand Up @@ -282,6 +296,8 @@ golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA=
golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down Expand Up @@ -315,6 +331,8 @@ modernc.org/httpfs v1.0.6 h1:AAgIpFZRXuYnkjftxTAZwMIiwEqAfk8aVB2/oA6nAeM=
modernc.org/httpfs v1.0.6/go.mod h1:7dosgurJGp0sPaRanU53W4xZYKh14wfzX420oZADeHM=
modernc.org/libc v1.40.1 h1:ZhRylEBcj3GyQbPVC8JxIg7SdrT4JOxIDJoUon0NfF8=
modernc.org/libc v1.40.1/go.mod h1:YAXkAZ8ktnkCKaN9sw/UDeUVkGYJ/YquGO4FTi5nmHE=
modernc.org/libc v1.40.8 h1:ZHN83BZzEytp4ctJMC2lxGTU3l8jo+2kGW7AUatIDZw=
modernc.org/libc v1.40.8/go.mod h1:YAXkAZ8ktnkCKaN9sw/UDeUVkGYJ/YquGO4FTi5nmHE=
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
modernc.org/memory v1.7.2 h1:Klh90S215mmH8c9gO98QxQFsY+W451E8AnzjoE2ee1E=
Expand Down

0 comments on commit 5e8ba98

Please sign in to comment.