feat: add option to set the cache-control header

This commit is contained in:
Vikram Rangnekar
2020-04-24 20:45:03 -04:00
parent 505335d872
commit 1a70603b1a
8 changed files with 53 additions and 8 deletions

View File

@ -46,6 +46,7 @@ type Serv struct {
AllowedOrigins []string `mapstructure:"cors_allowed_origins"`
DebugCORS bool `mapstructure:"cors_debug"`
APIPath string `mapstructure:"api_path"`
CacheControl string `mapstructure:"cache_control"`
Auth auth.Auth
Auths []auth.Auth

View File

@ -7,6 +7,7 @@ import (
"io/ioutil"
"net/http"
"github.com/dosco/super-graph/core"
"github.com/dosco/super-graph/internal/serv/internal/auth"
"github.com/rs/cors"
"go.uber.org/zap"
@ -84,15 +85,19 @@ func apiV1(w http.ResponseWriter, r *http.Request) {
log.Printf("DBG query %s: %s", res.QueryName(), res.SQL())
}
if err != nil {
renderErr(w, err)
} else {
if err == nil {
if len(conf.CacheControl) != 0 && res.Operation() == core.OpQuery {
w.Header().Set("Cache-Control", conf.CacheControl)
}
json.NewEncoder(w).Encode(res)
} else {
renderErr(w, err)
}
if doLog && logLevel >= LogLevelInfo {
zlog.Info("success",
zap.String("op", res.Operation()),
zap.String("op", res.OperationName()),
zap.String("name", res.QueryName()),
zap.String("role", res.Role()),
)

View File

@ -46,6 +46,13 @@ cors_allowed_origins: ["*"]
# Debug Cross Origin Resource Sharing requests
cors_debug: false
# Default API path prefix is /api you can change it if you like
# api_path: "/data"
# Cache-Control header can help cache queries if your CDN supports cache-control
# on POST requests (does not work with not mutations)
# cache_control: "public, max-age=300, s-maxage=600"
# Postgres related environment Variables
# SG_DATABASE_HOST
# SG_DATABASE_PORT

View File

@ -49,6 +49,13 @@ reload_on_config_change: false
# Debug Cross Origin Resource Sharing requests
# cors_debug: false
# Default API path prefix is /api you can change it if you like
# api_path: "/data"
# Cache-Control header can help cache queries if your CDN supports cache-control
# on POST requests (does not work with not mutations)
# cache_control: "public, max-age=300, s-maxage=600"
# Postgres related environment Variables
# SG_DATABASE_HOST
# SG_DATABASE_PORT