Skip to content

Commit

Permalink
include response and requst headers
Browse files Browse the repository at this point in the history
  • Loading branch information
krishanthisera committed Nov 17, 2023
1 parent dd737f0 commit c8d943a
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 54 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ require (
github.com/gin-gonic/gin v1.9.1
)

require github.com/google/uuid v1.4.0

require (
github.com/aws/aws-sdk-go-v2 v1.22.2 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.5.0 // indirect
github.com/aws/aws-sdk-go-v2/config v1.23.0 // indirect
github.com/aws/aws-sdk-go-v2/config v1.23.0
github.com/aws/aws-sdk-go-v2/credentials v1.15.2 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.3 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
Expand Down
16 changes: 8 additions & 8 deletions grender.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
version: v1
renderingConfig:
pageWaitTime: 5
pageWaitTime: 10
pageWaitCondition: '(function() { return window.prerenderReady === true })()'
requestHeaders:
- name: "X-Grender-Request"
value: "1"
# requestHeaders:
# - name: "X-Grender-Request"
# value: "1"
server:
port: "8080"
headers:
port: "8081"
responseHeaders:
- name: "X-Prerender"
value: "1"
backend:
fileSystem:
baseDir: "./tmp"
# fileSystem:
# baseDir: "./tmp"
s3:
bucketName: "grender.io"
region: "ap-southeast-2"
54 changes: 54 additions & 0 deletions pilot/handler_render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package pilot

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/krishanthisera/grender/backend"
)

// Rendering handler
func (C *Config) renderHandler(ctx *gin.Context) {
url := ctx.Param("url")
fmt.Println(url)

var be backend.Backend
var err error

// Check S3 backend configuration
if s3 := C.Backend.S3; s3.BucketName != "" {
be, err = createBackendFromConfig(s3)
} else if fs := C.Backend.FileSystem; fs.BaseDir != "" {
// Check FileSystem backend configuration
be, err = createBackendFromConfig(fs)
} else {
// No backend found
fmt.Println("No backend found")
}

if err != nil {
fmt.Println(err)
ctx.String(http.StatusInternalServerError, "Error creating backend: %v", err)
return
}
rac := renderAndCacheConfig{backend: &be, render: &C.RenderingConfig}
renderedHTML, err := rac.RenderAndCache(url)

// Page is rendered successfully
if renderedHTML != nil {
// Setting required headers
C.addResponseHeaders(ctx)
ctx.Data(http.StatusOK, "text/html", []byte(renderedHTML))

// Page cannot be cached
if err != nil {
ctx.String(http.StatusInternalServerError, "Error caching URL: %v", err)
return
}
return
}

// Page cannot be rendered
ctx.String(http.StatusInternalServerError, "Error rendering URL: %v", err)
}
14 changes: 9 additions & 5 deletions pilot/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ import (

// Config struct to represent the overall YAML configuration
type Config struct {
Version string `yaml:"version"`
RenderingConfig render.Config `yaml:"renderingConfig"`
Version string `yaml:"version" required:"true"`
RenderingConfig render.Config `yaml:"renderingConfig" required:"true"`
Server struct {
Port string `yaml:"port"`
} `yaml:"server"`
Port string `yaml:"port" required:"true"`
ResponseHeaders []struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
} `yaml:"responseHeaders"`
} `yaml:"server" required:"true"`
Backend struct {
S3 backend.S3 `yaml:"s3"`
FileSystem backend.FileSystem `yaml:"fileSystem"`
} `yaml:"backend"`
} `yaml:"backend" required:"true"`
}

type renderAndCacheConfig struct {
Expand Down
30 changes: 3 additions & 27 deletions pilot/pilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,17 @@ package pilot

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
)

func (C *Config) Grender() {

router := gin.Default()
router.GET("/render/*url", func(c *gin.Context) {
url := c.Param("url")
fmt.Println(url)

be, err := createBackendFromConfig(C.Backend.S3)
if err != nil {
fmt.Println(err)
}
rac := renderAndCacheConfig{backend: &be, render: &C.RenderingConfig}
renderedHTML, err := rac.RenderAndCache(url)

// Page is rendered successfully
if renderedHTML != nil {
c.Data(http.StatusOK, "text/html", []byte(renderedHTML))
// Page cannot be cached
if err != nil {
c.String(http.StatusInternalServerError, "Error caching URL: %v", err)
return
}
return
} else {
// Page cannot be rendered
c.String(http.StatusInternalServerError, "Error rendering URL: %v", err)
return
}

})
// Rendering requests
router.GET("/render/*url", C.renderHandler)
router.HEAD("/render/*url", C.renderHandler)

router.Run(fmt.Sprintf(":%s", C.Server.Port))
}
7 changes: 7 additions & 0 deletions pilot/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

aws "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/gin-gonic/gin"
"github.com/krishanthisera/grender/backend"
"gopkg.in/yaml.v2"
)
Expand All @@ -26,6 +27,12 @@ func createBackendFromConfig(backendConfig interface{}) (backend.Backend, error)
}
}

func (C *Config) addResponseHeaders(ctx *gin.Context) {
for _, h := range C.Server.ResponseHeaders {
ctx.Header(h.Name, h.Value)
}
}

// TO DO: Refactor this to use the backend package
// func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
// var buf map[string]interface{}
Expand Down
21 changes: 8 additions & 13 deletions render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/chromedp/cdproto/dom"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/chromedp"
"github.com/google/uuid"
)

func (config Config) Render(webAddr string) (*string, error) {
Expand All @@ -36,32 +35,28 @@ func (config Config) Render(webAddr string) (*string, error) {

var html string
// Set request headers
headers := func() network.Headers {
h := network.Headers{"X-Grender-Request-Id": fmt.Sprintf("%v", uuid.New())}
for _, v := range config.RequestHeaders {
h[v.Name] = v.Value
}
fmt.Println(h)
return h
}()
headers := map[string]interface{}{
"Grerender-Request-Id": "my request header",
}

if err := chromedp.Run(ctx, pageRender(webAddr, config.PageWailCondition, time.Duration(config.PageWaitTime*float32(time.Second)), &headers, &html)); err != nil {
if err := chromedp.Run(ctx, pageRender(webAddr, config.PageWailCondition, time.Duration(config.PageWaitTime*float32(time.Second)), headers, &html)); err != nil {
return nil, err
}
return &html, nil
}

// This is the function that does the actual rendering
func pageRender(webAddr string, waitCondition string, pageWaitTime time.Duration, headers *network.Headers, html *string) chromedp.Tasks {
func pageRender(webAddr string, pageWaitCondition string, pageWaitTime time.Duration, headers network.Headers, html *string) chromedp.Tasks {
return chromedp.Tasks{
network.Enable(),
network.SetExtraHTTPHeaders(*headers),
// Setting the netowrk headers has been disable as: ..."net::ERR_FAILED","canceled":false,"corsErrorStatus":{"corsError":"HeaderDisallowedByPreflightResponse","failedParameter":"grerender-request-id"}}
// network.SetExtraHTTPHeaders(network.Headers(headers)),
chromedp.Navigate(webAddr),
chromedp.ActionFunc(func(ctx context.Context) error {
var result bool
startTime := time.Now()
for time.Since(startTime) < pageWaitTime {
if err := chromedp.Evaluate(waitCondition, &result).Do(ctx); err != nil {
if err := chromedp.Evaluate(pageWaitCondition, &result).Do(ctx); err != nil {
return err
}
if result {
Expand Down

0 comments on commit c8d943a

Please sign in to comment.