Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): Env var config (#88)" #122

Merged
merged 2 commits into from
Sep 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,29 @@ import (
"context"
"flag"
"fmt"
"log"
"os"
"strconv"

"github.com/googleforgames/triton/internal/app/server"
)
log "github.com/sirupsen/logrus"

var (
port = flag.Uint("port", 6000, "The port number to run Triton on")
cloud = flag.String("cloud", "gcp", "The public cloud provider you wish to run Triton on")
bucket = flag.String("bucket", "gs://triton-dev-store", "The bucket which will hold Triton blobs")
project = flag.String("project", "triton-for-games-dev", "The GCP project ID to use for Datastore")
cache = flag.String("cache", "localhost:6379", "The address of the cache store instance")
"github.com/googleforgames/triton/internal/app/server"
)

func main() {
defaultPort := getEnvVarUInt("TRITON_PORT", 6000)
defaultCloud := getEnvVarString("TRITON_CLOUD", "gcp")
defaultBucket := getEnvVarString("TRITON_BUCKET", "gs://triton-dev-store")
defaultProject := getEnvVarString("TRITON_PROJECT", "triton-for-games-dev")
defaultCache := getEnvVarString("TRITON_CACHE", "localhost:6379")

var (
port = flag.Uint("port", uint(defaultPort), "The port number to run Triton on")
cloud = flag.String("cloud", defaultCloud, "The public cloud provider you wish to run Triton on")
bucket = flag.String("bucket", defaultBucket, "The bucket which will hold Triton blobs")
project = flag.String("project", defaultProject, "The GCP project ID to use for Datastore")
cache = flag.String("cache", defaultCache, "The address of the cache store instance")
)

flag.Parse()
if *cloud == "" {
log.Fatal("missing -cloud argument for cloud provider")
Expand Down Expand Up @@ -70,3 +77,22 @@ func main() {
log.Fatalf("got error starting server: %v", err)
}
}

func getEnvVarString(name string, defValue string) string {
if value := os.Getenv(name); value != "" {
return value
}
return defValue
}

func getEnvVarUInt(name string, defValue uint64) uint64 {
if value := os.Getenv(name); value != "" {
uval, err := strconv.ParseUint(value, 10, 64)
if err != nil {
log.Warningf("failed to parse %s env variable, default to %v", name, defValue)
uval = defValue
}
return uval
}
return defValue
}