-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from luizvnasc/issue_13
Criado package config
- Loading branch information
Showing
15 changed files
with
323 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Package config obtém as configurações da app que estão nas variáveis de ambiente | ||
package config | ||
|
||
import "os" | ||
|
||
const ( | ||
prefix = "CWBUS_" | ||
serviceURL = "URBS_SERVICE_URL" | ||
urbsCode = "URBS_CODE" | ||
dbStrConn = "DB_URL" | ||
dbName = "DB_HIST" | ||
wakeUpURL = "WAKEUP_URL" | ||
) | ||
|
||
// Configurer é a interface que define um configurador no sistema. | ||
type Configurer interface { | ||
ServiceURL() string | ||
UrbsCode() string | ||
DBName() string | ||
DBStrConn() string | ||
WakeUpURL() string | ||
} | ||
|
||
// EnvConfigurer é um confiurador que capitura as configurações das variáveis de ambiente. | ||
type EnvConfigurer struct{} | ||
|
||
func (ec EnvConfigurer) key(name string) string { | ||
return prefix + name | ||
} | ||
|
||
func (ec EnvConfigurer) getValue(name string) string { | ||
return os.Getenv(ec.key(name)) | ||
} | ||
|
||
// ServiceURL retorna a URL dos serviços da urbs. | ||
func (ec EnvConfigurer) ServiceURL() string { | ||
return ec.getValue(serviceURL) | ||
} | ||
|
||
// UrbsCode retorna o código urbs de acesso aos serviços. | ||
func (ec EnvConfigurer) UrbsCode() string { | ||
return ec.getValue(urbsCode) | ||
} | ||
|
||
// DBStrConn retorna a string de conexão do banco de dados. | ||
func (ec EnvConfigurer) DBStrConn() string { | ||
return ec.getValue(dbStrConn) | ||
} | ||
|
||
// DBName retorna o nome do banco de dados. | ||
func (ec EnvConfigurer) DBName() string { | ||
return ec.getValue(dbName) | ||
} | ||
|
||
// WakeUpURL retorna a url utilizada para acordar o dyno do heroku | ||
func (ec EnvConfigurer) WakeUpURL() string { | ||
return ec.getValue(wakeUpURL) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/luizvnasc/cwbus-hist/test" | ||
) | ||
|
||
func TestConfig(t *testing.T) { | ||
ec := EnvConfigurer{} | ||
t.Run("Obtendo url de serviços da urbs", func(t *testing.T) { | ||
want := os.Getenv("CWBUS_URBS_SERVICE_URL") | ||
got := ec.ServiceURL() | ||
test.AssertStringsEqual(t, want, got) | ||
}) | ||
|
||
t.Run("Obtendo codigo urbs", func(t *testing.T) { | ||
want := os.Getenv("CWBUS_URBS_CODE") | ||
got := ec.UrbsCode() | ||
test.AssertStringsEqual(t, want, got) | ||
}) | ||
|
||
t.Run("Obtendo conexão com o banco", func(t *testing.T) { | ||
want := os.Getenv("CWBUS_DB_URL") | ||
got := ec.DBStrConn() | ||
test.AssertStringsEqual(t, want, got) | ||
}) | ||
|
||
t.Run("Obtendo nome do banco", func(t *testing.T) { | ||
want := os.Getenv("CWBUS_DB_HIST") | ||
got := ec.DBName() | ||
test.AssertStringsEqual(t, want, got) | ||
}) | ||
|
||
t.Run("Obtendo url para acordar dyno", func(t *testing.T) { | ||
want := os.Getenv("CWBUS_WAKEUP_URL") | ||
got := ec.WakeUpURL() | ||
test.AssertStringsEqual(t, want, got) | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.