Skip to content

Commit

Permalink
Dedicated package for app configuration (#4)
Browse files Browse the repository at this point in the history
* initConfig function

* removed additional env injection

* reverted env variables injection

* added heroku deployment mode

* hzconfig package
  • Loading branch information
walkrist authored Sep 26, 2021
1 parent 62c8dae commit 75f506e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 47 deletions.
62 changes: 62 additions & 0 deletions cmd/hzpaste-api/internal/hzconfig/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package hzconfig

import (
"log"
"os"
"path"
"strings"

"github.com/tkanos/gonfig"
)

type Configuration struct {
Host string `env:"HZPASTE_HOST"`
Port string `env:"HZPASTE_PORT"`
}

func GetConfigFileName() string {
env := os.Getenv("ENV")
if len(env) == 0 {
env = "development"
}
filename := []string{"config.", env, ".json"}
filePath := path.Join(strings.Join(filename, ""))

return filePath
}

func GetPortEnv() (string, string) {
envVarName := "HZPASTE_PORT"
// Specific environment variable override for heroku deployment only
_, herokuMode := os.LookupEnv("HEROKU_DEPLOY")
if herokuMode {
envVarName = "PORT"
}
//
return os.Getenv(envVarName), envVarName
}

func GetHostEnv() (string, string) {
envVarName := "HZPASTE_HOST"
return os.Getenv(envVarName), envVarName
}

func InitConfig() Configuration {
PortEnvValue, PortEnvName := GetPortEnv()
HostEnvValue, HostEnvName := GetHostEnv()
configuration := Configuration{
Port: PortEnvValue,
Host: HostEnvValue,
}
if len(configuration.Port) == 0 || len(configuration.Host) == 0 {
err := gonfig.GetConf(GetConfigFileName(), &configuration)
if err != nil {
log.Println("Cannot initialize configuration!")
log.Println("Either provide configuration file", GetConfigFileName())
log.Println("or set", HostEnvName, "and", PortEnvName, "environment variables")
log.Fatal(err)
}
}

return configuration
}
1 change: 1 addition & 0 deletions cmd/hzpaste-api/internal/hzconfig/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package hzconfig
41 changes: 2 additions & 39 deletions cmd/hzpaste-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,16 @@ package main

import (
"fmt"
"log"
"os"
"path"
"strings"

_ "github.com/ep4sh/hzpaste/cmd/hzpaste-api/internal/docs"
"github.com/ep4sh/hzpaste/cmd/hzpaste-api/internal/handlers"
"github.com/ep4sh/hzpaste/cmd/hzpaste-api/internal/hzconfig"
"github.com/ep4sh/hzpaste/internal/paste"
"github.com/gin-gonic/gin"
ginSwagger "github.com/swaggo/gin-swagger" // gin-swagger middleware
"github.com/swaggo/gin-swagger/swaggerFiles"
"github.com/tkanos/gonfig"
)

type Configuration struct {
Host string `env:"HZPASTE_HOST"`
Port string `env:"HZPASTE_PORT"`
}

// @contact.name API Support
// @contact.url http://ep4sh.cc
// @contact.email ep4sh2k@gm[a]il.com
Expand Down Expand Up @@ -53,36 +44,8 @@ func setupRouter() *gin.Engine {
}

func main() {
config := initConfig()
config := hzconfig.InitConfig()
endpoint := fmt.Sprintf("%s:%s", config.Host, config.Port)
router := setupRouter()
router.Run(endpoint)
}

func getConfigFileName() string {
env := os.Getenv("ENV")
if len(env) == 0 {
env = "development"
}
filename := []string{"config.", env, ".json"}
filePath := path.Join(strings.Join(filename, ""))

return filePath
}

func initConfig() Configuration {
configuration := Configuration{
Port: os.Getenv("HZPASTE_PORT"),
Host: os.Getenv("HZPASTE_HOST"),
}
if len(configuration.Port) == 0 || len(configuration.Host) == 0 {
err := gonfig.GetConf(getConfigFileName(), &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
}
17 changes: 9 additions & 8 deletions cmd/hzpaste-api/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"strings"
"testing"

"github.com/ep4sh/hzpaste/cmd/hzpaste-api/internal/hzconfig"
"github.com/stretchr/testify/assert"
)

func TestGCNotEnoughDataToCollect(t *testing.T) {

//TODO: add config to the router
// currently now way to Stop the router
_ = initConfig()
// currently no way to Stop the router
_ = hzconfig.InitConfig()
router := setupRouter()

w := httptest.NewRecorder()
Expand All @@ -30,7 +31,7 @@ func TestGCNotEnoughDataToCollect(t *testing.T) {

func TestNoPastesFound(t *testing.T) {

_ = initConfig()
_ = hzconfig.InitConfig()
router := setupRouter()

w := httptest.NewRecorder()
Expand All @@ -45,7 +46,7 @@ func TestNoPastesFound(t *testing.T) {

func TestInvalidPasteUUID(t *testing.T) {

_ = initConfig()
_ = hzconfig.InitConfig()
router := setupRouter()

wrongUUID := "I am wrongUUID"
Expand All @@ -61,7 +62,7 @@ func TestInvalidPasteUUID(t *testing.T) {

func TestPasteUUIDNotFound(t *testing.T) {

_ = initConfig()
_ = hzconfig.InitConfig()
router := setupRouter()

randomUUID := "ee6edb3a-db64-11eb-8d19-0242ac130003" // random UUID
Expand All @@ -77,7 +78,7 @@ func TestPasteUUIDNotFound(t *testing.T) {

func TestPasteAdd(t *testing.T) {

_ = initConfig()
_ = hzconfig.InitConfig()
router := setupRouter()

newPaste := `{"name":"Very important paste",
Expand Down Expand Up @@ -118,7 +119,7 @@ func TestPasteAdd(t *testing.T) {

func TestKillPastes(t *testing.T) {

_ = initConfig()
_ = hzconfig.InitConfig()
router := setupRouter()

newPaste := `{"name":"Very important paste",
Expand All @@ -142,7 +143,7 @@ func TestKillPastes(t *testing.T) {

func TestPing(t *testing.T) {

_ = initConfig()
_ = hzconfig.InitConfig()
router := setupRouter()

req, _ := http.NewRequest("GET", "/ping", nil)
Expand Down

0 comments on commit 75f506e

Please sign in to comment.