From e5b40a79ea85aa6bb75f2e1cff6726f363b70dc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=A0Ilya=20Atamas?= Date: Sun, 21 Apr 2019 18:30:04 +0300 Subject: [PATCH] feat: add cli flags --- Makefile | 2 +- cli/list.go | 18 +++++------- cli/main.go | 34 ++++++++++++++++++++++ cli/purge.go | 15 ++++------ cli/root.go | 47 ++++++++++++++++-------------- cli/utils.go | 25 ++++++++++++++++ example/main.go | 31 ++++++++++++++++++++ go.mod | 17 ++--------- go.sum | 70 ++------------------------------------------ proxy/main.go | 5 +--- readme.md | 77 +++++++++++++++++++++++++++++++++++++------------ 11 files changed, 191 insertions(+), 150 deletions(-) create mode 100644 cli/utils.go create mode 100644 example/main.go diff --git a/Makefile b/Makefile index d3bed69..9d3e710 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ run: .PHONY: build build: - CGO_ENABLED=0 go build -ldflags="-w -s" -o build/router + gox -output=build/ncp_{{.OS}}_{{.Arch}} .PHONY: test test: diff --git a/cli/list.go b/cli/list.go index 7c60c40..b4a4d14 100644 --- a/cli/list.go +++ b/cli/list.go @@ -2,10 +2,8 @@ package cli import ( "fmt" - "net/http" - "github.com/emeralt/npm-cache-proxy/proxy" - "github.com/go-redis/redis" + npmproxy "github.com/emeralt/npm-cache-proxy/proxy" "github.com/spf13/cobra" ) @@ -14,15 +12,13 @@ var listCmd = &cobra.Command{ Use: "list", Short: "List all cached packages", Run: func(cmd *cobra.Command, args []string) { - prx := proxy.Proxy{ - RedisClient: redis.NewClient(&redis.Options{}), - HttpClient: &http.Client{ - Transport: http.DefaultTransport, - }, - GetOptions: getOptions, - } + proxy := getProxy(func() (npmproxy.Options, error) { + return npmproxy.Options{ + RedisPrefix: persistentOptions.RedisPrefix, + }, nil + }) - metadatas, err := prx.ListMetadata() + metadatas, err := proxy.ListMetadata() if err != nil { panic(err) } diff --git a/cli/main.go b/cli/main.go index 78fc7bc..80b396c 100644 --- a/cli/main.go +++ b/cli/main.go @@ -2,9 +2,43 @@ package cli import ( "fmt" + "net/http" "os" + + npmproxy "github.com/emeralt/npm-cache-proxy/proxy" + "github.com/go-redis/redis" ) +// global options +var persistentOptions struct { + RedisAddress string + RedisDatabase int + RedisPassword string + RedisPrefix string +} + +// initialize global options +func init() { + rootCmd.PersistentFlags().StringVar(&persistentOptions.RedisAddress, "redis-address", getEnvString("REDIS_ADDRESS", "localhost:6379"), "Redis address") + rootCmd.PersistentFlags().IntVar(&persistentOptions.RedisDatabase, "redis-database", getEnvInt("REDIS_DATABASE", "0"), "Redis database") + rootCmd.PersistentFlags().StringVar(&persistentOptions.RedisPassword, "redis-password", getEnvString("REDIS_PASSWORD", ""), "Redis password") + rootCmd.PersistentFlags().StringVar(&persistentOptions.RedisPrefix, "redis-prefix", getEnvString("REDIS_PREFIX", "ncp-"), "Redis prefix") +} + +func getProxy(getOptions func() (npmproxy.Options, error)) *npmproxy.Proxy { + return &npmproxy.Proxy{ + RedisClient: redis.NewClient(&redis.Options{ + Addr: persistentOptions.RedisAddress, + DB: persistentOptions.RedisDatabase, + Password: persistentOptions.RedisPassword, + }), + HttpClient: &http.Client{ + Transport: http.DefaultTransport, + }, + GetOptions: getOptions, + } +} + // Run starts the CLI func Run() { rootCmd.AddCommand(listCmd) diff --git a/cli/purge.go b/cli/purge.go index fd1506a..3112189 100644 --- a/cli/purge.go +++ b/cli/purge.go @@ -1,10 +1,7 @@ package cli import ( - "net/http" - npmproxy "github.com/emeralt/npm-cache-proxy/proxy" - "github.com/go-redis/redis" "github.com/spf13/cobra" ) @@ -13,13 +10,11 @@ var purgeCmd = &cobra.Command{ Use: "purge", Short: "Purge all cached packages", Run: func(cmd *cobra.Command, args []string) { - proxy := npmproxy.Proxy{ - RedisClient: redis.NewClient(&redis.Options{}), - HttpClient: &http.Client{ - Transport: http.DefaultTransport, - }, - GetOptions: getOptions, - } + proxy := getProxy(func() (npmproxy.Options, error) { + return npmproxy.Options{ + RedisPrefix: persistentOptions.RedisPrefix, + }, nil + }) err := proxy.PurgeMetadata() if err != nil { diff --git a/cli/root.go b/cli/root.go index 03f5a2c..0caa1cf 100644 --- a/cli/root.go +++ b/cli/root.go @@ -1,11 +1,9 @@ package cli import ( - "net/http" "time" npmproxy "github.com/emeralt/npm-cache-proxy/proxy" - "github.com/go-redis/redis" "github.com/spf13/cobra" ) @@ -13,28 +11,33 @@ import ( var rootCmd = &cobra.Command{ Use: "ncp", Short: "ncp is a fast npm cache proxy that stores data in Redis", - Run: func(cmd *cobra.Command, args []string) { - proxy := npmproxy.Proxy{ - RedisClient: redis.NewClient(&redis.Options{}), - HttpClient: &http.Client{ - Transport: http.DefaultTransport, - }, - GetOptions: getOptions, - } + Run: run, +} + +var rootOptions struct { + ListenAddress string + UpstreamAddress string + CacheLimit string + CacheTTL int +} - proxy.Server(npmproxy.ServerOptions{ - ListenAddress: "localhost:8080", - }).ListenAndServe() - }, +func init() { + 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") + rootCmd.Flags().IntVar(&rootOptions.CacheTTL, "cache-ttl", getEnvInt("CACHE_TTL", "3600"), "Cache expiration timeout in seconds") } -func getOptions() (npmproxy.Options, error) { - return npmproxy.Options{ - RedisPrefix: "ncp-", - RedisExpireTimeout: 1 * time.Hour, +func run(cmd *cobra.Command, args []string) { + proxy := getProxy(func() (npmproxy.Options, error) { + return npmproxy.Options{ + RedisPrefix: persistentOptions.RedisPrefix, + RedisExpireTimeout: time.Duration(rootOptions.CacheTTL) * time.Second, + UpstreamAddress: rootOptions.UpstreamAddress, + }, nil + }) - UpstreamAddress: "http://registry.npmjs.org", - ReplaceAddress: "https://registry.npmjs.org", - StaticServerAddress: "http://localhost:8080", - }, nil + proxy.Server(npmproxy.ServerOptions{ + ListenAddress: rootOptions.ListenAddress, + }).ListenAndServe() } diff --git a/cli/utils.go b/cli/utils.go new file mode 100644 index 0000000..e8a2000 --- /dev/null +++ b/cli/utils.go @@ -0,0 +1,25 @@ +package cli + +import ( + "os" + "strconv" +) + +func getEnvString(env string, def string) string { + value := os.Getenv(env) + + if value != "" { + return value + } else { + return def + } +} + +func getEnvInt(env string, def string) int { + value := getEnvString(env, def) + + // TODO: handle error + converted, _ := strconv.Atoi(value) + + return converted +} diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..33ec560 --- /dev/null +++ b/example/main.go @@ -0,0 +1,31 @@ +package main + +import ( + "net/http" + "time" + + npmproxy "github.com/emeralt/npm-cache-proxy/proxy" + "github.com/go-redis/redis" +) + +func main() { + proxy := npmproxy.Proxy{ + RedisClient: redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + DB: 0, + Password: "", + }), + HttpClient: &http.Client{}, + GetOptions: func() (npmproxy.Options, error) { + return npmproxy.Options{ + RedisPrefix: "ncp-", + RedisExpireTimeout: 1 * time.Hour, + UpstreamAddress: "https://registry.npmjs.org", + }, nil + }, + } + + proxy.Server(npmproxy.ServerOptions{ + ListenAddress: "localhost:8080", + }).ListenAndServe() +} diff --git a/go.mod b/go.mod index 25c2173..9b3e37d 100644 --- a/go.mod +++ b/go.mod @@ -3,24 +3,11 @@ module github.com/emeralt/npm-cache-proxy go 1.12 require ( - github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 // indirect github.com/gin-contrib/zap v0.0.0-20190405225521-7c4b822813e7 github.com/gin-gonic/gin v1.3.0 github.com/go-redis/redis v6.15.2+incompatible - github.com/golang/protobuf v1.3.1 // indirect - github.com/json-iterator/go v1.1.6 // indirect - github.com/mattn/go-isatty v0.0.7 // indirect - github.com/mitchellh/go-homedir v1.1.0 // indirect - github.com/onsi/ginkgo v1.8.0 // indirect - github.com/onsi/gomega v1.5.0 // indirect - github.com/pkg/errors v0.8.1 // indirect + github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/spf13/cobra v0.0.3 - github.com/spf13/viper v1.3.2 // indirect - github.com/stretchr/objx v0.2.0 // indirect + github.com/spf13/pflag v1.0.3 // indirect go.uber.org/zap v1.9.1 - golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a // indirect - golang.org/x/net v0.0.0-20190415214537-1da14a5a36f2 // indirect - golang.org/x/sync v0.0.0-20190412183630-56d357773e84 // indirect - golang.org/x/sys v0.0.0-20190415145633-3fd5a3612ccd // indirect - gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect ) diff --git a/go.sum b/go.sum index 048c069..26da0fc 100644 --- a/go.sum +++ b/go.sum @@ -1,15 +1,6 @@ -github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= -github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= -github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= -github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= -github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 h1:AzN37oI0cOS+cougNAV9szl6CVoj2RYwzS3DpUQNtlY= github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= -github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 h1:t8FVkw33L+wilf2QiWkw0UV77qRpcH/JHPKGpKa2E8g= -github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= github.com/gin-contrib/zap v0.0.0-20190405225521-7c4b822813e7 h1:v6rNWmMnVDBVMc1pUUSCTobu2p4BukiPMwoj0pLqBhA= github.com/gin-contrib/zap v0.0.0-20190405225521-7c4b822813e7/go.mod h1:pQKeeey3PeRN2SbZe1jWiIkTJkylO9hL1K0Hf4Wbtt4= github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs= @@ -18,91 +9,34 @@ github.com/go-redis/redis v6.15.2+incompatible h1:9SpNVG76gr6InJGxoZ6IuuxaCOQwDA github.com/go-redis/redis v6.15.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg= -github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= -github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= -github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= -github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= -github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= -github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY= -github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs= github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= -github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc= -github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= -github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= -github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= -github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= -github.com/onsi/ginkgo v1.8.0 h1:VkHVNpR4iVnU8XQR6DBm8BqYjN7CRzw+xKUbVVbbW9w= -github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= -github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo= -github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= -github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= -github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI= -github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= -github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8= -github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8= github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= -github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk= -github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= -github.com/spf13/viper v1.3.2 h1:VUFqw5KcqRf7i70GOzW7N+Q7+gxVBkSSqiXB12+JQ4M= -github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2 h1:EICbibRW4JNKMcY+LsWmuwob+CRS1BmdRdjphAm9mH4= github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= -github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= go.uber.org/atomic v1.3.2 h1:2Oa65PReHzfn29GpvgsYwloV9AVFHPDk8tYxt2c2tr4= go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/zap v1.9.1 h1:XCJQEf3W6eZaVwhRBof6ImoYGJSITeKWsyeh3HFu/5o= go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= -golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE= -golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190415214537-1da14a5a36f2 h1:iC0Y6EDq+rhnAePxGvJs2kzUAYcwESqdcGRPzEUfzTU= -golang.org/x/net v0.0.0-20190415214537-1da14a5a36f2/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190412183630-56d357773e84/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181221143128-b4a75ba826a6/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190415145633-3fd5a3612ccd h1:MNN7PRW7zYXd8upVO5qfKeOnQG74ivRNv7sz4k4cQMs= -golang.org/x/sys v0.0.0-20190415145633-3fd5a3612ccd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= -gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ= gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= -gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= -gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= -gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/proxy/main.go b/proxy/main.go index 78a7493..c1a523a 100644 --- a/proxy/main.go +++ b/proxy/main.go @@ -17,8 +17,5 @@ type Proxy struct { type Options struct { RedisPrefix string RedisExpireTimeout time.Duration - - UpstreamAddress string - ReplaceAddress string - StaticServerAddress string + UpstreamAddress string } diff --git a/readme.md b/readme.md index 69a0901..08a83a9 100644 --- a/readme.md +++ b/readme.md @@ -1,42 +1,81 @@ # npm-cache-proxy -![Docker Cloud Build Status](https://img.shields.io/docker/cloud/build/emeralt/npm-cache-proxy.svg?style=for-the-badge) +![Docker Cloud Build Status](https://img.shields.io/docker/cloud/build/emeralt/npm-cache-proxy.svg?style=for-the-badge) - [npm-cache-proxy](#npm-cache-proxy) - - [Download](#download) - - [Usage](#usage) - - [`ncp`](#ncp) - - [`ncp list`](#ncp-list) - - [`ncp purge`](#ncp-purge) - - [Programmatic usage](#programmatic-usage) - - [License](#license) + - [Download](#download) + - [Usage](#usage) + - [`ncp`](#ncp) + - [`ncp list`](#ncp-list) + - [`ncp purge`](#ncp-purge) + - [Programmatic usage](#programmatic-usage) + - [License](#license) ## Download + ... ## Usage ### `ncp` -Start proxy server. -| Options | Env | Default | Description | -| ------------------------- | ------------- | ----------------------- | --------------------------- | -| `-p, --port ` | `HTTP_PORT` | `8080` | Port to listen to | -| `-l, --limit ` | `CACHE_LIMIT` | - | Cached packages count limit | -| `-t, --ttl ` | `CACHE_TTL` | `3600` | Cache expiration timeout | -| `-h, --host
` | `REDIS_HOST` | `http://localhost:6379` | Redis address | -| `-d, --db ` | `REDIS_DB` | `0` | Redis database | -| `-a, --access ` | `REDIS_PASS` | - | Redis password | +Start proxy server. +| Options | Env | Default | Description | +| ----------------------------- | ------------------ | ---------------------------- | ----------------------------------- | +| `--listen
` | `LISTEN_ADDRESS` | `locahost:8080` | Address to listen | +| `--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 | +| `--redis-address
` | `REDIS_ADDRESS` | `http://localhost:6379` | Redis address | +| `--redis-database ` | `REDIS_DATABASE` | `0` | Redis database | +| `--redis-password ` | `REDIS_PASS` | - | Redis password | +| `--redis-prefix ` | `REDIS_PREFIX` | `ncp-` | Redis keys prefix | ### `ncp list` + List cached packages. ### `ncp purge` -Purge cached pacakges. + +Purge cached packages. ## Programmatic usage -... + +```golang +package main + +import ( + "net/http" + "time" + + npmproxy "github.com/emeralt/npm-cache-proxy/proxy" + "github.com/go-redis/redis" +) + +func main() { + proxy := npmproxy.Proxy{ + RedisClient: redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + DB: 0, + Password: "", + }), + HttpClient: &http.Client{}, + GetOptions: func() (npmproxy.Options, error) { + return npmproxy.Options{ + RedisPrefix: "ncp-", + RedisExpireTimeout: 1 * time.Hour, + UpstreamAddress: "https://registry.npmjs.org", + }, nil + }, + } + + proxy.Server(npmproxy.ServerOptions{ + ListenAddress: "localhost:8080", + }).ListenAndServe() +} +``` ## License + [MIT](./license)