perf: move dynamic options reciever to server, enable Cache-Control HTTP header
This commit is contained in:
parent
f821e794f8
commit
62e64e5154
10
cli/list.go
10
cli/list.go
@ -12,13 +12,11 @@ var listCmd = &cobra.Command{
|
|||||||
Use: "list",
|
Use: "list",
|
||||||
Short: "List all cached paths",
|
Short: "List all cached paths",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
proxy := getProxy(func() (npmproxy.Options, error) {
|
proxy := getProxy()
|
||||||
return npmproxy.Options{
|
|
||||||
DatabasePrefix: persistentOptions.RedisPrefix,
|
|
||||||
}, nil
|
|
||||||
})
|
|
||||||
|
|
||||||
metadatas, err := proxy.ListCachedPaths()
|
metadatas, err := proxy.ListCachedPaths(npmproxy.Options{
|
||||||
|
DatabasePrefix: persistentOptions.RedisPrefix,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ func init() {
|
|||||||
rootCmd.PersistentFlags().StringVar(&persistentOptions.RedisPrefix, "redis-prefix", getEnvString("REDIS_PREFIX", "ncp-"), "Redis prefix")
|
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{
|
return &npmproxy.Proxy{
|
||||||
Database: npmproxy.DatabaseRedis{
|
Database: npmproxy.DatabaseRedis{
|
||||||
Client: redis.NewClient(&redis.Options{
|
Client: redis.NewClient(&redis.Options{
|
||||||
@ -37,7 +37,6 @@ func getProxy(getOptions func() (npmproxy.Options, error)) *npmproxy.Proxy {
|
|||||||
HttpClient: &http.Client{
|
HttpClient: &http.Client{
|
||||||
Transport: http.DefaultTransport,
|
Transport: http.DefaultTransport,
|
||||||
},
|
},
|
||||||
GetOptions: getOptions,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
10
cli/purge.go
10
cli/purge.go
@ -10,13 +10,11 @@ var purgeCmd = &cobra.Command{
|
|||||||
Use: "purge",
|
Use: "purge",
|
||||||
Short: "Purge all cached paths",
|
Short: "Purge all cached paths",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
proxy := getProxy(func() (npmproxy.Options, error) {
|
proxy := getProxy()
|
||||||
return npmproxy.Options{
|
|
||||||
DatabasePrefix: persistentOptions.RedisPrefix,
|
|
||||||
}, nil
|
|
||||||
})
|
|
||||||
|
|
||||||
err := proxy.PurgeCachedPaths()
|
err := proxy.PurgeCachedPaths(npmproxy.Options{
|
||||||
|
DatabasePrefix: persistentOptions.RedisPrefix,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
21
cli/root.go
21
cli/root.go
@ -1,6 +1,7 @@
|
|||||||
package cli
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
npmproxy "github.com/emeralt/npm-cache-proxy/proxy"
|
npmproxy "github.com/emeralt/npm-cache-proxy/proxy"
|
||||||
@ -31,16 +32,24 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func run(cmd *cobra.Command, args []string) {
|
func run(cmd *cobra.Command, args []string) {
|
||||||
proxy := getProxy(func() (npmproxy.Options, error) {
|
proxy := getProxy()
|
||||||
|
|
||||||
|
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{
|
return npmproxy.Options{
|
||||||
DatabasePrefix: persistentOptions.RedisPrefix,
|
DatabasePrefix: persistentOptions.RedisPrefix,
|
||||||
DatabaseExpiration: time.Duration(rootOptions.CacheTTL) * time.Second,
|
DatabaseExpiration: time.Duration(rootOptions.CacheTTL) * time.Second,
|
||||||
UpstreamAddress: rootOptions.UpstreamAddress,
|
UpstreamAddress: rootOptions.UpstreamAddress,
|
||||||
}, nil
|
}, nil
|
||||||
})
|
},
|
||||||
|
|
||||||
proxy.Server(npmproxy.ServerOptions{
|
|
||||||
ListenAddress: rootOptions.ListenAddress,
|
|
||||||
Silent: rootOptions.Silent,
|
|
||||||
}).ListenAndServe()
|
}).ListenAndServe()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,10 @@ func main() {
|
|||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
HttpClient: &http.Client{},
|
HttpClient: &http.Client{},
|
||||||
|
}
|
||||||
|
|
||||||
|
proxy.Server(npmproxy.ServerOptions{
|
||||||
|
ListenAddress: "localhost:8080",
|
||||||
GetOptions: func() (npmproxy.Options, error) {
|
GetOptions: func() (npmproxy.Options, error) {
|
||||||
return npmproxy.Options{
|
return npmproxy.Options{
|
||||||
DatabasePrefix: "ncp-",
|
DatabasePrefix: "ncp-",
|
||||||
@ -25,9 +29,5 @@ func main() {
|
|||||||
UpstreamAddress: "https://registry.npmjs.org",
|
UpstreamAddress: "https://registry.npmjs.org",
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
}
|
|
||||||
|
|
||||||
proxy.Server(npmproxy.ServerOptions{
|
|
||||||
ListenAddress: "localhost:8080",
|
|
||||||
}).ListenAndServe()
|
}).ListenAndServe()
|
||||||
}
|
}
|
||||||
|
@ -10,12 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// GetCachedPath returns cached upstream response for a given url path.
|
// GetCachedPath returns cached upstream response for a given url path.
|
||||||
func (proxy Proxy) GetCachedPath(path string, request *http.Request) ([]byte, error) {
|
func (proxy Proxy) GetCachedPath(options Options, path string, request *http.Request) ([]byte, error) {
|
||||||
options, err := proxy.GetOptions()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
key := options.DatabasePrefix + path
|
key := options.DatabasePrefix + path
|
||||||
|
|
||||||
// get package from database
|
// get package from database
|
||||||
@ -72,12 +67,7 @@ func (proxy Proxy) GetCachedPath(path string, request *http.Request) ([]byte, er
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListCachedPaths returns list of all cached url paths.
|
// ListCachedPaths returns list of all cached url paths.
|
||||||
func (proxy Proxy) ListCachedPaths() ([]string, error) {
|
func (proxy Proxy) ListCachedPaths(options Options) ([]string, error) {
|
||||||
options, err := proxy.GetOptions()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
metadata, err := proxy.Database.Keys(options.DatabasePrefix)
|
metadata, err := proxy.Database.Keys(options.DatabasePrefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -92,12 +82,7 @@ func (proxy Proxy) ListCachedPaths() ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// PurgeCachedPaths deletes all cached url paths.
|
// PurgeCachedPaths deletes all cached url paths.
|
||||||
func (proxy Proxy) PurgeCachedPaths() error {
|
func (proxy Proxy) PurgeCachedPaths(options Options) error {
|
||||||
options, err := proxy.GetOptions()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
metadata, err := proxy.Database.Keys(options.DatabasePrefix)
|
metadata, err := proxy.Database.Keys(options.DatabasePrefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -14,8 +14,6 @@ import (
|
|||||||
type Proxy struct {
|
type Proxy struct {
|
||||||
Database Database
|
Database Database
|
||||||
HttpClient *http.Client
|
HttpClient *http.Client
|
||||||
|
|
||||||
GetOptions func() (Options, error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Options provides dynamic options for Proxy.
|
// Options provides dynamic options for Proxy.
|
||||||
|
@ -14,10 +14,14 @@ import (
|
|||||||
type ServerOptions struct {
|
type ServerOptions struct {
|
||||||
ListenAddress string
|
ListenAddress string
|
||||||
Silent bool
|
Silent bool
|
||||||
|
|
||||||
|
GetOptions func() (Options, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Server creates http proxy server
|
// Server creates http proxy server
|
||||||
func (proxy Proxy) Server(options ServerOptions) *http.Server {
|
func (proxy Proxy) Server(options ServerOptions) *http.Server {
|
||||||
|
gin.SetMode("release")
|
||||||
|
|
||||||
router := gin.New()
|
router := gin.New()
|
||||||
|
|
||||||
if options.Silent {
|
if options.Silent {
|
||||||
@ -28,9 +32,9 @@ func (proxy Proxy) Server(options ServerOptions) *http.Server {
|
|||||||
router.Use(ginzap.RecoveryWithZap(logger, true))
|
router.Use(ginzap.RecoveryWithZap(logger, true))
|
||||||
}
|
}
|
||||||
|
|
||||||
router.GET("/:scope/:name", proxy.getPackageHandler)
|
router.GET("/:scope/:name", proxy.getPackageHandler(options))
|
||||||
router.GET("/:scope", proxy.getPackageHandler)
|
router.GET("/:scope", proxy.getPackageHandler(options))
|
||||||
router.NoRoute(proxy.noRouteHandler)
|
router.NoRoute(proxy.noRouteHandler(options))
|
||||||
|
|
||||||
return &http.Server{
|
return &http.Server{
|
||||||
Handler: router,
|
Handler: router,
|
||||||
@ -38,31 +42,34 @@ func (proxy Proxy) Server(options ServerOptions) *http.Server {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (proxy Proxy) getPackageHandler(c *gin.Context) {
|
func (proxy Proxy) getPackageHandler(options ServerOptions) gin.HandlerFunc {
|
||||||
pkg, err := proxy.GetCachedPath(c.Request.URL.Path, c.Request)
|
return func(c *gin.Context) {
|
||||||
|
options, err := options.GetOptions()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithError(500, err)
|
c.AbortWithError(500, err)
|
||||||
} else {
|
} else {
|
||||||
// c.Header("Content-Encoding", "gzip")
|
pkg, err := proxy.GetCachedPath(options, c.Request.URL.Path, c.Request)
|
||||||
c.Data(200, "application/json", pkg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (proxy Proxy) getTarballHabdler(c *gin.Context) {
|
|
||||||
pkg, err := proxy.GetCachedPath(c.Request.URL.Path, c.Request)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithError(500, err)
|
c.AbortWithError(500, err)
|
||||||
} else {
|
} else {
|
||||||
|
c.Header("Cache-Control", "public, max-age="+string(int(options.DatabaseExpiration.Seconds())))
|
||||||
c.Data(200, "application/json", pkg)
|
c.Data(200, "application/json", pkg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (proxy Proxy) noRouteHandler(c *gin.Context) {
|
func (proxy Proxy) noRouteHandler(options ServerOptions) gin.HandlerFunc {
|
||||||
|
tarballHandler := proxy.getPackageHandler(options)
|
||||||
|
|
||||||
|
return func(c *gin.Context) {
|
||||||
if strings.Contains(c.Request.URL.Path, ".tgz") {
|
if strings.Contains(c.Request.URL.Path, ".tgz") {
|
||||||
proxy.getTarballHabdler(c)
|
// get tarball
|
||||||
|
tarballHandler(c)
|
||||||
} else if c.Request.URL.Path == "/" {
|
} else if c.Request.URL.Path == "/" {
|
||||||
|
// get health
|
||||||
err := proxy.Database.Health()
|
err := proxy.Database.Health()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -71,7 +78,8 @@ func (proxy Proxy) noRouteHandler(c *gin.Context) {
|
|||||||
c.AbortWithStatusJSON(200, gin.H{"ok": true})
|
c.AbortWithStatusJSON(200, gin.H{"ok": true})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
options, err := proxy.GetOptions()
|
// redirect
|
||||||
|
options, err := options.GetOptions()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithStatusJSON(500, err)
|
c.AbortWithStatusJSON(500, err)
|
||||||
@ -80,3 +88,4 @@ func (proxy Proxy) noRouteHandler(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
21
readme.md
21
readme.md
@ -107,23 +107,29 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
npmproxy "github.com/emeralt/npm-cache-proxy/proxy"
|
npmproxy "github.com/emeralt/npm-cache-proxy/proxy"
|
||||||
redis "github.com/go-redis/redis"
|
"github.com/go-redis/redis"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// create proxy
|
||||||
proxy := npmproxy.Proxy{
|
proxy := npmproxy.Proxy{
|
||||||
// you can provide you own Database
|
// use redis as database
|
||||||
// or use an existing one
|
|
||||||
Database: npmproxy.DatabaseRedis{
|
Database: npmproxy.DatabaseRedis{
|
||||||
|
// see github.com/go-redis/redis
|
||||||
Client: redis.NewClient(&redis.Options{
|
Client: redis.NewClient(&redis.Options{
|
||||||
Addr: "localhost:6379",
|
Addr: "localhost:6379",
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
|
||||||
// allows to reuse tcp connections
|
// reuse connections
|
||||||
HttpClient: &http.Client{},
|
HttpClient: &http.Client{},
|
||||||
|
}
|
||||||
|
|
||||||
// allows to get options dynamically
|
// create and start server
|
||||||
|
proxy.Server(npmproxy.ServerOptions{
|
||||||
|
ListenAddress: "localhost:8080",
|
||||||
|
|
||||||
|
// allow fetching options dynamically on each request
|
||||||
GetOptions: func() (npmproxy.Options, error) {
|
GetOptions: func() (npmproxy.Options, error) {
|
||||||
return npmproxy.Options{
|
return npmproxy.Options{
|
||||||
DatabasePrefix: "ncp-",
|
DatabasePrefix: "ncp-",
|
||||||
@ -131,11 +137,6 @@ func main() {
|
|||||||
UpstreamAddress: "https://registry.npmjs.org",
|
UpstreamAddress: "https://registry.npmjs.org",
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
}
|
|
||||||
|
|
||||||
// listen on http://localhost:8080
|
|
||||||
proxy.Server(npmproxy.ServerOptions{
|
|
||||||
ListenAddress: "localhost:8080",
|
|
||||||
}).ListenAndServe()
|
}).ListenAndServe()
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user