Skip to content

Commit

Permalink
Etag updation automatic
Browse files Browse the repository at this point in the history
No need to restart server
  • Loading branch information
ilango100 committed Sep 25, 2017
1 parent 0c71bff commit a92e02d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Here index.html is the main file and style.css and script.js are the dependant f

Now start the server, the server will take care of pushing the dependant files along with the main file. If the server detects the browser already has cached copy of style.css, it just pushes 304 Not Modified response, which also avoids the browser revalidating the cache.

Internally, CAPush uses Etags to check for file updates. If you update a file, you need to restart the server (Will be updated soon). Due to CAPush, your site will be very fast. EcServ is one of the few servers that have implemented the Cache Aware Server Push mechanism.
Internally, CAPush uses Etags to check for file updates. Due to CAPush, your site will be very fast. EcServ is one of the few servers that have implemented the Cache Aware Server Push mechanism.

## Extending / CGI

Expand Down
23 changes: 23 additions & 0 deletions capush_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"os"
"path"
"strings"
"sync"
"time"
)

type CAPHandler struct {
Expand All @@ -17,6 +19,7 @@ type CAPHandler struct {
Root string
IndexFile string
NotFoundHandler http.Handler
sync.RWMutex
}

func (c *CAPHandler) send(rw http.ResponseWriter, f io.Reader, compr bool) {
Expand Down Expand Up @@ -92,6 +95,10 @@ func (c *CAPHandler) typeAndSendFile(rw http.ResponseWriter, filename string, co

func (c *CAPHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {

//Read lock
c.RLock()
defer c.RUnlock()

//Set correct filename
filename := req.URL.Path[1:]
if filename == "" {
Expand Down Expand Up @@ -156,5 +163,21 @@ func createCAPHandler(root string) http.Handler {
NotFoundHandler: http.NotFoundHandler(),
}

go func(c *CAPHandler) {
for {
etags, err := depEtags(c.Root)
if err != nil {
fmt.Println("Fatal: Error refreshing Etags...")
os.Exit(1)
}
deps, _ := genDeps(c.Root)
c.Lock()
c.Etags = etags
c.Deps = deps
c.Unlock()
time.Sleep(time.Minute)
}
}(handler)

return handler
}

0 comments on commit a92e02d

Please sign in to comment.