(chore) define fragment references
This commit is contained in:
Родитель
0b7b1fdd08
Коммит
206e7968e8
|
@ -1,3 +1,3 @@
|
|||
<fragment src="http://localhost:3000/fragment1"></fragment>
|
||||
<fragment src="fragment2" primary></fragment>
|
||||
<fragment src="fragment3" timeout="10" fallback="fallback"></fragment>
|
||||
<fragment src="fragment3" timeout="1" fallback="fallback"></fragment>
|
23
fragment.go
23
fragment.go
|
@ -10,6 +10,7 @@ import (
|
|||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
"github.com/valyala/fasthttp"
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
@ -24,6 +25,9 @@ type Fragment struct {
|
|||
src string
|
||||
timeout int64
|
||||
|
||||
id string
|
||||
ref string
|
||||
|
||||
statusCode int
|
||||
head []*html.Node
|
||||
|
||||
|
@ -52,6 +56,15 @@ func FromSelection(s *goquery.Selection) *Fragment {
|
|||
t, _ := strconv.ParseInt(timeout, 10, 64)
|
||||
f.timeout = t
|
||||
|
||||
id, ok := s.Attr("id")
|
||||
if !ok {
|
||||
id = uuid.New().String()
|
||||
}
|
||||
f.id = id
|
||||
|
||||
ref, _ := s.Attr("ref")
|
||||
f.ref = ref
|
||||
|
||||
deferred, ok := s.Attr("deferred")
|
||||
f.deferred = ok && strings.ToUpper(deferred) != "FALSE"
|
||||
|
||||
|
@ -105,6 +118,16 @@ func (f *Fragment) Links() []*html.Node {
|
|||
return f.head
|
||||
}
|
||||
|
||||
// Ref represents the reference to another fragment
|
||||
func (f *Fragment) Ref() string {
|
||||
return f.ref
|
||||
}
|
||||
|
||||
// ID represents a unique id for the fragment
|
||||
func (f *Fragment) ID() string {
|
||||
return f.id
|
||||
}
|
||||
|
||||
// Resolve is resolving all needed data, setting headers
|
||||
// and the status code.
|
||||
func (f *Fragment) Resolve() ResolverFunc {
|
||||
|
|
1
go.mod
1
go.mod
|
@ -6,6 +6,7 @@ require (
|
|||
github.com/PuerkitoBio/goquery v1.7.0
|
||||
github.com/gofiber/fiber/v2 v2.13.0
|
||||
github.com/gofiber/template v1.6.12
|
||||
github.com/google/uuid v1.0.0
|
||||
github.com/valyala/fasthttp v1.26.0
|
||||
golang.org/x/net v0.0.0-20210510120150-4163338589ed
|
||||
)
|
||||
|
|
1
go.sum
1
go.sum
|
@ -129,6 +129,7 @@ github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXi
|
|||
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
|
||||
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
|
||||
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
|
||||
github.com/google/uuid v1.0.0 h1:b4Gk+7WdP/d3HZH8EJsZpvV7EtDOgaZLtnaNGIu1adA=
|
||||
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
|
||||
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
|
||||
|
|
Загрузка…
Ссылка в новой задаче