Skip to content

Commit

Permalink
Add the env support (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
warrenrhodes authored Oct 15, 2024
1 parent 5f5761f commit 45c92ed
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
migrate_working_dir/
.history
*.db
.env**

# IntelliJ related
*.iml
Expand Down
4 changes: 2 additions & 2 deletions server/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
func main() {
flag.Parse()

config, err := config.LoadConfig("configs/config.yml")
config, err := config.LoadConfig()
if err != nil {
log.Fatalf("Failed to load configuration: %v", err)
}
log.Printf("Starting server with configuration: %v", config)

// Use config values
port := config.Server.GRPC.Port
webPort := config.Server.GRPCWeb.Port
Expand Down
39 changes: 37 additions & 2 deletions server/configs/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package config

import (
"fmt"
"log"
"os"

"github.com/joho/godotenv"
"gopkg.in/yaml.v3"
)

const AppConfigPath = "resources/config/app.%s.yml"

type Config struct {
Server struct {
GRPC struct {
Expand All @@ -31,10 +36,40 @@ type Config struct {
} `yaml:"auth"`
}

func LoadConfig(configPath string) (*Config, error) {
const (
// DEV represents development environment
DEV = "develop"
// PRD represents production environment
PRD = "production"
)

func LoadConfig() (*Config, error) {
config := &Config{}

data, err := os.ReadFile(configPath)
if len(os.Args) < 2 {
log.Fatal("Please specify an environment: dev or prod")
}

env := os.Args[1]
var envFile string

// Determine which .env file to load
switch env {
case "dev":
envFile = ".env.dev"
case "prod":
envFile = ".env.prod"
default:
log.Fatal("Invalid environment specified. Use 'dev' or 'prod'.")
}

// Load the environment variables from the specified .env file
err := godotenv.Load(envFile)
if err != nil {
log.Fatalf("Error loading .env file: %v", err)
}

data, err := os.ReadFile(fmt.Sprintf(AppConfigPath, env))
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/cenkalti/backoff/v4 v4.1.1 // indirect
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
github.com/improbable-eng/grpc-web v0.15.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/klauspost/compress v1.11.7 // indirect
github.com/rs/cors v1.7.0 // indirect
golang.org/x/sys v0.26.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ github.com/improbable-eng/grpc-web v0.15.0/go.mod h1:1sy9HKV4Jt9aEs9JSnkWlRJPuPt
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
Expand Down
File renamed without changes.

0 comments on commit 45c92ed

Please sign in to comment.