diff --git a/README.md b/README.md index 781adbb..188edc1 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,38 @@ If `redirect` option is provided, the server also listens on port 80 and redirec If the `--tls` option is provided, the Let's Encrypt Terms of Service are accepted automatically on your behalf. Please review them [here](https://letsencrypt.org/repository/). Certificates are cached in a `.certs` folder created where you are executing `mbtileserver`. Please make sure this folder can be written by the `mbtileserver` process or you will get errors. +Also you can set up server config by environment variables. It may be helpful, when you deploying it in docker image. Just now exists next variables: +- PORT +- TILE_PATH +- PRIVATE_KEY +- PATH_PREFIX +- DOMAIN +- SENTRY_DSN +- VERBOSE +- AUTO_TLS +- REDIRECT + +Simple example: + +``` +$ PORT=7777 TILE_PATH=./path/to/your/tiles VERBOSE=true mbtileserver +``` + +In docker-compose.yml file it will be look like: + +``` +mbtileserver: + ... + + environment: + PORT: 7777 + TYLE_PATH: "./path/to/your/tiles" + VERBOSE: true + entrypoint: mbtileserver + + ... +``` + ## Specifications - expects mbtiles files to follow version 1.0 of the [mbtiles specification](https://github.com/mapbox/mbtiles-spec). Version 1.1 is preferred. diff --git a/main.go b/main.go index 21e6311..1a4d430 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "strconv" "golang.org/x/crypto/acme/autocert" @@ -63,6 +64,58 @@ func init() { flags.BoolVarP(&autotls, "tls", "t", false, "Auto TLS via Let's Encrypt") flags.BoolVarP(&redirect, "redirect", "r", false, "Redirect HTTP to HTTPS") + if env := os.Getenv("PORT"); env != "" { + p, err := strconv.Atoi(env) + if err != nil { + log.Fatalln("PORT must be a number") + } + port = p + } + + if env := os.Getenv("TILE_PATH"); env != "" { + tilePath = env + } + + if env := os.Getenv("PRIVATE_KEY"); env != "" { + privateKey = env + } + + if env := os.Getenv("PATH_PREFIX"); env != "" { + pathPrefix = env + } + + if env := os.Getenv("DOMAIN"); env != "" { + domain = env + } + + if env := os.Getenv("SENTRY_DSN"); env != "" { + sentryDSN = env + } + + if env := os.Getenv("VERBOSE"); env != "" { + p, err := strconv.ParseBool(env) + if err != nil { + log.Fatalln("VERBOSE must be a bool(true/false)") + } + verbose = p + } + + if env := os.Getenv("AUTO_TLS"); env != "" { + p, err := strconv.ParseBool(env) + if err != nil { + log.Fatalln("AUTO_TLS must be a bool(true/false)") + } + autotls = p + } + + if env := os.Getenv("REDIRECT"); env != "" { + p, err := strconv.ParseBool(env) + if err != nil { + log.Fatalln("REDIRECT must be a bool(true/false)") + } + redirect = p + } + if secretKey == "" { secretKey = os.Getenv("MBTILESERVER_SECRET_KEY") }