From bac2a555ac722b9bae7428d8e1db1466fe8a2196 Mon Sep 17 00:00:00 2001 From: Seagyn Davis Date: Fri, 1 Apr 2022 15:35:28 +0200 Subject: [PATCH] WIP --- go.mod | 11 +++++++++++ go.sum | 24 ++++++++++++++++++++++++ inertia.go | 46 ++++++++++++++++++++++++++++------------------ middleware.go | 29 +++++++++++++---------------- page.go | 3 +++ 5 files changed, 79 insertions(+), 34 deletions(-) create mode 100644 go.sum diff --git a/go.mod b/go.mod index 9851b70..01edd42 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,14 @@ module github.com/seagyn/inertia-fiber go 1.17 + +require github.com/gofiber/fiber/v2 v2.25.0 + +require ( + github.com/andybalholm/brotli v1.0.2 // indirect + github.com/klauspost/compress v1.13.4 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasthttp v1.32.0 // indirect + github.com/valyala/tcplisten v1.0.0 // indirect + golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3a1fc16 --- /dev/null +++ b/go.sum @@ -0,0 +1,24 @@ +github.com/andybalholm/brotli v1.0.2 h1:JKnhI/XQ75uFBTiuzXpzFrUriDPiZjlOSzh6wXogP0E= +github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= +github.com/gofiber/fiber/v2 v2.25.0 h1:kv8dmG/sAFDFpTueCMEn4X0JS5d72pEFTKLZ3miOREw= +github.com/gofiber/fiber/v2 v2.25.0/go.mod h1:7efVWcBOZi1PyMWznnbitjnARPA7nYZxmQXJVod0bo0= +github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/klauspost/compress v1.13.4 h1:0zhec2I8zGnjWcKyLl6i3gPqKANCCn5e9xmviEEeX6s= +github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.32.0 h1:keswgWzyKyNIIjz2a7JmCYHOOIkRp6HMx9oTV6QrZWY= +github.com/valyala/fasthttp v1.32.0/go.mod h1:2rsYD01CKFrjjsvFxx75KlEUNpWNBY9JWD3K/7o2Cus= +github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= +github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= +golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 h1:hZR0X1kPW+nwyJ9xRxqZk1vx5RUObAPBdKVvXPDUH/E= +golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/inertia.go b/inertia.go index 4a016af..9d7105b 100644 --- a/inertia.go +++ b/inertia.go @@ -8,6 +8,8 @@ import ( "net/http" "path/filepath" "strings" + + "github.com/gofiber/fiber/v2" ) // Inertia type. @@ -87,11 +89,11 @@ func (i *Inertia) WithViewData(ctx context.Context, key string, value interface{ } // Render function. -func (i *Inertia) Render(w http.ResponseWriter, r *http.Request, component string, props map[string]interface{}) error { +func (i *Inertia) Render(c *fiber.Ctx, component string, props map[string]interface{}) error { only := make(map[string]string) - partial := r.Header.Get("X-Inertia-Partial-Data") + partial := c.Get("X-Inertia-Partial-Data") - if partial != "" && r.Header.Get("X-Inertia-Partial-Component") == component { + if partial != "" && c.Get("X-Inertia-Partial-Component") == component { for _, value := range strings.Split(partial, ",") { only[value] = value } @@ -100,7 +102,8 @@ func (i *Inertia) Render(w http.ResponseWriter, r *http.Request, component strin page := &Page{ Component: component, Props: make(map[string]interface{}), - URL: r.RequestURI, + Routes: flattenRoutes(c.App().Stack()), + URL: c.OriginalURL(), Version: i.version, } @@ -110,7 +113,7 @@ func (i *Inertia) Render(w http.ResponseWriter, r *http.Request, component strin } } - contextProps := r.Context().Value(ContextKeyProps) + contextProps := c.Context().Value(ContextKeyProps) if contextProps != nil { contextProps, ok := contextProps.(map[string]interface{}) @@ -131,26 +134,21 @@ func (i *Inertia) Render(w http.ResponseWriter, r *http.Request, component strin } } - if r.Header.Get("X-Inertia") != "" { + if c.Get("X-Inertia") != "" { js, err := json.Marshal(page) if err != nil { return err } - w.Header().Set("Vary", "Accept") - w.Header().Set("X-Inertia", "true") - w.Header().Set("Content-Type", "application/json") - - _, err = w.Write(js) - if err != nil { - return err - } + c.Set("Vary", "Accept") + c.Set("X-Inertia", "true") + c.Set("Content-Type", "application/json") - return nil + return c.Send(js) } viewData := make(map[string]interface{}) - contextViewData := r.Context().Value(ContextKeyViewData) + contextViewData := c.Context().Value(ContextKeyViewData) if contextViewData != nil { contextViewData, ok := contextViewData.(map[string]interface{}) @@ -170,9 +168,9 @@ func (i *Inertia) Render(w http.ResponseWriter, r *http.Request, component strin return err } - w.Header().Set("Content-Type", "text/html") + c.Set("Content-Type", "text/html") - err = ts.Execute(w, viewData) + err = ts.Execute(c.Response().BodyWriter(), viewData) if err != nil { return err } @@ -195,3 +193,15 @@ func (i *Inertia) createRootTemplate() (*template.Template, error) { return ts.ParseFiles(i.rootTemplate) } + +func flattenRoutes(r [][]*fiber.Route) []*fiber.Route { + routes := []*fiber.Route{} + + for _, routeGroup := range r { + for _, route := range routeGroup { + routes = append(routes, route) + } + } + + return routes +} diff --git a/middleware.go b/middleware.go index 1824463..127646a 100644 --- a/middleware.go +++ b/middleware.go @@ -1,23 +1,20 @@ package inertia -import "net/http" +import ( + "github.com/gofiber/fiber/v2" +) -// Middleware function. -func (i *Inertia) Middleware(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Header.Get("X-Inertia") == "" { - next.ServeHTTP(w, r) +func (i *Inertia) Middleware(c *fiber.Ctx) error { + if c.Get("X-Inertia") == "" { + return c.Next() + } - return - } + if c.Method() == "GET" && c.Get("X-Inertia-Version") != i.version { + c.Set("X-Inertia-Location", i.url+c.OriginalURL()) - if r.Method == "GET" && r.Header.Get("X-Inertia-Version") != i.version { - w.Header().Set("X-Inertia-Location", i.url+r.RequestURI) - w.WriteHeader(http.StatusConflict) + return c.SendStatus(fiber.StatusConflict) + } - return - } - - next.ServeHTTP(w, r) - }) + // Go to next middleware: + return c.Next() } diff --git a/page.go b/page.go index 4179904..57320b9 100644 --- a/page.go +++ b/page.go @@ -1,8 +1,11 @@ package inertia +import "github.com/gofiber/fiber/v2" + // Page type. type Page struct { Component string `json:"component"` + Routes []*fiber.Route `json:"routes"` Props map[string]interface{} `json:"props"` URL string `json:"url"` Version string `json:"version"`