feat: database as plugin
This commit is contained in:
parent
ab36f4f081
commit
838154ce9c
@ -14,7 +14,7 @@ var listCmd = &cobra.Command{
|
|||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
proxy := getProxy(func() (npmproxy.Options, error) {
|
proxy := getProxy(func() (npmproxy.Options, error) {
|
||||||
return npmproxy.Options{
|
return npmproxy.Options{
|
||||||
RedisPrefix: persistentOptions.RedisPrefix,
|
DatabasePrefix: persistentOptions.RedisPrefix,
|
||||||
}, nil
|
}, nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -27,11 +27,13 @@ func init() {
|
|||||||
|
|
||||||
func getProxy(getOptions func() (npmproxy.Options, error)) *npmproxy.Proxy {
|
func getProxy(getOptions func() (npmproxy.Options, error)) *npmproxy.Proxy {
|
||||||
return &npmproxy.Proxy{
|
return &npmproxy.Proxy{
|
||||||
RedisClient: redis.NewClient(&redis.Options{
|
Database: npmproxy.DatabaseRedis{
|
||||||
|
Client: redis.NewClient(&redis.Options{
|
||||||
Addr: persistentOptions.RedisAddress,
|
Addr: persistentOptions.RedisAddress,
|
||||||
DB: persistentOptions.RedisDatabase,
|
DB: persistentOptions.RedisDatabase,
|
||||||
Password: persistentOptions.RedisPassword,
|
Password: persistentOptions.RedisPassword,
|
||||||
}),
|
}),
|
||||||
|
},
|
||||||
HttpClient: &http.Client{
|
HttpClient: &http.Client{
|
||||||
Transport: http.DefaultTransport,
|
Transport: http.DefaultTransport,
|
||||||
},
|
},
|
||||||
|
@ -12,7 +12,7 @@ var purgeCmd = &cobra.Command{
|
|||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
proxy := getProxy(func() (npmproxy.Options, error) {
|
proxy := getProxy(func() (npmproxy.Options, error) {
|
||||||
return npmproxy.Options{
|
return npmproxy.Options{
|
||||||
RedisPrefix: persistentOptions.RedisPrefix,
|
DatabasePrefix: persistentOptions.RedisPrefix,
|
||||||
}, nil
|
}, nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -31,8 +31,8 @@ func init() {
|
|||||||
func run(cmd *cobra.Command, args []string) {
|
func run(cmd *cobra.Command, args []string) {
|
||||||
proxy := getProxy(func() (npmproxy.Options, error) {
|
proxy := getProxy(func() (npmproxy.Options, error) {
|
||||||
return npmproxy.Options{
|
return npmproxy.Options{
|
||||||
RedisPrefix: persistentOptions.RedisPrefix,
|
DatabasePrefix: persistentOptions.RedisPrefix,
|
||||||
RedisExpireTimeout: time.Duration(rootOptions.CacheTTL) * time.Second,
|
DatabaseExpiration: time.Duration(rootOptions.CacheTTL) * time.Second,
|
||||||
UpstreamAddress: rootOptions.UpstreamAddress,
|
UpstreamAddress: rootOptions.UpstreamAddress,
|
||||||
}, nil
|
}, nil
|
||||||
})
|
})
|
||||||
|
@ -10,16 +10,18 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
proxy := npmproxy.Proxy{
|
proxy := npmproxy.Proxy{
|
||||||
RedisClient: redis.NewClient(&redis.Options{
|
Database: npmproxy.DatabaseRedis{
|
||||||
|
Client: redis.NewClient(&redis.Options{
|
||||||
Addr: "localhost:6379",
|
Addr: "localhost:6379",
|
||||||
DB: 0,
|
DB: 0,
|
||||||
Password: "",
|
Password: "",
|
||||||
}),
|
}),
|
||||||
|
},
|
||||||
HttpClient: &http.Client{},
|
HttpClient: &http.Client{},
|
||||||
GetOptions: func() (npmproxy.Options, error) {
|
GetOptions: func() (npmproxy.Options, error) {
|
||||||
return npmproxy.Options{
|
return npmproxy.Options{
|
||||||
RedisPrefix: "ncp-",
|
DatabasePrefix: "ncp-",
|
||||||
RedisExpireTimeout: 1 * time.Hour,
|
DatabaseExpiration: 1 * time.Hour,
|
||||||
UpstreamAddress: "https://registry.npmjs.org",
|
UpstreamAddress: "https://registry.npmjs.org",
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
|
@ -13,8 +13,8 @@ func (proxy Proxy) GetMetadata(name string, originalPath string, header http.Hea
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// get package from redis
|
// get package from database
|
||||||
pkg, err := proxy.RedisClient.Get(options.RedisPrefix + name).Result()
|
pkg, err := proxy.Database.Get(options.DatabasePrefix + name)
|
||||||
|
|
||||||
// either package doesn't exist or there's some other problem
|
// either package doesn't exist or there's some other problem
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -50,11 +50,11 @@ func (proxy Proxy) GetMetadata(name string, originalPath string, header http.Hea
|
|||||||
pkg = string(body)
|
pkg = string(body)
|
||||||
|
|
||||||
// save to redis
|
// save to redis
|
||||||
_, err = proxy.RedisClient.Set(
|
err = proxy.Database.Set(
|
||||||
options.RedisPrefix+name,
|
options.DatabasePrefix+name,
|
||||||
pkg,
|
pkg,
|
||||||
options.RedisExpireTimeout,
|
options.DatabaseExpiration,
|
||||||
).Result()
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -74,14 +74,14 @@ func (proxy Proxy) ListMetadata() ([]string, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
metadata, err := proxy.RedisClient.Keys(options.RedisPrefix + "*").Result()
|
metadata, err := proxy.Database.Keys(options.DatabasePrefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
deprefixedMetadata := make([]string, 0)
|
deprefixedMetadata := make([]string, 0)
|
||||||
for _, record := range metadata {
|
for _, record := range metadata {
|
||||||
deprefixedMetadata = append(deprefixedMetadata, strings.Replace(record, options.RedisPrefix, "", 1))
|
deprefixedMetadata = append(deprefixedMetadata, strings.Replace(record, options.DatabasePrefix, "", 1))
|
||||||
}
|
}
|
||||||
|
|
||||||
return deprefixedMetadata, nil
|
return deprefixedMetadata, nil
|
||||||
@ -94,13 +94,13 @@ func (proxy Proxy) PurgeMetadata() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
metadata, err := proxy.RedisClient.Keys(options.RedisPrefix + "*").Result()
|
metadata, err := proxy.Database.Keys(options.DatabasePrefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, record := range metadata {
|
for _, record := range metadata {
|
||||||
_, err := proxy.RedisClient.Del(record).Result()
|
err := proxy.Database.Delete(record)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
31
proxy/database_redis.go
Normal file
31
proxy/database_redis.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package proxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-redis/redis"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DatabaseRedis struct {
|
||||||
|
Client *redis.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db DatabaseRedis) Get(key string) (string, error) {
|
||||||
|
return db.Client.Get(key).Result()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db DatabaseRedis) Set(key string, value string, expiration time.Duration) error {
|
||||||
|
return db.Client.Set(key, value, expiration).Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db DatabaseRedis) Delete(key string) error {
|
||||||
|
return db.Client.Del(key).Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db DatabaseRedis) Keys(prefix string) ([]string, error) {
|
||||||
|
return db.Client.Keys(prefix + "*").Result()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db DatabaseRedis) Health() error {
|
||||||
|
return db.Client.Ping().Err()
|
||||||
|
}
|
@ -3,19 +3,25 @@ package proxy
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Proxy struct {
|
type Proxy struct {
|
||||||
RedisClient *redis.Client
|
Database Database
|
||||||
HttpClient *http.Client
|
HttpClient *http.Client
|
||||||
|
|
||||||
GetOptions func() (Options, error)
|
GetOptions func() (Options, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
RedisPrefix string
|
DatabasePrefix string
|
||||||
RedisExpireTimeout time.Duration
|
DatabaseExpiration time.Duration
|
||||||
UpstreamAddress string
|
UpstreamAddress string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Database interface {
|
||||||
|
Get(key string) (string, error)
|
||||||
|
Set(key string, value string, ttl time.Duration) error
|
||||||
|
Delete(key string) error
|
||||||
|
Keys(prefix string) ([]string, error)
|
||||||
|
Health() error
|
||||||
|
}
|
||||||
|
@ -52,7 +52,7 @@ func (proxy Proxy) NoRouteHandler(c *gin.Context) {
|
|||||||
// } else
|
// } else
|
||||||
|
|
||||||
if c.Request.URL.Path == "/" {
|
if c.Request.URL.Path == "/" {
|
||||||
_, err := proxy.RedisClient.Ping().Result()
|
err := proxy.Database.Health()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithStatusJSON(503, err)
|
c.AbortWithStatusJSON(503, err)
|
||||||
|
10
readme.md
10
readme.md
@ -54,16 +54,16 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
proxy := npmproxy.Proxy{
|
proxy := npmproxy.Proxy{
|
||||||
RedisClient: redis.NewClient(&redis.Options{
|
Database: npmproxy.DatabaseRedis{
|
||||||
|
Client: redis.NewClient(&redis.Options{
|
||||||
Addr: "localhost:6379",
|
Addr: "localhost:6379",
|
||||||
DB: 0,
|
|
||||||
Password: "",
|
|
||||||
}),
|
}),
|
||||||
|
},
|
||||||
HttpClient: &http.Client{},
|
HttpClient: &http.Client{},
|
||||||
GetOptions: func() (npmproxy.Options, error) {
|
GetOptions: func() (npmproxy.Options, error) {
|
||||||
return npmproxy.Options{
|
return npmproxy.Options{
|
||||||
RedisPrefix: "ncp-",
|
DatabasePrefix: "ncp-",
|
||||||
RedisExpireTimeout: 1 * time.Hour,
|
DatabaseExpiration: 1 * time.Hour,
|
||||||
UpstreamAddress: "https://registry.npmjs.org",
|
UpstreamAddress: "https://registry.npmjs.org",
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user