perf: move dynamic options reciever to server, enable Cache-Control HTTP header

This commit is contained in:
 Ilya Atamas
2019-04-23 17:59:18 +03:00
parent f821e794f8
commit 62e64e5154
9 changed files with 91 additions and 94 deletions

View File

@@ -10,12 +10,7 @@ import (
)
// GetCachedPath returns cached upstream response for a given url path.
func (proxy Proxy) GetCachedPath(path string, request *http.Request) ([]byte, error) {
options, err := proxy.GetOptions()
if err != nil {
return nil, err
}
func (proxy Proxy) GetCachedPath(options Options, path string, request *http.Request) ([]byte, error) {
key := options.DatabasePrefix + path
// get package from database
@@ -72,12 +67,7 @@ func (proxy Proxy) GetCachedPath(path string, request *http.Request) ([]byte, er
}
// ListCachedPaths returns list of all cached url paths.
func (proxy Proxy) ListCachedPaths() ([]string, error) {
options, err := proxy.GetOptions()
if err != nil {
return nil, err
}
func (proxy Proxy) ListCachedPaths(options Options) ([]string, error) {
metadata, err := proxy.Database.Keys(options.DatabasePrefix)
if err != nil {
return nil, err
@@ -92,12 +82,7 @@ func (proxy Proxy) ListCachedPaths() ([]string, error) {
}
// PurgeCachedPaths deletes all cached url paths.
func (proxy Proxy) PurgeCachedPaths() error {
options, err := proxy.GetOptions()
if err != nil {
return err
}
func (proxy Proxy) PurgeCachedPaths(options Options) error {
metadata, err := proxy.Database.Keys(options.DatabasePrefix)
if err != nil {
return err

View File

@@ -14,8 +14,6 @@ import (
type Proxy struct {
Database Database
HttpClient *http.Client
GetOptions func() (Options, error)
}
// Options provides dynamic options for Proxy.

View File

@@ -14,10 +14,14 @@ import (
type ServerOptions struct {
ListenAddress string
Silent bool
GetOptions func() (Options, error)
}
// Server creates http proxy server
func (proxy Proxy) Server(options ServerOptions) *http.Server {
gin.SetMode("release")
router := gin.New()
if options.Silent {
@@ -28,9 +32,9 @@ func (proxy Proxy) Server(options ServerOptions) *http.Server {
router.Use(ginzap.RecoveryWithZap(logger, true))
}
router.GET("/:scope/:name", proxy.getPackageHandler)
router.GET("/:scope", proxy.getPackageHandler)
router.NoRoute(proxy.noRouteHandler)
router.GET("/:scope/:name", proxy.getPackageHandler(options))
router.GET("/:scope", proxy.getPackageHandler(options))
router.NoRoute(proxy.noRouteHandler(options))
return &http.Server{
Handler: router,
@@ -38,45 +42,50 @@ func (proxy Proxy) Server(options ServerOptions) *http.Server {
}
}
func (proxy Proxy) getPackageHandler(c *gin.Context) {
pkg, err := proxy.GetCachedPath(c.Request.URL.Path, c.Request)
if err != nil {
c.AbortWithError(500, err)
} else {
// c.Header("Content-Encoding", "gzip")
c.Data(200, "application/json", pkg)
}
}
func (proxy Proxy) getTarballHabdler(c *gin.Context) {
pkg, err := proxy.GetCachedPath(c.Request.URL.Path, c.Request)
if err != nil {
c.AbortWithError(500, err)
} else {
c.Data(200, "application/json", pkg)
}
}
func (proxy Proxy) noRouteHandler(c *gin.Context) {
if strings.Contains(c.Request.URL.Path, ".tgz") {
proxy.getTarballHabdler(c)
} else if c.Request.URL.Path == "/" {
err := proxy.Database.Health()
func (proxy Proxy) getPackageHandler(options ServerOptions) gin.HandlerFunc {
return func(c *gin.Context) {
options, err := options.GetOptions()
if err != nil {
c.AbortWithStatusJSON(503, err)
c.AbortWithError(500, err)
} else {
c.AbortWithStatusJSON(200, gin.H{"ok": true})
}
} else {
options, err := proxy.GetOptions()
pkg, err := proxy.GetCachedPath(options, c.Request.URL.Path, c.Request)
if err != nil {
c.AbortWithStatusJSON(500, err)
} else {
c.Redirect(http.StatusTemporaryRedirect, options.UpstreamAddress+c.Request.URL.Path)
if err != nil {
c.AbortWithError(500, err)
} else {
c.Header("Cache-Control", "public, max-age="+string(int(options.DatabaseExpiration.Seconds())))
c.Data(200, "application/json", pkg)
}
}
}
}
func (proxy Proxy) noRouteHandler(options ServerOptions) gin.HandlerFunc {
tarballHandler := proxy.getPackageHandler(options)
return func(c *gin.Context) {
if strings.Contains(c.Request.URL.Path, ".tgz") {
// get tarball
tarballHandler(c)
} else if c.Request.URL.Path == "/" {
// get health
err := proxy.Database.Health()
if err != nil {
c.AbortWithStatusJSON(503, err)
} else {
c.AbortWithStatusJSON(200, gin.H{"ok": true})
}
} else {
// redirect
options, err := options.GetOptions()
if err != nil {
c.AbortWithStatusJSON(500, err)
} else {
c.Redirect(http.StatusTemporaryRedirect, options.UpstreamAddress+c.Request.URL.Path)
}
}
}
}