-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6abde80
commit e5b40a7
Showing
11 changed files
with
191 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,43 @@ | ||
package cli | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
npmproxy "github.com/emeralt/npm-cache-proxy/proxy" | ||
"github.com/go-redis/redis" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// start a server | ||
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.