1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-12-05 23:28:10 +02:00
Files
imgproxy/config.go

126 lines
2.4 KiB
Go
Raw Normal View History

2017-06-20 16:58:55 +03:00
package main
import (
2017-06-26 01:20:48 +03:00
"bytes"
2017-06-20 16:58:55 +03:00
"encoding/hex"
"flag"
"io/ioutil"
"log"
"os"
"path/filepath"
2017-06-26 01:20:48 +03:00
"strconv"
2017-06-20 16:58:55 +03:00
)
2017-06-26 01:20:48 +03:00
func absPathToFile(path string) string {
if filepath.IsAbs(path) {
return path
}
2017-06-20 16:58:55 +03:00
2017-06-26 01:20:48 +03:00
appPath, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatalln(err)
}
2017-06-20 16:58:55 +03:00
2017-06-26 01:20:48 +03:00
return filepath.Join(appPath, path)
2017-06-20 16:58:55 +03:00
}
2017-06-26 01:20:48 +03:00
func intEnvConfig(i *int, name string) {
if env, err := strconv.Atoi(os.Getenv(name)); err == nil {
*i = env
}
2017-06-20 16:58:55 +03:00
}
2017-06-26 01:20:48 +03:00
func strEnvConfig(s *string, name string) {
if env := os.Getenv(name); len(env) > 0 {
*s = env
2017-06-20 16:58:55 +03:00
}
2017-06-26 01:20:48 +03:00
}
2017-06-20 16:58:55 +03:00
2017-06-26 01:20:48 +03:00
func hexEnvConfig(b *[]byte, name string) {
var err error
if env := os.Getenv(name); len(env) > 0 {
if *b, err = hex.DecodeString(env); err != nil {
log.Fatalf("%s expected to be hex-encoded string\n", name)
}
}
2017-06-20 16:58:55 +03:00
}
2017-06-26 01:20:48 +03:00
func hexFileConfig(b *[]byte, filepath string) {
if len(filepath) == 0 {
return
}
2017-06-20 16:58:55 +03:00
2017-06-26 01:20:48 +03:00
fullfp := absPathToFile(filepath)
f, err := os.Open(fullfp)
2017-06-20 16:58:55 +03:00
if err != nil {
2017-06-26 01:20:48 +03:00
log.Fatalf("Can't open file %s\n", fullfp)
2017-06-20 16:58:55 +03:00
}
2017-06-26 01:20:48 +03:00
src, err := ioutil.ReadAll(f)
2017-06-20 16:58:55 +03:00
if err != nil {
log.Fatalln(err)
}
2017-06-26 01:20:48 +03:00
src = bytes.TrimSpace(src)
dst := make([]byte, hex.DecodedLen(len(src)))
n, err := hex.Decode(dst, src)
2017-06-20 16:58:55 +03:00
if err != nil {
2017-06-26 01:20:48 +03:00
log.Fatalf("%s expected to contain hex-encoded string\n", fullfp)
2017-06-20 16:58:55 +03:00
}
2017-06-26 01:20:48 +03:00
*b = dst[:n]
}
2017-06-20 16:58:55 +03:00
2017-06-26 01:20:48 +03:00
type config struct {
Bind string
ReadTimeout int
WriteTimeout int
2017-06-20 16:58:55 +03:00
2017-06-26 01:20:48 +03:00
MaxSrcDimension int
2017-06-20 16:58:55 +03:00
2017-06-26 01:20:48 +03:00
Quality int
Compression int
2017-06-20 16:58:55 +03:00
2017-06-26 01:20:48 +03:00
Key []byte
Salt []byte
}
2017-06-20 16:58:55 +03:00
2017-06-26 01:20:48 +03:00
var conf = config{
Bind: ":8080",
ReadTimeout: 10,
WriteTimeout: 10,
MaxSrcDimension: 4096,
Quality: 80,
Compression: 6,
}
func init() {
keypath := flag.String("keypath", "", "path of the file with hex-encoded key")
saltpath := flag.String("saltpath", "", "path of the file with hex-encoded salt")
flag.Parse()
strEnvConfig(&conf.Bind, "IMGPROXY_BIND")
intEnvConfig(&conf.ReadTimeout, "IMGPROXY_READ_TIMEOUT")
intEnvConfig(&conf.WriteTimeout, "IMGPROXY_WRITE_TIMEOUT")
intEnvConfig(&conf.MaxSrcDimension, "IMGPROXY_MAX_SRC_DIMENSION")
intEnvConfig(&conf.Quality, "IMGPROXY_QUALITY")
intEnvConfig(&conf.Compression, "IMGPROXY_COMPRESSION")
2017-06-20 16:58:55 +03:00
2017-06-26 01:20:48 +03:00
hexEnvConfig(&conf.Key, "IMGPROXY_KEY")
hexEnvConfig(&conf.Salt, "IMGPROXY_SALT")
hexFileConfig(&conf.Key, *keypath)
hexFileConfig(&conf.Salt, *saltpath)
if len(conf.Key) == 0 {
log.Fatalln("Key is not defined")
}
if len(conf.Salt) == 0 {
log.Fatalln("Salt is not defined")
2017-06-20 16:58:55 +03:00
}
}