Skip to content

Commit

Permalink
perf: move dynamic options reciever to server, enable Cache-Control H…
Browse files Browse the repository at this point in the history
…TTP header
  • Loading branch information
stackdumper committed Apr 23, 2019
1 parent f821e79 commit 62e64e5
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 91 deletions.
10 changes: 4 additions & 6 deletions cli/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -37,7 +37,6 @@ func getProxy(getOptions func() (npmproxy.Options, error)) *npmproxy.Proxy {
HttpClient: &http.Client{
Transport: http.DefaultTransport,
},
GetOptions: getOptions,
}
}

Expand Down
10 changes: 4 additions & 6 deletions cli/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
27 changes: 18 additions & 9 deletions cli/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"log"
"time"

npmproxy "github.com/emeralt/npm-cache-proxy/proxy"
Expand Down Expand Up @@ -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.Server(npmproxy.ServerOptions{
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{
DatabasePrefix: persistentOptions.RedisPrefix,
DatabaseExpiration: time.Duration(rootOptions.CacheTTL) * time.Second,
UpstreamAddress: rootOptions.UpstreamAddress,
}, nil
},
}).ListenAndServe()

if err != nil {
log.Fatal(err)
}
}
8 changes: 4 additions & 4 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ func main() {
}),
},
HttpClient: &http.Client{},
}

proxy.Server(npmproxy.ServerOptions{
ListenAddress: "localhost:8080",
GetOptions: func() (npmproxy.Options, error) {
return npmproxy.Options{
DatabasePrefix: "ncp-",
DatabaseExpiration: 1 * time.Hour,
UpstreamAddress: "https://registry.npmjs.org",
}, nil
},
}

proxy.Server(npmproxy.ServerOptions{
ListenAddress: "localhost:8080",
}).ListenAndServe()
}
21 changes: 3 additions & 18 deletions proxy/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ import (
)

