Skip to content

Commit 838154c

Browse files
committed
feat: database as plugin
1 parent ab36f4f commit 838154c

File tree

10 files changed

+82
-41
lines changed

10 files changed

+82
-41
lines changed

cli/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var listCmd = &cobra.Command{
1414
Run: func(cmd *cobra.Command, args []string) {
1515
proxy := getProxy(func() (npmproxy.Options, error) {
1616
return npmproxy.Options{
17-
RedisPrefix: persistentOptions.RedisPrefix,
17+
DatabasePrefix: persistentOptions.RedisPrefix,
1818
}, nil
1919
})
2020

cli/main.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ func init() {
2727

2828
func getProxy(getOptions func() (npmproxy.Options, error)) *npmproxy.Proxy {
2929
return &npmproxy.Proxy{
30-
RedisClient: redis.NewClient(&redis.Options{
31-
Addr: persistentOptions.RedisAddress,
32-
DB: persistentOptions.RedisDatabase,
33-
Password: persistentOptions.RedisPassword,
34-
}),
30+
Database: npmproxy.DatabaseRedis{
31+
Client: redis.NewClient(&redis.Options{
32+
Addr: persistentOptions.RedisAddress,
33+
DB: persistentOptions.RedisDatabase,
34+
Password: persistentOptions.RedisPassword,
35+
}),
36+
},
3537
HttpClient: &http.Client{
3638
Transport: http.DefaultTransport,
3739
},

cli/purge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var purgeCmd = &cobra.Command{
1212
Run: func(cmd *cobra.Command, args []string) {
1313
proxy := getProxy(func() (npmproxy.Options, error) {
1414
return npmproxy.Options{
15-
RedisPrefix: persistentOptions.RedisPrefix,
15+
DatabasePrefix: persistentOptions.RedisPrefix,
1616
}, nil
1717
})
1818

cli/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func init() {
3131
func run(cmd *cobra.Command, args []string) {
3232
proxy := getProxy(func() (npmproxy.Options, error) {
3333
return npmproxy.Options{
34-
RedisPrefix: persistentOptions.RedisPrefix,
35-
RedisExpireTimeout: time.Duration(rootOptions.CacheTTL) * time.Second,
34+
DatabasePrefix: persistentOptions.RedisPrefix,
35+
DatabaseExpiration: time.Duration(rootOptions.CacheTTL) * time.Second,
3636
UpstreamAddress: rootOptions.UpstreamAddress,
3737
}, nil
3838
})

example/main.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ import (
1010

1111
func main() {
1212
proxy := npmproxy.Proxy{
13-
RedisClient: redis.NewClient(&redis.Options{
14-
Addr: "localhost:6379",
15-
DB: 0,
16-
Password: "",
17-
}),
13+
Database: npmproxy.DatabaseRedis{
14+
Client: redis.NewClient(&redis.Options{
15+
Addr: "localhost:6379",
16+
DB: 0,
17+
Password: "",
18+
}),
19+
},
1820
HttpClient: &http.Client{},
1921
GetOptions: func() (npmproxy.Options, error) {
2022
return npmproxy.Options{
21-
RedisPrefix: "ncp-",
22-
RedisExpireTimeout: 1 * time.Hour,
23+
DatabasePrefix: "ncp-",
24+
DatabaseExpiration: 1 * time.Hour,
2325
UpstreamAddress: "https://registry.npmjs.org",
2426
}, nil
2527
},

proxy/cache.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ func (proxy Proxy) GetMetadata(name string, originalPath string, header http.Hea
1313
return nil, err
1414
}
1515

16-
// get package from redis
17-
pkg, err := proxy.RedisClient.Get(options.RedisPrefix + name).Result()
16+
// get package from database
17+
pkg, err := proxy.Database.Get(options.DatabasePrefix + name)
1818

1919
// either package doesn't exist or there's some other problem
2020
if err != nil {
@@ -50,11 +50,11 @@ func (proxy Proxy) GetMetadata(name string, originalPath string, header http.Hea
5050
pkg = string(body)
5151

5252
// save to redis
53-
_, err = proxy.RedisClient.Set(
54-
options.RedisPrefix+name,
53+
err = proxy.Database.Set(
54+
options.DatabasePrefix+name,
5555
pkg,
56-
options.RedisExpireTimeout,
57-
).Result()
56+
options.DatabaseExpiration,
57+
)
5858
if err != nil {
5959
return nil, err
6060
}
@@ -74,14 +74,14 @@ func (proxy Proxy) ListMetadata() ([]string, error) {
7474
return nil, err
7575
}
7676

77-
metadata, err := proxy.RedisClient.Keys(options.RedisPrefix + "*").Result()
77+
metadata, err := proxy.Database.Keys(options.DatabasePrefix)
7878
if err != nil {
7979
return nil, err
8080
}
8181

8282
deprefixedMetadata := make([]string, 0)
8383
for _, record := range metadata {
84-
deprefixedMetadata = append(deprefixedMetadata, strings.Replace(record, options.RedisPrefix, "", 1))
84+
deprefixedMetadata = append(deprefixedMetadata, strings.Replace(record, options.DatabasePrefix, "", 1))
8585
}
8686

8787
return deprefixedMetadata, nil
@@ -94,13 +94,13 @@ func (proxy Proxy) PurgeMetadata() error {
9494
return err
9595
}
9696

97-
metadata, err := proxy.RedisClient.Keys(options.RedisPrefix + "*").Result()
97+
metadata, err := proxy.Database.Keys(options.DatabasePrefix)
9898
if err != nil {
9999
return err
100100
}
101101

102102
for _, record := range metadata {
103-
_, err := proxy.RedisClient.Del(record).Result()
103+
err := proxy.Database.Delete(record)
104104
if err != nil {
105105
return err
106106
}

proxy/database_redis.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package proxy
2+
3+
import (
4+
"time"
5+
6+
"github.com/go-redis/redis"
7+
)
8+
9+
type DatabaseRedis struct {
10+
Client *redis.Client
11+
}
12+
13+
func (db DatabaseRedis) Get(key string) (string, error) {
14+
return db.Client.Get(key).Result()
15+
}
16+
17+
func (db DatabaseRedis) Set(key string, value string, expiration time.Duration) error {
18+
return db.Client.Set(key, value, expiration).Err()
19+
}
20+
21+
func (db DatabaseRedis) Delete(key string) error {
22+
return db.Client.Del(key).Err()
23+
}
24+
25+
func (db DatabaseRedis) Keys(prefix string) ([]string, error) {
26+
return db.Client.Keys(prefix + "*").Result()
27+
}
28+
29+
func (db DatabaseRedis) Health() error {
30+
return db.Client.Ping().Err()
31+
}

proxy/main.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,25 @@ package proxy
33
import (
44
"net/http"
55
"time"
6-
7-
"github.com/go-redis/redis"
86
)
97

108
type Proxy struct {
11-
RedisClient *redis.Client
12-
HttpClient *http.Client
9+
Database Database
10+
HttpClient *http.Client
1311

1412
GetOptions func() (Options, error)
1513
}
1614

1715
type Options struct {
18-
RedisPrefix string
19-
RedisExpireTimeout time.Duration
16+
DatabasePrefix string
17+
DatabaseExpiration time.Duration
2018
UpstreamAddress string
2119
}
20+
21+
type Database interface {
22+
Get(key string) (string, error)
23+
Set(key string, value string, ttl time.Duration) error
24+
Delete(key string) error
25+
Keys(prefix string) ([]string, error)
26+
Health() error
27+
}

proxy/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (proxy Proxy) NoRouteHandler(c *gin.Context) {
5252
// } else
5353

5454
if c.Request.URL.Path == "/" {
55-
_, err := proxy.RedisClient.Ping().Result()
55+
err := proxy.Database.Health()
5656

5757
if err != nil {
5858
c.AbortWithStatusJSON(503, err)

readme.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Start proxy server.
2828
| `--cache-ttl <timeout>` | `CACHE_TTL` | `3600` | Cache expiration timeout in seconds |
2929
| `--redis-address <address>` | `REDIS_ADDRESS` | `http://localhost:6379` | Redis address |
3030
| `--redis-database <database>` | `REDIS_DATABASE` | `0` | Redis database |
31-
| `--redis-password <password>` | `REDIS_PASSWORD` | - | Redis password |
31+
| `--redis-password <password>` | `REDIS_PASSWORD` | - | Redis password |
3232
| `--redis-prefix <prefix>` | `REDIS_PREFIX` | `ncp-` | Redis keys prefix |
3333

3434
### `ncp list`
@@ -54,16 +54,16 @@ import (
5454

5555
func main() {
5656
proxy := npmproxy.Proxy{
57-
RedisClient: redis.NewClient(&redis.Options{
58-
Addr: "localhost:6379",
59-
DB: 0,
60-
Password: "",
61-
}),
57+
Database: npmproxy.DatabaseRedis{
58+
Client: redis.NewClient(&redis.Options{
59+
Addr: "localhost:6379",
60+
}),
61+
},
6262
HttpClient: &http.Client{},
6363
GetOptions: func() (npmproxy.Options, error) {
6464
return npmproxy.Options{
65-
RedisPrefix: "ncp-",
66-
RedisExpireTimeout: 1 * time.Hour,
65+
DatabasePrefix: "ncp-",
66+
DatabaseExpiration: 1 * time.Hour,
6767
UpstreamAddress: "https://registry.npmjs.org",
6868
}, nil
6969
},

0 commit comments

Comments
 (0)