Skip to content

Commit

Permalink
Environment args (#70)
Browse files Browse the repository at this point in the history
Add env for all arguments and info about env usage
  • Loading branch information
mika-go-13 authored and brendan-ward committed Jun 2, 2019
1 parent 9435275 commit e49102f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
53 changes: 53 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"strconv"

"golang.org/x/crypto/acme/autocert"

Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit e49102f

Please sign in to comment.