feat: initial commit

This commit is contained in:
 Ilya Atamas
2019-04-16 15:05:21 +03:00
commit 4dad509dc6
11 changed files with 307 additions and 0 deletions

24
proxy/main.go Normal file
View File

@@ -0,0 +1,24 @@
package proxy
import (
"net/http"
"time"
"github.com/go-redis/redis"
)
type Proxy struct {
RedisClient *redis.Client
HttpClient *http.Client
GetOptions func() (Options, error)
}
type Options struct {
RedisPrefix string
RedisExpireTimeout time.Duration
UpstreamAddress string
ReplaceAddress string
StaticServerAddress string
}

68
proxy/metadata.go Normal file
View File

@@ -0,0 +1,68 @@
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
}

65
proxy/server.go Normal file
View File

@@ -0,0 +1,65 @@
package proxy
import (
"net/http"
"strings"
"time"
ginzap "github.com/gin-contrib/zap"
gin "github.com/gin-gonic/gin"
zap "go.uber.org/zap"
)
type ServerOptions struct {
ListenAddress string
}
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)
return &http.Server{
Handler: router,
Addr: options.ListenAddress,
}
}
func (proxy Proxy) GetPackageHandler(c *gin.Context) {
key := c.Request.URL.Path
pkg, err := proxy.GetMetadata(key, c.Request.Header)
if err != nil {
c.AbortWithError(500, err)
} else {
c.Data(200, "application/json", pkg)
}
}
func (proxy Proxy) NoRouteHandler(c *gin.Context) {
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 {
c.AbortWithStatusJSON(503, err)
} else {
c.AbortWithStatusJSON(200, gin.H{"ok": true})
}
} else {
options, err := proxy.GetOptions()
if err != nil {
c.AbortWithStatusJSON(500, err)
} else {
c.Redirect(http.StatusTemporaryRedirect, options.UpstreamAddress+c.Request.URL.Path)
}
}
}