perf: move dynamic options reciever to server, enable Cache-Control HTTP header
This commit is contained in:
10
cli/list.go
10
cli/list.go
@@ -12,13 +12,11 @@ var listCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List all cached paths",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
proxy := getProxy(func() (npmproxy.Options, error) {
|
||||
return npmproxy.Options{
|
||||
DatabasePrefix: persistentOptions.RedisPrefix,
|
||||
}, nil
|
||||
})
|
||||
proxy := getProxy()
|
||||
|
||||
metadatas, err := proxy.ListCachedPaths()
|
||||
metadatas, err := proxy.ListCachedPaths(npmproxy.Options{
|
||||
DatabasePrefix: persistentOptions.RedisPrefix,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ func init() {
|
||||
rootCmd.PersistentFlags().StringVar(&persistentOptions.RedisPrefix, "redis-prefix", getEnvString("REDIS_PREFIX", "ncp-"), "Redis prefix")
|
||||
}
|
||||
|
||||
func getProxy(getOptions func() (npmproxy.Options, error)) *npmproxy.Proxy {
|
||||
func getProxy() *npmproxy.Proxy {
|
||||
return &npmproxy.Proxy{
|
||||
Database: npmproxy.DatabaseRedis{
|
||||
Client: redis.NewClient(&redis.Options{
|
||||
@@ -37,7 +37,6 @@ func getProxy(getOptions func() (npmproxy.Options, error)) *npmproxy.Proxy {
|
||||
HttpClient: &http.Client{
|
||||
Transport: http.DefaultTransport,
|
||||
},
|
||||
GetOptions: getOptions,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
10
cli/purge.go
10
cli/purge.go
@@ -10,13 +10,11 @@ var purgeCmd = &cobra.Command{
|
||||
Use: "purge",
|
||||
Short: "Purge all cached paths",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
proxy := getProxy(func() (npmproxy.Options, error) {
|
||||
return npmproxy.Options{
|
||||
DatabasePrefix: persistentOptions.RedisPrefix,
|
||||
}, nil
|
||||
})
|
||||
proxy := getProxy()
|
||||
|
||||
err := proxy.PurgeCachedPaths()
|
||||
err := proxy.PurgeCachedPaths(npmproxy.Options{
|
||||
DatabasePrefix: persistentOptions.RedisPrefix,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
25
cli/root.go
25
cli/root.go
@@ -1,6 +1,7 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
npmproxy "github.com/emeralt/npm-cache-proxy/proxy"
|
||||
@@ -31,16 +32,24 @@ func init() {
|
||||
}
|
||||
|
||||
func run(cmd *cobra.Command, args []string) {
|
||||
proxy := getProxy(func() (npmproxy.Options, error) {
|
||||
return npmproxy.Options{
|
||||
DatabasePrefix: persistentOptions.RedisPrefix,
|
||||
DatabaseExpiration: time.Duration(rootOptions.CacheTTL) * time.Second,
|
||||
UpstreamAddress: rootOptions.UpstreamAddress,
|
||||
}, nil
|
||||
})
|
||||
proxy := getProxy()
|
||||
|
||||
proxy.Server(npmproxy.ServerOptions{
|
||||
log.Print("Listening on " + rootOptions.ListenAddress)
|
||||
|
||||
err := proxy.Server(npmproxy.ServerOptions{
|
||||
ListenAddress: rootOptions.ListenAddress,
|
||||
Silent: rootOptions.Silent,
|
||||
|
||||
GetOptions: func() (npmproxy.Options, error) {
|
||||
return npmproxy.Options{
|
||||
DatabasePrefix: persistentOptions.RedisPrefix,
|
||||
DatabaseExpiration: time.Duration(rootOptions.CacheTTL) * time.Second,
|
||||
UpstreamAddress: rootOptions.UpstreamAddress,
|
||||
}, nil
|
||||
},
|
||||
}).ListenAndServe()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user