Skip to content

Commit

Permalink
Merge pull request #3 from justcoded/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
yvoitenko authored May 11, 2022
2 parents 8a866ff + 61b67d5 commit b449f40
Show file tree
Hide file tree
Showing 10 changed files with 350 additions and 141 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: init build build-server build-storage up run start down stop
.PHONY: init build build-server build-storage up run start down stop restart

init:
@if [ ! -f '.env' ]; then \
Expand Down Expand Up @@ -34,3 +34,5 @@ run:
down: stop
stop:
docker-compose down

restart: stop run
38 changes: 32 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@ GO Prerender

> Inspired by https://github.com/goprerender/prerender
GO Prerender is a dockerized server app written in Golang that uses Headless Chrome to render HTML and JS files out of any web page.
The Prerender server listens for a http request, takes the URL and loads it in Headless Chrome, waits for the page to finish loading by waiting for the network to be idle, and then returns your content.
GO Prerender is a dockerized server app written in Golang that uses Headless Chrome to render HTML and JS files out of
any web page.
The Prerender server listens for a http request, takes the URL and loads it in Headless Chrome, waits for the page to
finish loading by waiting for the network to be idle, and then returns your content.

#### In memory cache

Caches pages in memory using prerender `storage` container.
It caches pages in memory using prerender `storage` container.

### Installation
### Installation

* clone repository
* run `make init`
* configure `.env` and `docker-compose.yml` to fit your needs or use the default.
* run `make up` - this will build and start docker containers


### Development

Run `make build-server` to build server Go application.
Run `make build-storage` to build storage Go application.
Run `make build` to build both Go applications server and storage.
Run `make build` to build both Go applications server and storage.

#### Usage

Expand All @@ -33,3 +34,28 @@ To prerender some URL you need to specify it as a query parameter, like this:
```
http://localhost:3000/render?url=https://www.example.com/
```

#### Purge cache

Cached pages are valid for 7 days by default, but you can force clear it.

To purge all cached pages you need to pass the `Cache-Control` header
with the value `must-revalidate` or the `Clear-Site-Data` header with any value.

Just send a usual request to

```
http://localhost:3000/render?url=https://www.example.com/
```

With this header

```
Cache-Control: must-revalidate
```

or

```
Clear-Site-Data: *
```
10 changes: 8 additions & 2 deletions src/api/proto/storage.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import "google/protobuf/timestamp.proto";
// The storage service definition.
service Storage {
// Sends a store request message
rpc Store (StoreRequest) returns (StoreReply) {}
rpc Store(StoreRequest) returns (StoreReply) {}
// Get a request from storage
rpc Get (GetRequest) returns (GetReplay) {}
rpc Get(GetRequest) returns (GetReplay) {}
//
rpc Len(LenRequest) returns (LenReplay) {}
//
rpc Purge(PurgeRequest) returns (PurgeReply) {}
}

message Page {
Expand Down Expand Up @@ -57,3 +59,7 @@ message LenRequest {}
message LenReplay {
int32 length = 1;
}

message PurgeRequest {}

message PurgeReply {}
8 changes: 7 additions & 1 deletion src/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func main() {
logger.Infof("Starting server...")
time.Sleep(5 * time.Second)

logger.Infof("Connecting to storage %v", address)
logger.Infof("Connecting to storage %v", address)
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
Expand Down Expand Up @@ -156,6 +156,12 @@ func handleRequest(ctx context.Context, pc cachers.Сacher, logger prLog.Logger)
ctx, cancel := context.WithTimeout(newTabCtx, time.Second*60)
defer cancel()

req := c.Request

if req.Header.Get("Cache-Control") == "must-revalidate" || req.Header.Get("Clear-Site-Data") != "" {
pc.Purge()
}

res, err := renderer.DoRender(ctx, queryString, pc, false, logger)
if err != nil {
return err
Expand Down
17 changes: 11 additions & 6 deletions src/cmd/storage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import (
)

const (
port = ":50051"
duration = time.Hour*24*7
port = ":50051"
duration = time.Hour * 24 * 7
)

var gc = gcache.New(100000).
LRU().
Build()
LRU().
Build()

// server is used to implement Saver.
type server struct {
Expand Down Expand Up @@ -51,12 +51,17 @@ func (s *server) Get(ctx context.Context, in *storage.GetRequest) (*storage.GetR
}

func (s *server) Len(ctx context.Context, in *storage.LenRequest) (*storage.LenReplay, error) {
log.Printf("Received: Len request")
//log.Printf("Received: Len request")
return &storage.LenReplay{Length: int32(gc.Len(true))}, nil
}

func main() {
func (s *server) Purge(ctx context.Context, in *storage.PurgeRequest) (*storage.PurgeReply, error) {
gc.Purge()

return &storage.PurgeReply{}, nil
}

func main() {
ctx := context.Background()
lis, err := net.Listen("tcp", port)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion src/internal/cachers/cacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ type Сacher interface {
Put(key string, data []byte) error
Get(key string) ([]byte, error)
Len() int
}
Purge()
}
6 changes: 5 additions & 1 deletion src/internal/cachers/inmemory/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type repository struct {
}

func New(gc gcache.Cache) cachers.Сacher {
return repository{gc: gc, dd: time.Hour*24*7}
return repository{gc: gc, dd: time.Hour * 24 * 7}
}

func (r repository) Put(key string, data []byte) error {
Expand All @@ -30,3 +30,7 @@ func (r repository) Get(key string) ([]byte, error) {
func (r repository) Len() int {
return r.gc.Len(true)
}

func (r repository) Purge() {
r.gc.Purge()
}
11 changes: 10 additions & 1 deletion src/internal/cachers/rstorage/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,16 @@ func (s server) Len() int {
req := storage.LenRequest{}
result, err := s.gw.Len(ctx, &req)
if err != nil {
log.Fatal( err)
log.Fatal(err)
}
return int(result.GetLength())
}

func (s server) Purge() {
ctx := context.Background()
req := storage.PurgeRequest{}
_, err := s.gw.Purge(ctx, &req)
if err != nil {
log.Fatal(err)
}
}
Loading

0 comments on commit b449f40

Please sign in to comment.