diff --git a/cli/root.go b/cli/root.go index db18a16..956fef6 100644 --- a/cli/root.go +++ b/cli/root.go @@ -15,6 +15,7 @@ var rootCmd = &cobra.Command{ } var rootOptions struct { + Silent bool ListenAddress string UpstreamAddress string CacheLimit string @@ -22,6 +23,7 @@ var rootOptions struct { } func init() { + rootCmd.Flags().BoolVar(&rootOptions.Silent, "silent", getEnvBool("SILENT", "0"), "Disable logging") rootCmd.Flags().StringVar(&rootOptions.ListenAddress, "listen", getEnvString("LISTEN_ADDRESS", "localhost:8080"), "Address to listen") rootCmd.Flags().StringVar(&rootOptions.UpstreamAddress, "upstream", getEnvString("UPSTREAM_ADDRESS", "https://registry.npmjs.org"), "Upstream registry address") rootCmd.Flags().StringVar(&rootOptions.CacheLimit, "cache-limit", getEnvString("CACHE_LIMIT", "0"), "Cached packages count limit") @@ -39,5 +41,6 @@ func run(cmd *cobra.Command, args []string) { proxy.Server(npmproxy.ServerOptions{ ListenAddress: rootOptions.ListenAddress, + Silent: rootOptions.Silent, }).ListenAndServe() } diff --git a/cli/utils.go b/cli/utils.go index e8a2000..0617c2b 100644 --- a/cli/utils.go +++ b/cli/utils.go @@ -23,3 +23,12 @@ func getEnvInt(env string, def string) int { return converted } + +func getEnvBool(env string, def string) bool { + value := getEnvString(env, def) + + // TODO: handle error + converted, _ := strconv.ParseBool(value) + + return converted +} diff --git a/proxy/cache.go b/proxy/cache.go index e56de0f..ceaaca2 100644 --- a/proxy/cache.go +++ b/proxy/cache.go @@ -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 } diff --git a/proxy/server.go b/proxy/server.go index 2f98b67..cfb08cf 100644 --- a/proxy/server.go +++ b/proxy/server.go @@ -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) diff --git a/readme.md b/readme.md index 5e675a0..1240a55 100644 --- a/readme.md +++ b/readme.md @@ -77,6 +77,7 @@ ncp --listen "localhost:1234" # listen on port 1234 | `--upstream
` | `UPSTREAM_ADDRESS` | `https://registry.npmjs.org` | Upstream registry address | | `--cache-limit ` | `CACHE_LIMIT` | - | Cached packages count limit | | `--cache-ttl ` | `CACHE_TTL` | `3600` | Cache expiration timeout in seconds | +| `--silent
` | `SILENT` | `0` | Disable logs | --- @@ -146,17 +147,17 @@ NCP can be deployed using Kubernetes, Docker Compose or any other container orch Macbook Pro 15″ 2017, Intel Core i7-7700HQ. Note `GOMACPROCS=1`. ```bash -# GOMAXPROCS=1 ncp --listen localhost:8080 +# SILENT=1 GIN_MODE=release GOMAXPROCS=1 ncp -$ go-wrk -c 100 -d 5 http://localhost:8080/ascii -Running 5s test @ http://localhost:8080/ascii +$ go-wrk -c 100 -d 10 http://localhost:8080/ascii +Running 10s test @ http://localhost:8080/ascii 100 goroutine(s) running concurrently -33321 requests in 5.00537759s, 212.69MB read -Requests/sec: 6657.04 -Transfer/sec: 42.49MB -Avg Req Time: 15.02169ms -Fastest Request: 230.514µs -Slowest Request: 571.420487ms +84216 requests in 10.000196326s, 535.30MB read +Requests/sec: 8421.43 +Transfer/sec: 53.53MB +Avg Req Time: 11.874461ms +Fastest Request: 2.213324ms +Slowest Request: 745.874068ms Number of Errors: 0 ```