Skip to content

Commit

Permalink
ISSUE-36 - Organize Environment and Logger init (#48)
Browse files Browse the repository at this point in the history
* ISSUE-36 - Organize Environment and Logger init

- Organizing Logger
- Organizing Config
  • Loading branch information
GitHNX authored Jun 10, 2024
1 parent 49f5da9 commit 290418e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 39 deletions.
44 changes: 5 additions & 39 deletions cmd/discord_bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,26 @@ import (
"log"

"github.com/caarlos0/env/v11"
config "github.com/ccil-kbw/robot/internal/config"
environment "github.com/ccil-kbw/robot/internal/environment"
logger "github.com/ccil-kbw/robot/internal/logger"
"github.com/ccil-kbw/robot/pkg/discord"
"go.uber.org/zap"
)

// TODO: Should Types have a folder of their own?
type config struct {
Environment string `env:"ENVIRONMENT" envDefault:"dev"`
DiscordServerID string `env:"DISCORD_SERVER_ID"`
DiscordBotToken string `env:"DISCORD_BOT_TOKEN"`
DiscordBotAsPublic bool `env:"DISCORD_BOT_AS_PUBLIC" envDefault:"true"`
}

var (
cfg = config{}
logger *zap.Logger
)

func init() {
environment.LoadEnvironmentVariables()
logger.InitializeLogger()
loadConfig()
initializeLogger()
}

func main() {
bot := discord.NewDiscordBot(logger, cfg.DiscordServerID, cfg.DiscordBotToken, cfg.DiscordBotAsPublic)
bot := discord.NewDiscordBot(logger.Logger, config.Cfg.DiscordServerID, config.Cfg.DiscordBotToken, config.Cfg.DiscordBotAsPublic)
bot.StartBot()
}

func loadConfig() {
if err := env.Parse(&cfg); err != nil {
if err := env.Parse(&config.Cfg); err != nil {
log.Fatalf("%+v\n", err)
}
log.Println("configuration ready")
}

func initializeLogger() {
var err error
if cfg.Environment == "dev" {
logger, err = zap.NewDevelopment()
if err != nil {
initExampleLoggerWhenFail()
}
logger.Info("Using Development Logger")
} else {
logger, err = zap.NewProduction()
if err != nil {
initExampleLoggerWhenFail()
}
logger.Info("Using Production Logger")
}
}

func initExampleLoggerWhenFail() {
log.Println("can't initialize zap logger, using basic logger")
logger = zap.NewExample()
}
12 changes: 12 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package config

var (
Cfg = config{}
)

type config struct {
Environment string `env:"ENVIRONMENT" envDefault:"dev"`
DiscordServerID string `env:"DISCORD_SERVER_ID"`
DiscordBotToken string `env:"DISCORD_BOT_TOKEN"`
DiscordBotAsPublic bool `env:"DISCORD_BOT_AS_PUBLIC" envDefault:"true"`
}
34 changes: 34 additions & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package logger

import (
"log"

config "github.com/ccil-kbw/robot/internal/config"
"go.uber.org/zap"
)

var (
Logger *zap.Logger
)

func InitializeLogger() {
var err error
if config.Cfg.Environment == "dev" {
Logger, err = zap.NewDevelopment()
if err != nil {
initExampleLoggerWhenFail()
}
Logger.Info("Using Development Logger")
} else {
Logger, err = zap.NewProduction()
if err != nil {
initExampleLoggerWhenFail()
}
Logger.Info("Using Production Logger")
}
}

func initExampleLoggerWhenFail() {
log.Println("can't initialize zap logger, using basic logger")
Logger = zap.NewExample()
}

0 comments on commit 290418e

Please sign in to comment.