// GetCachedPath returns cached upstream response for a given url path.
func (proxy Proxy) GetCachedPath(path string, request *http.Request) ([]byte, error) {
options, err := proxy.GetOptions()
if err != nil {
return nil, err
}

func (proxy Proxy) GetCachedPath(options Options, path string, request *http.Request) ([]byte, error) {
key := options.DatabasePrefix + path

// get package from database
Expand Down Expand Up @@ -72,12 +67,7 @@ func (proxy Proxy) GetCachedPath(path string, request *http.Request) ([]byte, er
}

// ListCachedPaths returns list of all cached url paths.
func (proxy Proxy) ListCachedPaths() ([]string, error) {
options, err := proxy.GetOptions()
if err != nil {
return nil, err
}

func (proxy Proxy) ListCachedPaths(options Options) ([]string, error) {
metadata, err := proxy.Database.Keys(options.DatabasePrefix)
if err != nil {
return nil, err
Expand All @@ -92,12 +82,7 @@ func (proxy Proxy) ListCachedPaths() ([]string, error) {
}

// PurgeCachedPaths deletes all cached url paths.
func (proxy Proxy) PurgeCachedPaths() error {
options, err := proxy.GetOptions()
if err != nil {
return err
}

func (proxy Proxy) PurgeCachedPaths(options Options) error {
metadata, err := proxy.Database.Keys(options.DatabasePrefix)
if err != nil {
return err
Expand Down
2 changes: 0 additions & 2 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
type Proxy struct {
Database Database
HttpClient *http.Client

GetOptions func() (Options, error)
}

// Options provides dynamic options for Proxy.
Expand Down
77 changes: 43 additions & 34 deletions proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ import (
type ServerOptions struct {
ListenAddress string
Silent bool

GetOptions func() (Options, error)
}

// Server creates http proxy server
func (proxy Proxy) Server(options ServerOptions) *http.Server {
gin.SetMode("release")

router := gin.New()

if options.Silent {
Expand All @@ -28,55 +32,60 @@ func (proxy Proxy) Server(options ServerOptions) *http.Server {
router.Use(ginzap.RecoveryWithZap(logger, true))
}

router.GET("/:scope/:name", proxy.getPackageHandler)
router.GET("/:scope", proxy.getPackageHandler)
router.NoRoute(proxy.noRouteHandler)
router.GET("/:scope/:name", proxy.getPackageHandler(options))
router.GET("/:scope", proxy.getPackageHandler(options))
router.NoRoute(proxy.noRouteHandler(options))

return &http.Server{
Handler: router,
Addr: options.ListenAddress,
}
}

func (proxy Proxy) getPackageHandler(c *gin.Context) {
pkg, err := proxy.GetCachedPath(c.Request.URL.Path, c.Request)

if err != nil {
c.AbortWithError(500, err)
} else {
// c.Header("Content-Encoding", "gzip")
c.Data(200, "application/json", pkg)
}
}
func (proxy Proxy) getPackageHandler(options ServerOptions) gin.HandlerFunc {
return func(c *gin.Context) {
options, err := options.GetOptions()

func (proxy Proxy) getTarballHabdler(c *gin.Context) {
pkg, err := proxy.GetCachedPath(c.Request.URL.Path, c.Request)
if err != nil {
c.AbortWithError(500, err)
} else {
pkg, err := proxy.GetCachedPath(options, c.Request.URL.Path, c.Request)

if err != nil {
c.AbortWithError(500, err)
} else {
c.Data(200, "application/json", pkg)
if err != nil {
c.AbortWithError(500, err)
} else {
c.Header("Cache-Control", "public, max-age="+string(int(options.DatabaseExpiration.Seconds())))
c.Data(200, "application/json", pkg)
}
}
}
}

func (proxy Proxy) noRouteHandler(c *gin.Context) {
if strings.Contains(c.Request.URL.Path, ".tgz") {
proxy.getTarballHabdler(c)
} else if c.Request.URL.Path == "/" {
err := proxy.Database.Health()
func (proxy Proxy) noRouteHandler(options ServerOptions) gin.HandlerFunc {
tarballHandler := proxy.getPackageHandler(options)

if err != nil {
c.AbortWithStatusJSON(503, err)
} else {
c.AbortWithStatusJSON(200, gin.H{"ok": true})
}
} else {
options, err := proxy.GetOptions()
return func(c *gin.Context) {
if strings.Contains(c.Request.URL.Path, ".tgz") {
// get tarball
tarballHandler(c)
} else if c.Request.URL.Path == "/" {
// get health
err := proxy.Database.Health()

if err != nil {
c.AbortWithStatusJSON(500, err)
if err != nil {
c.AbortWithStatusJSON(503, err)
} else {
c.AbortWithStatusJSON(200, gin.H{"ok": true})
}
} else {
c.Redirect(http.StatusTemporaryRedirect, options.UpstreamAddress+c.Request.URL.Path)
// redirect
options, err := options.GetOptions()

if err != nil {
c.AbortWithStatusJSON(500, err)
} else {
c.Redirect(http.StatusTemporaryRedirect, options.UpstreamAddress+c.Request.URL.Path)
}
}
}
}
21 changes: 11 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,35 +107,36 @@ import (
"time"

npmproxy "github.com/emeralt/npm-cache-proxy/proxy"
redis "github.com/go-redis/redis"
"github.com/go-redis/redis"
)

func main() {
// create proxy
proxy := npmproxy.Proxy{
// you can provide you own Database
// or use an existing one
// use redis as database
Database: npmproxy.DatabaseRedis{
// see github.com/go-redis/redis
Client: redis.NewClient(&redis.Options{
Addr: "localhost:6379",
}),
},

// allows to reuse tcp connections
// reuse connections
HttpClient: &http.Client{},
}

// create and start server
proxy.Server(npmproxy.ServerOptions{
ListenAddress: "localhost:8080",

// allows to get options dynamically
// allow fetching options dynamically on each request
GetOptions: func() (npmproxy.Options, error) {
return npmproxy.Options{
DatabasePrefix: "ncp-",
DatabaseExpiration: 1 * time.Hour,
UpstreamAddress: "https://registry.npmjs.org",
}, nil
},
}

// listen on http://localhost:8080
proxy.Server(npmproxy.ServerOptions{
ListenAddress: "localhost:8080",
}).ListenAndServe()
}
```
Expand Down

0 comments on commit 62e64e5

Please sign in to comment.