feat: add cli - start server, list packages, purge packages
This commit is contained in:
110
proxy/cache.go
Normal file
110
proxy/cache.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GetMetadata returns NPM response for a given package path
|
||||
func (proxy Proxy) GetMetadata(name string, originalPath string, header http.Header) ([]byte, error) {
|
||||
options, err := proxy.GetOptions()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// get package from redis
|
||||
pkg, err := proxy.RedisClient.Get(options.RedisPrefix + name).Result()
|
||||
|
||||
// either package doesn't exist or there's some other problem
|
||||
if err != nil {
|
||||
|
||||
// check if error is caused by nonexistend package
|
||||
// if no, return error
|
||||
if err.Error() != "redis: nil" {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// error is caused by nonexistent package
|
||||
// fetch package
|
||||
req, err := http.NewRequest("GET", options.UpstreamAddress+originalPath, nil)
|
||||
|
||||
// inherit headers from request
|
||||
req.Header = header
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res, err := proxy.HttpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// convert body to string
|
||||
pkg = string(body)
|
||||
|
||||
// save to redis
|
||||
_, err = proxy.RedisClient.Set(
|
||||
options.RedisPrefix+name,
|
||||
pkg,
|
||||
options.RedisExpireTimeout,
|
||||
).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// replace tarball urls
|
||||
// FIXME: unmarshall and replace only necessary fields
|
||||
// convertedPkg := strings.ReplaceAll(string(pkg), options.ReplaceAddress, options.StaticServerAddress)
|
||||
|
||||
return []byte(pkg), nil
|
||||
}
|
||||
|
||||
// ListMetadata returns a list of all cached packages
|
||||
func (proxy Proxy) ListMetadata() ([]string, error) {
|
||||
options, err := proxy.GetOptions()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
metadata, err := proxy.RedisClient.Keys(options.RedisPrefix + "*").Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
deprefixedMetadata := make([]string, 0)
|
||||
for _, record := range metadata {
|
||||
deprefixedMetadata = append(deprefixedMetadata, strings.Replace(record, options.RedisPrefix, "", 1))
|
||||
}
|
||||
|
||||
return deprefixedMetadata, nil
|
||||
}
|
||||
|
||||
// Purge deletes all cached packages
|
||||
func (proxy Proxy) PurgeMetadata() error {
|
||||
options, err := proxy.GetOptions()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
metadata, err := proxy.RedisClient.Keys(options.RedisPrefix + "*").Result()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, record := range metadata {
|
||||
_, err := proxy.RedisClient.Del(record).Result()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GetMetadata returns NPM response for a given package path
|
||||
func (proxy Proxy) GetMetadata(path string, header http.Header) ([]byte, error) {
|
||||
options, err := proxy.GetOptions()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// get package from redis
|
||||
pkg, err := proxy.RedisClient.Get(options.RedisPrefix + path).Result()
|
||||
|
||||
// either package doesn't exist or there's some other problem
|
||||
if err != nil {
|
||||
|
||||
// check if error is caused by nonexistend package
|
||||
// if no, return error
|
||||
if err.Error() != "redis: nil" {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// error is caused by nonexistent package
|
||||
// fetch package
|
||||
req, err := http.NewRequest("GET", options.UpstreamAddress+path, nil)
|
||||
|
||||
// inherit headers from request
|
||||
req.Header = header
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res, err := proxy.HttpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// convert body to string
|
||||
pkg = string(body)
|
||||
|
||||
// save to redis
|
||||
_, err = proxy.RedisClient.Set(
|
||||
options.RedisPrefix+path,
|
||||
pkg,
|
||||
options.RedisExpireTimeout,
|
||||
).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// replace tarball urls
|
||||
// FIXME: unmarshall and replace only necessary fields
|
||||
convertedPkg := strings.ReplaceAll(string(pkg), options.ReplaceAddress, options.StaticServerAddress)
|
||||
|
||||
return []byte(convertedPkg), nil
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package proxy
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
ginzap "github.com/gin-contrib/zap"
|
||||
@@ -31,9 +30,14 @@ func (proxy Proxy) Server(options ServerOptions) *http.Server {
|
||||
}
|
||||
|
||||
func (proxy Proxy) GetPackageHandler(c *gin.Context) {
|
||||
key := c.Request.URL.Path
|
||||
var name string
|
||||
if c.Param("name") != "" {
|
||||
name = c.Param("scope") + "/" + c.Param("name")
|
||||
} else {
|
||||
name = c.Param("scope")
|
||||
}
|
||||
|
||||
pkg, err := proxy.GetMetadata(key, c.Request.Header)
|
||||
pkg, err := proxy.GetMetadata(name, c.Request.URL.Path, c.Request.Header)
|
||||
|
||||
if err != nil {
|
||||
c.AbortWithError(500, err)
|
||||
@@ -43,9 +47,11 @@ func (proxy Proxy) GetPackageHandler(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (proxy Proxy) NoRouteHandler(c *gin.Context) {
|
||||
if strings.Contains(c.Request.URL.Path, ".tgz") {
|
||||
proxy.GetPackageHandler(c)
|
||||
} else if c.Request.URL.Path == "/" {
|
||||
// if strings.Contains(c.Request.URL.Path, ".tgz") {
|
||||
// proxy.GetPackageHandler(c)
|
||||
// } else
|
||||
|
||||
if c.Request.URL.Path == "/" {
|
||||
_, err := proxy.RedisClient.Ping().Result()
|
||||
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user