Skip to content

Commit

Permalink
Add initConfig function (#2)
Browse files Browse the repository at this point in the history
* initConfig function

* removed additional env injection

* reverted env variables injection
  • Loading branch information
walkrist authored Sep 21, 2021
1 parent 31439d3 commit 534b998
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions cmd/hzpaste-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"log"
"os"
"path"
"path/filepath"
"runtime"
"strings"

_ "github.com/ep4sh/hzpaste/cmd/hzpaste-api/internal/docs"
Expand Down Expand Up @@ -55,13 +53,8 @@ func setupRouter() *gin.Engine {
}

func main() {
configuration := Configuration{}
err := gonfig.GetConf(getFileName(), &configuration)
if err != nil {
log.Fatal(err)
}

endpoint := fmt.Sprintf("%s:%s", configuration.Host, configuration.Port)
config := initConfig()
endpoint := fmt.Sprintf("%s:%s", config.Host, config.Port)
router := setupRouter()
router.Run(endpoint)
}
Expand All @@ -71,9 +64,25 @@ func getFileName() string {
if len(env) == 0 {
env = "development"
}
filename := []string{"internal/config/", "config.", env, ".json"}
_, dirname, _, _ := runtime.Caller(0)
filePath := path.Join(filepath.Dir(dirname), strings.Join(filename, ""))
filename := []string{"config.", env, ".json"}
filePath := path.Join(strings.Join(filename, ""))

return filePath
}

func initConfig() Configuration {
configuration := Configuration{
Port: os.Getenv("HZPASTE_HOST"),
Host: os.Getenv("HZPASTE_PORT"),
}
if len(configuration.Port) == 0 || len(configuration.Host) == 0 {
err := gonfig.GetConf(getFileName(), &configuration)
if err != nil {
log.Println("Cannot initialize configuration:")
log.Println("Please provide HZPASTE_HOST and HZPASTE_PORT environment variables or configuration file")
log.Fatal(err)
}
}

return configuration
}

0 comments on commit 534b998

Please sign in to comment.