Skip to content

Commit 50e0d11

Browse files
committed
allow using environment vars for configuration
fixes #151
1 parent dfcfda5 commit 50e0d11

7 files changed

Lines changed: 89 additions & 0 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,13 @@ you want to use a different caching implementation, it's probably easiest to
287287
just make a copy of `cmd/imageproxy/main.go` and customize it to fit your
288288
needs... it's a very simple command.
289289

290+
### Environment Variables ###
291+
292+
All configuration flags have equivalent environment variables of the form
293+
`IMAGEPROXY_$NAME`. For example, an on-disk cache could be configured by calling
294+
295+
IMAGEPROXY_CACHE="/tmp/imageproxy" imageproxy
296+
290297
## Deploying ##
291298

292299
In most cases, you can follow the normal procedure for building a deploying any

cmd/imageproxy/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/garyburd/redigo/redis"
3434
"github.com/gregjones/httpcache/diskcache"
3535
rediscache "github.com/gregjones/httpcache/redis"
36+
"github.com/jamiealquiza/envy"
3637
"github.com/peterbourgon/diskv"
3738
"willnorris.com/go/imageproxy"
3839
"willnorris.com/go/imageproxy/internal/gcscache"
@@ -60,6 +61,7 @@ func init() {
6061
}
6162

6263
func main() {
64+
envy.Parse("IMAGEPROXY")
6365
flag.Parse()
6466

6567
p := imageproxy.NewProxy(nil, cache.Cache)

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717
github.com/gregjones/httpcache v0.0.0-20190212212710-3befbb6ad0cc
1818
github.com/grpc-ecosystem/grpc-gateway v1.8.5 // indirect
1919
github.com/hashicorp/golang-lru v0.5.1 // indirect
20+
github.com/jamiealquiza/envy v1.1.0
2021
github.com/marstr/guid v0.0.0-20170427235115-8bdf7d1a087c // indirect
2122
github.com/muesli/smartcrop v0.2.1-0.20181030220600-548bbf0c0965
2223
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
@@ -36,3 +37,6 @@ require (
3637

3738
// temporary fix to https://github.com/golang/lint/issues/436 which still seems to be a problem
3839
replace github.com/golang/lint => github.com/golang/lint v0.0.0-20190227174305-8f45f776aaf1
40+
41+
// local copy of envy package without cobra support
42+
replace github.com/jamiealquiza/envy => ./third_party/envy

third_party/envy/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Jamie Alquiza
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

third_party/envy/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
envy is a copy of https://github.com/jamiealquiza/envy without the cobra
2+
support.

third_party/envy/envy.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Package envy automatically exposes environment
2+
// variables for all of your flags.
3+
package envy
4+
5+
import (
6+
"flag"
7+
"fmt"
8+
"os"
9+
"strings"
10+
)
11+
12+
// Parse takes a prefix string and exposes environment variables
13+
// for all flags in the default FlagSet (flag.CommandLine) in the
14+
// form of PREFIX_FLAGNAME.
15+
func Parse(p string) {
16+
update(p, flag.CommandLine)
17+
}
18+
19+
// update takes a prefix string p and *flag.FlagSet. Each flag
20+
// in the FlagSet is exposed as an upper case environment variable
21+
// prefixed with p. Any flag that was not explicitly set by a user
22+
// is updated to the environment variable, if set.
23+
func update(p string, fs *flag.FlagSet) {
24+
// Build a map of explicitly set flags.
25+
set := map[string]interface{}{}
26+
fs.Visit(func(f *flag.Flag) {
27+
set[f.Name] = nil
28+
})
29+
30+
fs.VisitAll(func(f *flag.Flag) {
31+
// Create an env var name
32+
// based on the supplied prefix.
33+
envVar := fmt.Sprintf("%s_%s", p, strings.ToUpper(f.Name))
34+
envVar = strings.Replace(envVar, "-", "_", -1)
35+
36+
// Update the Flag.Value if the
37+
// env var is non "".
38+
if val := os.Getenv(envVar); val != "" {
39+
// Update the value if it hasn't
40+
// already been set.
41+
if _, defined := set[f.Name]; !defined {
42+
fs.Set(f.Name, val)
43+
}
44+
}
45+
46+
// Append the env var to the
47+
// Flag.Usage field.
48+
f.Usage = fmt.Sprintf("%s [%s]", f.Usage, envVar)
49+
})
50+
}

third_party/envy/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module .
2+
3+
go 1.12

0 commit comments

Comments
 (0)