Add HTTP GZip compression

This commit is contained in:
Vikram Rangnekar
2019-12-31 01:30:20 -05:00
parent 343589c3bd
commit a40bd7fca5
16 changed files with 204 additions and 194 deletions

View File

@ -6,13 +6,15 @@ import (
"strings"
)
var (
userIDProviderKey = "user_id_provider"
userIDKey = "user_id"
userRoleKey = "user_role"
type ctxkey int
const (
userIDProviderKey ctxkey = iota
userIDKey
userRoleKey
)
func headerAuth(next http.HandlerFunc) http.HandlerFunc {
func headerAuth(next http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
@ -35,7 +37,7 @@ func headerAuth(next http.HandlerFunc) http.HandlerFunc {
}
}
func withAuth(next http.HandlerFunc) http.HandlerFunc {
func withAuth(next http.Handler) http.Handler {
at := conf.Auth.Type
ru := conf.Auth.Rails.URL

View File

@ -14,7 +14,7 @@ const (
jwtAuth0 int = iota + 1
)
func jwtHandler(next http.HandlerFunc) http.HandlerFunc {
func jwtHandler(next http.Handler) http.HandlerFunc {
var key interface{}
var jwtProvider int

View File

@ -12,7 +12,7 @@ import (
"github.com/garyburd/redigo/redis"
)
func railsRedisHandler(next http.HandlerFunc) http.HandlerFunc {
func railsRedisHandler(next http.Handler) http.HandlerFunc {
cookie := conf.Auth.Cookie
if len(cookie) == 0 {
errlog.Fatal().Msg("no auth.cookie defined")
@ -66,7 +66,7 @@ func railsRedisHandler(next http.HandlerFunc) http.HandlerFunc {
}
}
func railsMemcacheHandler(next http.HandlerFunc) http.HandlerFunc {
func railsMemcacheHandler(next http.Handler) http.HandlerFunc {
cookie := conf.Auth.Cookie
if len(cookie) == 0 {
errlog.Fatal().Msg("no auth.cookie defined")
@ -108,7 +108,7 @@ func railsMemcacheHandler(next http.HandlerFunc) http.HandlerFunc {
}
}
func railsCookieHandler(next http.HandlerFunc) http.HandlerFunc {
func railsCookieHandler(next http.Handler) http.HandlerFunc {
cookie := conf.Auth.Cookie
if len(cookie) == 0 {
errlog.Fatal().Msg("no auth.cookie defined")

View File

@ -20,6 +20,7 @@ type config struct {
HostPort string `mapstructure:"host_port"`
Host string
Port string
HTTPGZip bool `mapstructure:"http_compress"`
WebUI bool `mapstructure:"web_ui"`
LogLevel string `mapstructure:"log_level"`
EnableTracing bool `mapstructure:"enable_tracing"`

View File

@ -61,7 +61,7 @@ type resolver struct {
Duration time.Duration `json:"duration"`
}
func apiv1Http(w http.ResponseWriter, r *http.Request) {
func apiV1(w http.ResponseWriter, r *http.Request) {
ctx := &coreContext{Context: r.Context()}
//nolint: errcheck

File diff suppressed because one or more lines are too long

View File

@ -10,6 +10,7 @@ import (
"time"
rice "github.com/GeertJohan/go.rice"
"github.com/NYTimes/gziphandler"
"github.com/dosco/super-graph/psql"
"github.com/dosco/super-graph/qcode"
)
@ -121,11 +122,18 @@ func startHTTP() {
}
func routeHandler() http.Handler {
var apiH http.Handler
if conf.HTTPGZip {
gzipH := gziphandler.MustNewGzipLevelHandler(6)
apiH = gzipH(http.HandlerFunc(apiV1))
} else {
apiH = http.HandlerFunc(apiV1)
}
mux := http.NewServeMux()
mux.Handle("/health", http.HandlerFunc(health))
mux.Handle("/api/v1/graphql", withAuth(apiv1Http))
mux.HandleFunc("/health", health)
mux.Handle("/api/v1/graphql", withAuth(apiH))
if conf.WebUI {
mux.Handle("/", http.FileServer(rice.MustFindBox("../web/build").HTTPBox()))