ent seteup

This commit is contained in:
2024-11-10 14:52:33 +05:30
parent 45c318b897
commit b0db98452a
78 changed files with 21469 additions and 36 deletions

View File

@@ -2,19 +2,56 @@ package config
import (
"fmt"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"gitserver.in/patialtech/rano/config/dotenv"
"gitserver.in/patialtech/rano/pkg/logger"
)
const Env = "development"
const (
// projDir need to be same as project code root dir name
projDir = "rano"
EnvDev = "development"
EnvProd = "production"
EnvStage = "staging"
var conf *Config
AuthUserCtxKey = "AuthUser"
)
var (
conf *Config
AppEnv Env = EnvDev
)
type (
Env string
Config struct {
WebPort int `env:"WEB_PORT"`
WebURL string `env:"WEB_URL"`
GraphPort int `env:"GRAPH_PORT"`
GrapURL string `env:"GRAPH_URL"`
DbURL string `env:"DB_URL"`
}
)
func init() {
envVar, err := dotenv.Read(fmt.Sprintf(".env.%s", Env))
wd, _ := os.Getwd()
// In dev env we run test and other program for diff dir locations under project root,
// this makes reading env file harder.
// Let's add a hack to make sure we fallback to root dir in dev env
if AppEnv == EnvDev {
idx := strings.Index(wd, projDir)
if idx > -1 {
wd = filepath.Join(wd[:idx], projDir)
}
}
envVar, err := dotenv.Read(wd, fmt.Sprintf(".env.%s", AppEnv))
if err != nil {
panic(err)
}
@@ -32,13 +69,6 @@ func Read() *Config {
return conf
}
type Config struct {
WebPort int `env:"WEB_PORT"`
WebURL string `env:"WEB_URL"`
GraphPort int `env:"GRAPH_PORT"`
GrapURL string `env:"GRAPH_URL"`
}
func (c *Config) loadEnv(vars map[string]string) {
if c == nil {
return
@@ -54,7 +84,7 @@ func (c *Config) loadEnv(vars map[string]string) {
v, found := vars[tag]
if !found {
logger.Warn("var %q not found in file .env.%s", tag, Env)
logger.Warn("var %q not found in file .env.%s", tag, AppEnv)
continue
}

View File

@@ -4,6 +4,9 @@ import (
"bytes"
"io"
"os"
"path/filepath"
"gitserver.in/patialtech/rano/pkg/logger"
)
//
@@ -12,12 +15,13 @@ import (
// Read all env (with same file loading semantics as Load) but return values as
// a map rather than automatically writing values into env
func Read(filenames ...string) (envMap map[string]string, err error) {
func Read(dir string, filenames ...string) (envMap map[string]string, err error) {
filenames = filenamesOrDefault(filenames)
envMap = make(map[string]string)
for _, filename := range filenames {
individualEnvMap, individualErr := readFile(filename)
logger.Info("read env file %s", filepath.Join(dir, filename))
individualEnvMap, individualErr := readFile(filepath.Join(dir, filename))
if individualErr != nil {
err = individualErr