Skip to content

Commit

Permalink
Serve data.json directly if it already exists locally
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxr1998 committed Oct 26, 2018
1 parent 257ea2e commit e5de08e
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"log"
"net/http"
"os"
)

func main() {
Expand All @@ -12,11 +13,17 @@ func main() {
}

func handler(w http.ResponseWriter, req *http.Request) {
log.Println("GET file:", req.URL.Path)
if req.URL.Path == "/data.json" {
w.Header().Add("Cache-Control", "no-cache")
w.Write(getVisitsAndCaptures())
if _, err := os.Stat("dump/data.json"); !os.IsNotExist(err) {
log.Println("→ found locally")
http.ServeFile(w, req, "dump/data.json")
} else {
log.Println("→ generating now")
w.Header().Add("Cache-Control", "no-cache")
w.Write(getVisitsAndCaptures())
}
return
}
log.Println("GET file:", req.URL.Path)
http.ServeFile(w, req, "static/"+req.URL.Path)
}

0 comments on commit e5de08e

Please sign in to comment.