(chore) define fragment references

This commit is contained in:
Sebastian Döll 2021-08-19 14:10:03 +00:00 коммит произвёл GitHub
Родитель 0b7b1fdd08
Коммит 206e7968e8
4 изменённых файлов: 26 добавлений и 1 удалений

Просмотреть файл

@ -1,3 +1,3 @@
<fragment src="http://localhost:3000/fragment1"></fragment> <fragment src="http://localhost:3000/fragment1"></fragment>
<fragment src="fragment2" primary></fragment> <fragment src="fragment2" primary></fragment>
<fragment src="fragment3" timeout="10" fallback="fallback"></fragment> <fragment src="fragment3" timeout="1" fallback="fallback"></fragment>

Просмотреть файл

@ -10,6 +10,7 @@ import (
"github.com/PuerkitoBio/goquery" "github.com/PuerkitoBio/goquery"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
"golang.org/x/net/html" "golang.org/x/net/html"
) )
@ -24,6 +25,9 @@ type Fragment struct {
src string src string
timeout int64 timeout int64
id string
ref string
statusCode int statusCode int
head []*html.Node head []*html.Node
@ -52,6 +56,15 @@ func FromSelection(s *goquery.Selection) *Fragment {
t, _ := strconv.ParseInt(timeout, 10, 64) t, _ := strconv.ParseInt(timeout, 10, 64)
f.timeout = t 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") deferred, ok := s.Attr("deferred")
f.deferred = ok && strings.ToUpper(deferred) != "FALSE" f.deferred = ok && strings.ToUpper(deferred) != "FALSE"
@ -105,6 +118,16 @@ func (f *Fragment) Links() []*html.Node {
return f.head 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 // Resolve is resolving all needed data, setting headers
// and the status code. // and the status code.
func (f *Fragment) Resolve() ResolverFunc { func (f *Fragment) Resolve() ResolverFunc {

1
go.mod
Просмотреть файл

@ -6,6 +6,7 @@ require (
github.com/PuerkitoBio/goquery v1.7.0 github.com/PuerkitoBio/goquery v1.7.0
github.com/gofiber/fiber/v2 v2.13.0 github.com/gofiber/fiber/v2 v2.13.0
github.com/gofiber/template v1.6.12 github.com/gofiber/template v1.6.12
github.com/google/uuid v1.0.0
github.com/valyala/fasthttp v1.26.0 github.com/valyala/fasthttp v1.26.0
golang.org/x/net v0.0.0-20210510120150-4163338589ed golang.org/x/net v0.0.0-20210510120150-4163338589ed
) )

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-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/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/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/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.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=