perf: avoid replacing dist tarball url on each call

This commit is contained in:
 Ilya Atamas
2019-04-23 16:06:45 +03:00
parent 18338eb0d7
commit 7e4e00e3d7
5 changed files with 33 additions and 17 deletions

View File

@@ -57,8 +57,9 @@ func (proxy Proxy) GetCachedPath(path string, request *http.Request) ([]byte, er
return nil, err
}
// convert body to string
pkg = string(body)
// TODO: avoid calling MustCompile every time
// find "dist": "https?://.*/ and replace to "dist": "{localurl}/
pkg = regexp.MustCompile(`(?U)"tarball":"https?://.*/`).ReplaceAllString(string(body), `"dist": "http://localhost:8080/`)
// save to redis
err = proxy.Database.Set(key, pkg, options.DatabaseExpiration)
@@ -67,10 +68,6 @@ func (proxy Proxy) GetCachedPath(path string, request *http.Request) ([]byte, er
}
}
// TODO: avoid calling MustCompile every time
// find "dist": "https?://.*/ and replace to "dist": "{localurl}/
pkg = regexp.MustCompile(`(?U)"tarball":"https?://.*/`).ReplaceAllString(pkg, `"dist": "http://localhost:8080/`)
return []byte(pkg), nil
}

View File

@@ -13,14 +13,20 @@ import (
// ServerOptions provides configuration for Server method
type ServerOptions struct {
ListenAddress string
Silent bool
}
// Server creates http proxy server
func (proxy Proxy) Server(options ServerOptions) *http.Server {
router := gin.New()
logger, _ := zap.NewProduction()
router.Use(ginzap.Ginzap(logger, time.RFC3339, true))
if options.Silent {
router.Use(gin.Recovery())
} else {
logger, _ := zap.NewProduction()
router.Use(ginzap.Ginzap(logger, time.RFC3339, true))
router.Use(ginzap.RecoveryWithZap(logger, true))
}
router.GET("/:scope/:name", proxy.getPackageHandler)
router.GET("/:scope", proxy.getPackageHandler)