Skip to content

Commit

Permalink
adding env variables to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
eknowlton committed Oct 8, 2023
1 parent 7d0a32e commit aa0cb39
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.vscode/
.idea/
/bin/
.envrc
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,27 @@ CityFile = ""
AsnFile = ""
```

### Environment Variables for Configuration
You can also use environment variables for configuration, most likely used for Docker.

```
ECHOIP_LISTEN=":8080"
ECHOIP_TEMPLATE_DIR="html/"
ECHOIP_REDIS_URL="redis://localhost:6379"
ECHOIP_DATABASE="ipstack"
ECHOIP_IPSTACK_API_KEY="askdfj39sjdkf29dsjfk39sdfkj3"
ECHOIP_GEOIP_COUNTRY_FILE="/full/path/to/file.db"
ECHOIP_GEOIP_CITY_FILE="/full/path/to/file.db"
ECHOIP_GEOIP_ASN_FILE="/full/path/to/file.db"
ECHOIP_CACHE_TTL=3600
ECHOIP_REVERSE_LOOKUP=true
ECHOIP_PORT_LOOKUP=true
ECHOIP_SHOW_SPONSOR=true
ECHOIP_PROFILE=false
ECHOIP_IPSTACK_USE_HTTPS=true
ECHOIP_IPSTACK_ENABLE_SECURITY=true
```

### Caching with Redis

You can connect EchoIP to a Redis client to cache each request per IP. You can configure the life of the key in `config.CacheTtl`.
Expand Down
80 changes: 42 additions & 38 deletions cmd/echoip/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,49 +36,53 @@ func init() {

func main() {
file, err := os.Open("/etc/echoip/config.toml")
if err != nil {
panic(err)
}

var b []byte

runConfig, err := config.GetConfig()
defer file.Close()

config, err := config.GetConfig()
if err != nil {
panic(err)
log.Fatalf("Error building configuration: %s", err)
}

b, err := io.ReadAll(file)
if err != nil {
panic(err)
}
log.Printf("Error opening config file (/etc/echoip/config.toml): %s", err)

err = toml.Unmarshal(b, &config)
if err != nil {
panic(err)
b, err = io.ReadAll(file)
if err != nil {
log.Printf("Error reading config file (/etc/echoip/config.toml): %s", err)
}

err = toml.Unmarshal(b, &runConfig)
if err != nil {
panic(err)
}
}

var parser parser.Parser
if config.Database == "geoip" {
if runConfig.Database == "geoip" {
log.Print("Using GeoIP for IP database")
geo, err := geo.Open(
config.GeoIP.CountryFile,
config.GeoIP.CityFile,
config.GeoIP.AsnFile,
runConfig.GeoIP.CountryFile,
runConfig.GeoIP.CityFile,
runConfig.GeoIP.AsnFile,
)
if err != nil {
log.Fatal(err)
}
parser = &geo
}

if config.Database == "ipstack" {
log.Print("Using GeoIP for IP database")
if config.IPStack.EnableSecurity {
if runConfig.Database == "ipstack" {
log.Print("Using IPStack for IP database")
if runConfig.IPStack.EnableSecurity {
log.Print("Enable Security Module ( Requires Professional Plus account )")
}
enableSecurity := ipstackApi.ParamEnableSecurity(config.IPStack.EnableSecurity)
apiKey := ipstackApi.ParamToken(config.IPStack.ApiKey)
useHttps := ipstackApi.ParamUseHTTPS(config.IPStack.UseHttps)
if config.IPStack.UseHttps {
enableSecurity := ipstackApi.ParamEnableSecurity(runConfig.IPStack.EnableSecurity)
apiKey := ipstackApi.ParamToken(runConfig.IPStack.ApiKey)
useHttps := ipstackApi.ParamUseHTTPS(runConfig.IPStack.UseHttps)
if runConfig.IPStack.UseHttps {
log.Print("Use IP Stack HTTPS API ( Requires non-free account )")
}
if err := ipstackApi.Init(apiKey, enableSecurity, useHttps); err != nil {
Expand All @@ -89,8 +93,8 @@ func main() {
}

var serverCache cache.Cache
if len(config.RedisUrl) > 0 {
redisCache, err := cache.RedisCache(config.RedisUrl)
if len(runConfig.RedisUrl) > 0 {
redisCache, err := cache.RedisCache(runConfig.RedisUrl)
serverCache = &redisCache
if err != nil {
log.Fatal(err)
Expand All @@ -99,34 +103,34 @@ func main() {
serverCache = &cache.Null{}
}

server := http.New(parser, serverCache, config.CacheTtl, config.Profile)
server.IPHeaders = config.TrustedHeaders
server := http.New(parser, serverCache, runConfig.CacheTtl, runConfig.Profile)
server.IPHeaders = runConfig.TrustedHeaders

if _, err := os.Stat(config.TemplateDir); err == nil {
server.Template = config.TemplateDir
if _, err := os.Stat(runConfig.TemplateDir); err == nil {
server.Template = runConfig.TemplateDir
} else {
log.Printf("Not configuring default handler: Template not found: %s", config.TemplateDir)
log.Printf("Not configuring default handler: Template not found: %s", runConfig.TemplateDir)
}
if config.ReverseLookup {
if runConfig.ReverseLookup {
log.Println("Enabling reverse lookup")
server.LookupAddr = iputil.LookupAddr
}
if config.PortLookup {
if runConfig.PortLookup {
log.Println("Enabling port lookup")
server.LookupPort = iputil.LookupPort
}
if config.ShowSponsor {
if runConfig.ShowSponsor {
log.Println("Enabling sponsor logo")
server.Sponsor = config.ShowSponsor
server.Sponsor = runConfig.ShowSponsor
}
if len(config.TrustedHeaders) > 0 {
log.Printf("Trusting remote IP from header(s): %s", config.TrustedHeaders)
if len(runConfig.TrustedHeaders) > 0 {
log.Printf("Trusting remote IP from header(s): %s", runConfig.TrustedHeaders)
}
if config.Profile {
if runConfig.Profile {
log.Printf("Enabling profiling handlers")
}
log.Printf("Listening on http://%s", config.Listen)
if err := server.ListenAndServe(config.Listen); err != nil {
log.Printf("Listening on http://%s", runConfig.Listen)
if err := server.ListenAndServe(runConfig.Listen); err != nil {
log.Fatal(err)
}
}
7 changes: 5 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"os"
"strconv"
)
Expand Down Expand Up @@ -93,13 +94,15 @@ func GetConfig() (Config, error) {
}
defaultConfig.IPStack.EnableSecurity = ipStackEnableSecurity

fmt.Printf("%+v\n", defaultConfig)

return defaultConfig, nil
}

func getenv_int(key string, fallback int) (int, error) {
value := os.Getenv(key)

if len(value) == 0 {
if len(value) > 0 {
intValue, err := strconv.Atoi(value)
if err != nil {
return 0, err
Expand All @@ -122,7 +125,7 @@ func getenv_string(key string, fallback string) string {
func getenv_bool(key string, fallback bool) (bool, error) {
value := os.Getenv(key)

if len(value) == 0 {
if len(value) > 0 {
boolValue, err := strconv.ParseBool(value)
if err != nil {
return false, err
Expand Down

0 comments on commit aa0cb39

Please sign in to comment.