Skip to content

Commit

Permalink
Add cache-control header and config entry to manage it, closes #71
Browse files Browse the repository at this point in the history
  • Loading branch information
pramsey committed Dec 11, 2020
1 parent db3ac81 commit a8ff3bf
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ openssl req -nodes -new -x509 -keyout server.key -out server.crt
```

```toml
# Cache control configuration. TTL is time in seconds to request
# that responses be cached by any downstream caching services.
# Zero means no cache control header will be set.
CacheTTL = 60

# Advertise URLs relative to this server name
# default is to looke this up from incoming request headers
# UrlBase = "http://yourserver.com/"
Expand Down
7 changes: 7 additions & 0 deletions config/pg_tileserv.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@

# HTTPS configuration - TLS server certificate full chain and key
# If you do not specify these, the TLS server will not be started
# To generate self-signed key pair for testing,
# openssl req -nodes -new -x509 -keyout server.key -out server.crt
# TlsServerCertificateFile = "server.crt"
# TlsServerPrivateKeyFile = "server.key"

# Cache control configuration. TTL is time in seconds to request
# that responses be cached by any downstream caching services.
# Zero means no cache control header will be set.
# CacheTTL = 0

# Advertise URLs relative to this server name
# default is to look this up from incoming request headers
# UrlBase = "http://localhost/"
Expand Down
17 changes: 13 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ var globalPostGISVersion int = 0
// which tiles are constructed
var globalServerBounds *Bounds = nil

// timeToLive is the Cache-Control timeout value that will be advertised
// in the response headers
var globalTimeToLive = -1

/******************************************************************************/

func init() {
Expand All @@ -75,6 +79,7 @@ func init() {
viper.SetDefault("DbTimeout", 10)
viper.SetDefault("CORSOrigins", []string{"*"})
viper.SetDefault("BasePath", "/")
viper.SetDefault("CacheTTL", 0) // cache timeout in seconds

viper.SetDefault("CoordinateSystem.SRID", 3857)
// XMin, YMin, XMax, YMax, must be square
Expand Down Expand Up @@ -385,9 +390,13 @@ func (fn tileAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

/******************************************************************************/

func SetCacheControl(next http.Handler) http.Handler {
func setCacheControl(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "*")
ttl := getTTL()
if ttl > 0 {
ccVal := fmt.Sprintf("max-age=%d", ttl)
w.Header().Set("Cache-Control", ccVal)
}
next.ServeHTTP(w, r)
})
}
Expand Down Expand Up @@ -439,7 +448,7 @@ func handleRequests() {
ReadTimeout: 1 * time.Second,
WriteTimeout: writeTimeout,
Addr: fmt.Sprintf("%s:%d", viper.GetString("HttpHost"), viper.GetInt("HttpPort")),
Handler: handlers.CompressHandler(handlers.CORS(corsOpt)(r)),
Handler: setCacheControl(handlers.CompressHandler(handlers.CORS(corsOpt)(r))),
}

// start http service
Expand Down Expand Up @@ -468,7 +477,7 @@ func handleRequests() {
ReadTimeout: 1 * time.Second,
WriteTimeout: writeTimeout,
Addr: fmt.Sprintf("%s:%d", viper.GetString("HttpHost"), viper.GetInt("HttpsPort")),
Handler: handlers.CompressHandler(handlers.CORS(corsOpt)(r)),
Handler: setCacheControl(handlers.CompressHandler(handlers.CORS(corsOpt)(r))),
TLSConfig: &tls.Config{
MinVersion: tls.VersionTLS12, // Secure TLS versions only
},
Expand Down
7 changes: 7 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,10 @@ func getServerBounds() (b *Bounds, e error) {
globalServerBounds = &Bounds{srid, xmin, ymin, xmax, ymax}
return globalServerBounds, nil
}

func getTTL() (ttl int) {
if globalTimeToLive < 0 {
globalTimeToLive = viper.GetInt("CacheTTL")
}
return globalTimeToLive
}

0 comments on commit a8ff3bf

Please sign in to comment.