From 7828c75dfdf7306753b4a027d7b87eff162a3916 Mon Sep 17 00:00:00 2001 From: Alexandre Pauwels Date: Mon, 9 Oct 2023 14:32:56 +0200 Subject: [PATCH] fix: adds cmd/ directory - It was being ignored due to non-root "slack-bot" being in .gitignore --- .gitignore | 4 +-- cmd/slack-bot/main.go | 82 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 cmd/slack-bot/main.go diff --git a/.gitignore b/.gitignore index 7ff5ba7..c0d8887 100644 --- a/.gitignore +++ b/.gitignore @@ -18,7 +18,7 @@ go.work # executable file -slack-bot +/slack-bot # dot-env files -*.env \ No newline at end of file +*.env diff --git a/cmd/slack-bot/main.go b/cmd/slack-bot/main.go new file mode 100644 index 0000000..b12d55c --- /dev/null +++ b/cmd/slack-bot/main.go @@ -0,0 +1,82 @@ +package main + +import ( + "errors" + "log" + "net/http" + "os" + "strings" + + viperpit "github.com/ajpauwels/pit-of-vipers" + "github.com/pauwels-labs/slack-bot/internal/config" + "github.com/pauwels-labs/slack-bot/pkg/handlers" + "github.com/pauwels-labs/slack-bot/pkg/slack" + "github.com/spf13/viper" + "go.uber.org/zap" +) + +func main() { + // Create structured logger + logger, err := zap.NewProduction() + if err != nil { + log.Fatalf("couldn't initialize structured logger: %v", err) + } + defer logger.Sync() + + // Load env-specific configuration + env := os.Getenv("APPCFG_meta_env") + configPath := "./config" + if len(env) <= 0 { + env = "local" + } + if env != "local" { + configPath = "/etc/slack-bot/config" + } + + // Create viper instances for base and env-specific config files + baseViper := viper.New() + baseViper.AddConfigPath(configPath) + baseViper.SetConfigName("base") + envViper := viper.New() + envViper.AddConfigPath(configPath) + envViper.SetConfigName(env) + + vpCh, errCh := viperpit.New([]*viper.Viper{baseViper, envViper}) + for { + select { + case vp := <-vpCh: + // Workaround to add ENV prefix and be able to unmarshal env-provided values + vp.SetEnvPrefix("APPCFG") + vp.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + for _, key := range vp.AllKeys() { + val := vp.Get(key) + vp.Set(key, val) + } + + // Unmarshal config into struct + var config config.Config + vp.Unmarshal(&config) + + logger.Info("config", zap.Uint16("port", config.Port), zap.String("slack.signingkey", string(config.Slack.SigningKey))) + + // Create the list of supported handlers + echoHandler := handlers.NewEchoHandler() + handlers := []slack.SlackSlashCommandHandler{echoHandler} + + // Create slack bot server + slackBot := slack.NewSlackBot(config.Port, config.Slack.SigningKey, handlers) + logger.Info("starting server", zap.Uint16("port", config.Port)) + err := slackBot.ListenAndServe(logger) + + // Handle normal shutdown and server start errors + if errors.Is(err, http.ErrServerClosed) { + logger.Info("server has shutdown normally") + break + } else { + logger.Fatal("failed to start http server", zap.Error(err)) + } + case err := <-errCh: + logger.Error("error loading config", zap.Error(err)) + } + } +}