-
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 #9 from systemli/Add-Logging-and-extract-Config
♻️ Add Logging and extract Config
- Loading branch information
Showing
8 changed files
with
195 additions
and
57 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// Config is the configuration for the application. | ||
type Config struct { | ||
// UserliToken is the token for the userli service. | ||
UserliToken string | ||
|
||
// UserliBaseURL is the base URL for the userli service. | ||
UserliBaseURL string | ||
|
||
// AliasListenAddr is the address to listen for alias requests. | ||
AliasListenAddr string | ||
|
||
// DomainListenAddr is the address to listen for domain requests. | ||
DomainListenAddr string | ||
|
||
// MailboxListenAddr is the address to listen for mailbox requests. | ||
MailboxListenAddr string | ||
|
||
// SendersListenAddr is the address to listen for senders requests. | ||
SendersListenAddr string | ||
} | ||
|
||
// NewConfig creates a new Config with default values. | ||
func NewConfig() *Config { | ||
logLevel := os.Getenv("LOG_LEVEL") | ||
if logLevel == "" { | ||
logLevel = "info" | ||
} | ||
|
||
logFormat := os.Getenv("LOG_FORMAT") | ||
if logFormat == "" { | ||
logFormat = "text" | ||
} | ||
|
||
level, err := log.ParseLevel(logLevel) | ||
if err != nil { | ||
log.WithError(err).Fatal("Failed to parse log level") | ||
} | ||
log.SetLevel(level) | ||
|
||
if logFormat == "json" { | ||
log.SetFormatter(&log.JSONFormatter{}) | ||
} else { | ||
log.SetFormatter(&log.TextFormatter{}) | ||
} | ||
|
||
userliBaseURL := os.Getenv("USERLI_BASE_URL") | ||
if userliBaseURL == "" { | ||
userliBaseURL = "http://localhost:8000" | ||
} | ||
|
||
userliToken := os.Getenv("USERLI_TOKEN") | ||
if userliToken == "" { | ||
log.Fatal("USERLI_TOKEN is required") | ||
} | ||
|
||
aliasListenAddr := os.Getenv("ALIAS_LISTEN_ADDR") | ||
if aliasListenAddr == "" { | ||
aliasListenAddr = ":10001" | ||
} | ||
|
||
domainListenAddr := os.Getenv("DOMAIN_LISTEN_ADDR") | ||
if domainListenAddr == "" { | ||
domainListenAddr = ":10002" | ||
} | ||
|
||
mailboxListenAddr := os.Getenv("MAILBOX_LISTEN_ADDR") | ||
if mailboxListenAddr == "" { | ||
mailboxListenAddr = ":10003" | ||
} | ||
|
||
sendersListenAddr := os.Getenv("SENDERS_LISTEN_ADDR") | ||
if sendersListenAddr == "" { | ||
sendersListenAddr = ":10004" | ||
} | ||
|
||
return &Config{ | ||
UserliBaseURL: userliBaseURL, | ||
UserliToken: userliToken, | ||
AliasListenAddr: aliasListenAddr, | ||
DomainListenAddr: domainListenAddr, | ||
MailboxListenAddr: mailboxListenAddr, | ||
SendersListenAddr: sendersListenAddr, | ||
} | ||
} |
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,61 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type ConfigTestSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func (s *ConfigTestSuite) TestNewConfig() { | ||
s.Run("fail when userli token not set", func() { | ||
defer func() { log.StandardLogger().ExitFunc = nil }() | ||
var fatal bool | ||
log.StandardLogger().ExitFunc = func(int) { fatal = true } | ||
|
||
_ = NewConfig() | ||
|
||
s.True(fatal) | ||
}) | ||
|
||
s.Run("default config", func() { | ||
os.Setenv("USERLI_TOKEN", "token") | ||
|
||
config := NewConfig() | ||
|
||
s.Equal("token", config.UserliToken) | ||
s.Equal("http://localhost:8000", config.UserliBaseURL) | ||
s.Equal(":10001", config.AliasListenAddr) | ||
s.Equal(":10002", config.DomainListenAddr) | ||
s.Equal(":10003", config.MailboxListenAddr) | ||
s.Equal(":10004", config.SendersListenAddr) | ||
}) | ||
|
||
s.Run("custom config", func() { | ||
os.Setenv("USERLI_TOKEN", "token") | ||
os.Setenv("USERLI_BASE_URL", "http://example.com") | ||
os.Setenv("ALIAS_LISTEN_ADDR", ":20001") | ||
os.Setenv("DOMAIN_LISTEN_ADDR", ":20002") | ||
os.Setenv("MAILBOX_LISTEN_ADDR", ":20003") | ||
os.Setenv("SENDERS_LISTEN_ADDR", ":20004") | ||
|
||
config := NewConfig() | ||
|
||
s.Equal("token", config.UserliToken) | ||
s.Equal("http://example.com", config.UserliBaseURL) | ||
s.Equal(":20001", config.AliasListenAddr) | ||
s.Equal(":20002", config.DomainListenAddr) | ||
s.Equal(":20003", config.MailboxListenAddr) | ||
s.Equal(":20004", config.SendersListenAddr) | ||
}) | ||
} | ||
|
||
func TestConfig(t *testing.T) { | ||
suite.Run(t, new(ConfigTestSuite)) | ||
} |
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.