Переглянути джерело

Add IMGPROXY_BASE_URL param

DarthSim 7 роки тому
батько
коміт
bcd9dfe809
3 змінених файлів з 11 додано та 1 видалено
  1. 4 0
      README.md
  2. 4 0
      config.go
  3. 3 1
      download.go

+ 4 - 0
README.md

@@ -164,6 +164,10 @@ You can also specify a secret to enable authorization with the HTTP `Authorizati
 * `IMGPROXY_QUALITY` — quality of the resulting image, percentage. Default: `80`;
 * `IMGPROXY_GZIP_COMPRESSION` — GZip compression level. Default: `5`;
 
+#### Miscellaneous
+
+* `IMGPROXY_BASE_URL` - base URL part which will be added to every requestsd image URL. For example, if base URL is `http://example.com/images` and `/path/to/image.png` is requested, imgproxy will download the image from `http://example.com/images/path/to/image.png`. Default: blank.
+
 ## Generating the URL
 
 The URL should contain the signature and resize parameters, like this:

+ 4 - 0
config.go

@@ -101,6 +101,8 @@ type config struct {
 
 	ETagEnabled   bool
 	ETagSignature []byte
+
+	BaseURL string
 }
 
 var conf = config{
@@ -155,6 +157,8 @@ func init() {
 
 	boolEnvConfig(&conf.ETagEnabled, "IMGPROXY_USE_ETAG")
 
+	strEnvConfig(&conf.BaseURL, "IMGPROXY_BASE_URL")
+
 	if len(conf.Key) == 0 {
 		log.Fatalln("Key is not defined")
 	}

+ 3 - 1
download.go

@@ -106,7 +106,9 @@ func readAndCheckImage(res *http.Response) ([]byte, imageType, error) {
 }
 
 func downloadImage(url string) ([]byte, imageType, error) {
-	res, err := downloadClient.Get(url)
+	fullURL := fmt.Sprintf("%s%s", conf.BaseURL, url)
+
+	res, err := downloadClient.Get(fullURL)
 	if err != nil {
 		return nil, UNKNOWN, err
 	}