perf: avoid replacing dist tarball url on each call
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user