docs: extend readme, add inline docs
This commit is contained in:
@@ -8,7 +8,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GetMetadata returns cached NPM response for a given package path
|
||||
// GetMetadata returns cached NPM response for a given package path.
|
||||
func (proxy Proxy) GetMetadata(name string, originalPath string, request *http.Request) ([]byte, error) {
|
||||
options, err := proxy.GetOptions()
|
||||
if err != nil {
|
||||
@@ -93,7 +93,7 @@ func (proxy Proxy) ListMetadata() ([]string, error) {
|
||||
return deprefixedMetadata, nil
|
||||
}
|
||||
|
||||
// PurgeMetadata deletes all cached packages
|
||||
// PurgeMetadata deletes all cached packages.
|
||||
func (proxy Proxy) PurgeMetadata() error {
|
||||
options, err := proxy.GetOptions()
|
||||
if err != nil {
|
||||
|
||||
@@ -6,26 +6,32 @@ import (
|
||||
"github.com/go-redis/redis"
|
||||
)
|
||||
|
||||
// DatabaseRedis implements Database interface for Redis database.
|
||||
type DatabaseRedis struct {
|
||||
Client *redis.Client
|
||||
}
|
||||
|
||||
// Get returns data by key.
|
||||
func (db DatabaseRedis) Get(key string) (string, error) {
|
||||
return db.Client.Get(key).Result()
|
||||
}
|
||||
|
||||
// Set stores value identified by key with expiration timeout.
|
||||
func (db DatabaseRedis) Set(key string, value string, expiration time.Duration) error {
|
||||
return db.Client.Set(key, value, expiration).Err()
|
||||
}
|
||||
|
||||
// Delete deletes data by key.
|
||||
func (db DatabaseRedis) Delete(key string) error {
|
||||
return db.Client.Del(key).Err()
|
||||
}
|
||||
|
||||
// Keys returns stored keys filtered by prefix.
|
||||
func (db DatabaseRedis) Keys(prefix string) ([]string, error) {
|
||||
return db.Client.Keys(prefix + "*").Result()
|
||||
}
|
||||
|
||||
// Health returns an error if database connection cannot be estabilished.
|
||||
func (db DatabaseRedis) Health() error {
|
||||
return db.Client.Ping().Err()
|
||||
}
|
||||
|
||||
5
proxy/doc.go
Normal file
5
proxy/doc.go
Normal file
@@ -0,0 +1,5 @@
|
||||
//
|
||||
// Package proxy implements a HTTP caching proxy for Node package registry (NPM).
|
||||
// See https://github.com/emeralt/npm-cache-proxy/ for more information about proxy.
|
||||
//
|
||||
package proxy // import "https://github.com/emeralt/npm-cache-proxy/proxy"
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Proxy is the proxy instance, it contains Database and HttpClient as static options
|
||||
// and GetOptions as dynamic options provider
|
||||
type Proxy struct {
|
||||
Database Database
|
||||
HttpClient *http.Client
|
||||
@@ -12,12 +14,16 @@ type Proxy struct {
|
||||
GetOptions func() (Options, error)
|
||||
}
|
||||
|
||||
// Options provides dynamic options for Proxy.
|
||||
// This can be used for namespace separation,
|
||||
// allowing multiple users use the same proxy instance simultaneously.
|
||||
type Options struct {
|
||||
DatabasePrefix string
|
||||
DatabaseExpiration time.Duration
|
||||
UpstreamAddress string
|
||||
}
|
||||
|
||||
// Database provides interface for data storage.
|
||||
type Database interface {
|
||||
Get(key string) (string, error)
|
||||
Set(key string, value string, ttl time.Duration) error
|
||||
|
||||
@@ -13,15 +13,16 @@ type ServerOptions struct {
|
||||
ListenAddress string
|
||||
}
|
||||
|
||||
// Server creates http proxy server
|
||||
func (proxy Proxy) Server(options ServerOptions) *http.Server {
|
||||
router := gin.New()
|
||||
|
||||
logger, _ := zap.NewProduction()
|
||||
router.Use(ginzap.Ginzap(logger, time.RFC3339, true))
|
||||
|
||||
router.GET("/:scope/:name", proxy.GetPackageHandler)
|
||||
router.GET("/:scope", proxy.GetPackageHandler)
|
||||
router.NoRoute(proxy.NoRouteHandler)
|
||||
router.GET("/:scope/:name", proxy.getPackageHandler)
|
||||
router.GET("/:scope", proxy.getPackageHandler)
|
||||
router.NoRoute(proxy.noRouteHandler)
|
||||
|
||||
return &http.Server{
|
||||
Handler: router,
|
||||
@@ -29,7 +30,7 @@ func (proxy Proxy) Server(options ServerOptions) *http.Server {
|
||||
}
|
||||
}
|
||||
|
||||
func (proxy Proxy) GetPackageHandler(c *gin.Context) {
|
||||
func (proxy Proxy) getPackageHandler(c *gin.Context) {
|
||||
var name string
|
||||
if c.Param("name") != "" {
|
||||
name = c.Param("scope") + "/" + c.Param("name")
|
||||
@@ -47,7 +48,7 @@ func (proxy Proxy) GetPackageHandler(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func (proxy Proxy) NoRouteHandler(c *gin.Context) {
|
||||
func (proxy Proxy) noRouteHandler(c *gin.Context) {
|
||||
if c.Request.URL.Path == "/" {
|
||||
err := proxy.Database.Health()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user