perf: move dynamic options reciever to server, enable Cache-Control HTTP header

This commit is contained in:
 Ilya Atamas
2019-04-23 17:59:18 +03:00
parent f821e794f8
commit 62e64e5154
9 changed files with 91 additions and 94 deletions

View File

@@ -107,23 +107,29 @@ import (
"time"
npmproxy "github.com/emeralt/npm-cache-proxy/proxy"
redis "github.com/go-redis/redis"
"github.com/go-redis/redis"
)
func main() {
// create proxy
proxy := npmproxy.Proxy{
// you can provide you own Database
// or use an existing one
// use redis as database
Database: npmproxy.DatabaseRedis{
// see github.com/go-redis/redis
Client: redis.NewClient(&redis.Options{
Addr: "localhost:6379",
}),
},
// allows to reuse tcp connections
// reuse connections
HttpClient: &http.Client{},
}
// allows to get options dynamically
// create and start server
proxy.Server(npmproxy.ServerOptions{
ListenAddress: "localhost:8080",
// allow fetching options dynamically on each request
GetOptions: func() (npmproxy.Options, error) {
return npmproxy.Options{
DatabasePrefix: "ncp-",
@@ -131,11 +137,6 @@ func main() {
UpstreamAddress: "https://registry.npmjs.org",
}, nil
},
}
// listen on http://localhost:8080
proxy.Server(npmproxy.ServerOptions{
ListenAddress: "localhost:8080",
}).ListenAndServe()
}
```