generated from pauwels-labs/template-golang-http
-
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.
- It was being ignored due to non-root "slack-bot" being in .gitignore
- Loading branch information
Showing
2 changed files
with
84 additions
and
2 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 |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
go.work | ||
|
||
# executable file | ||
slack-bot | ||
/slack-bot | ||
|
||
# dot-env files | ||
*.env | ||
*.env |
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,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)) | ||
} | ||
} | ||
} |