Skip to content

Commit

Permalink
better env lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
jancajthaml committed Feb 20, 2021
1 parent e69474d commit 88c8b80
Show file tree
Hide file tree
Showing 18 changed files with 333 additions and 493 deletions.
Binary file added services/ledger-rest/.DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions services/ledger-rest/api/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ func GetTransaction(storage localfs.Storage) func(c echo.Context) error {
c.Response().WriteHeader(http.StatusNotFound)
return nil
}
unescapedId, err := url.PathUnescape(c.Param("id"))
unescapedID, err := url.PathUnescape(c.Param("id"))
if err != nil {
c.Response().WriteHeader(http.StatusNotFound)
return nil
}
id := strings.TrimSpace(unescapedId)
id := strings.TrimSpace(unescapedID)
if id == "" {
c.Response().WriteHeader(http.StatusNotFound)
return nil
Expand Down
21 changes: 12 additions & 9 deletions services/ledger-rest/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

package config

import "strings"
import (
"github.com/jancajthaml-openbank/ledger-rest/support/env"
"strings"
)

// Configuration of application
type Configuration struct {
Expand All @@ -41,13 +44,13 @@ type Configuration struct {
// LoadConfig loads application configuration
func LoadConfig() Configuration {
return Configuration{
RootStorage: envString("LEDGER_STORAGE", "/data"),
ServerPort: envInteger("LEDGER_HTTP_PORT", 4401),
ServerKey: envString("LEDGER_SERVER_KEY", ""),
ServerCert: envString("LEDGER_SERVER_CERT", ""),
LakeHostname: envString("LEDGER_LAKE_HOSTNAME", "127.0.0.1"),
LogLevel: strings.ToUpper(envString("LEDGER_LOG_LEVEL", "INFO")),
MinFreeDiskSpace: uint64(envInteger("VAULT_STORAGE_THRESHOLD", 0)),
MinFreeMemory: uint64(envInteger("VAULT_MEMORY_THRESHOLD", 0)),
RootStorage: env.String("LEDGER_STORAGE", "/data"),
ServerPort: env.Int("LEDGER_HTTP_PORT", 4401),
ServerKey: env.String("LEDGER_SERVER_KEY", ""),
ServerCert: env.String("LEDGER_SERVER_CERT", ""),
LakeHostname: env.String("LEDGER_LAKE_HOSTNAME", "127.0.0.1"),
LogLevel: strings.ToUpper(env.String("LEDGER_LOG_LEVEL", "INFO")),
MinFreeDiskSpace: env.Uint64("VAULT_STORAGE_THRESHOLD", 0),
MinFreeMemory: env.Uint64("VAULT_MEMORY_THRESHOLD", 0),
}
}
83 changes: 0 additions & 83 deletions services/ledger-rest/config/environment.go

This file was deleted.

147 changes: 0 additions & 147 deletions services/ledger-rest/config/environment_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion services/ledger-rest/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.15

require (
github.com/coreos/go-systemd/v22 v22.1.0
github.com/jancajthaml-openbank/actor-system v1.4.6
github.com/jancajthaml-openbank/actor-system v1.4.7
github.com/jancajthaml-openbank/local-fs v1.2.2
github.com/labstack/echo/v4 v4.2.0
github.com/rs/xid v1.2.1
Expand Down
8 changes: 4 additions & 4 deletions services/ledger-rest/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/godbus/dbus/v5 v5.0.3 h1:ZqHaoEF7TBzh4jzPmqVhE/5A1z9of6orkAe5uHoAeME=
github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/jancajthaml-openbank/actor-system v1.4.6 h1:mo9DFiFW136tLG3oYRXGvEgwPmxvrDxmzQiWj3WpVq4=
github.com/jancajthaml-openbank/actor-system v1.4.6/go.mod h1:mLnN69sePtKknn+OL18hOM4N8f/llDIoia98eNzBkXo=
github.com/jancajthaml-openbank/actor-system v1.4.7 h1:sqO/7BTm47y/NQ29ZuWxgPU7d2CmIEH1pCrcKyN/7ck=
github.com/jancajthaml-openbank/actor-system v1.4.7/go.mod h1:KzC0k0aZLZyj+eKAyEByXqM2eKCCKmBlMcjo2TXLtYU=
github.com/jancajthaml-openbank/local-fs v1.2.2 h1:zBhEDRh2zY7VI1KHTMwp8uqxTP7XR9dB9cK0NYGnzmw=
github.com/jancajthaml-openbank/local-fs v1.2.2/go.mod h1:aeF1ybqH3QQsFvqGdvk/0+5TXnHk1T7R1l+ZQ5d8SkY=
github.com/labstack/echo/v4 v4.2.0 h1:jkCSsjXmBmapVXF6U4BrSz/cgofWM0CU3Q74wQvXkIc=
Expand All @@ -22,8 +22,8 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/pebbe/zmq4 v1.2.2 h1:RZ5Ogp0D5S6u+tSxopnI3afAf0ifWbvQOAw9HxXvZP4=
github.com/pebbe/zmq4 v1.2.2/go.mod h1:7N4y5R18zBiu3l0vajMUWQgZyjv464prE8RCyBcmnZM=
github.com/pebbe/zmq4 v1.2.3 h1:SwOjTnXrtwyq4Hs4AXghkrNNEMuMnpvCZfEGTkDgrgI=
github.com/pebbe/zmq4 v1.2.3/go.mod h1:7N4y5R18zBiu3l0vajMUWQgZyjv464prE8RCyBcmnZM=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
62 changes: 62 additions & 0 deletions services/ledger-rest/support/env/lookup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) 2016-2021, Jan Cajthaml <jan.cajthaml@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package env

import (
"os"
"strconv"
)

// Get retrieves the string value of the environment variable named by the key
func Get(key string) (string, bool) {
if v := os.Getenv(key); v != "" {
return v, true
}
return "", false
}

// String retrieves the string value from environment named by the key.
func String(key string, fallback string) string {
if str, exists := Get(key); exists {
return str
}
return fallback
}

// Int retrieves integer value from the environment.
func Int(key string, fallback int) int {
if str, exists := Get(key); exists {
v, err := strconv.ParseInt(str, 10, 0)
if err != nil {
log.Warn().Msgf("invalid value in %s, using fallback", key)
return fallback
}
return int(v)
}
return fallback
}

// Uint64 retrieves 64-bit unsigned integer value from the environment.
func Uint64(key string, fallback uint64) uint64 {
if str, exists := Get(key); exists {
v, err := strconv.ParseUint(str, 10, 64)
if err != nil {
log.Warn().Msgf("invalid value in %s, using fallback", key)
return fallback
}
return v
}
return fallback
}

0 comments on commit 88c8b80

Please sign in to comment.