Skip to content

Commit

Permalink
simplify healthcheck and override port
Browse files Browse the repository at this point in the history
  • Loading branch information
Depado committed Nov 17, 2020
1 parent 3a0ae7b commit 95ae550
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
conf.*yml
fox
dockervol/
*.priv
12 changes: 9 additions & 3 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package bot

import (
"fmt"
"net/http"
"sync"

"github.com/Depado/fox/acl"
"github.com/Depado/fox/cmd"
"github.com/Depado/fox/commands"
"github.com/Depado/fox/healthcheck"
"github.com/bwmarrin/discordgo"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
)

Expand Down Expand Up @@ -75,9 +76,14 @@ func (b *Bot) AddCommand(c commands.Command) {
}
}

func Run(l *zerolog.Logger, b *Bot, c *cmd.Conf, r *healthcheck.HealthCheck) {
func Run(l *zerolog.Logger, b *Bot, c *cmd.Conf) {
go func() {
if err := r.Engine.Run(fmt.Sprintf(":%d", c.Port)); err != nil {
gin.SetMode("release")
r := gin.New()
r.GET("/", func(c *gin.Context) {
c.Status(http.StatusOK)
})
if err := r.Run(fmt.Sprintf(":%d", c.Port)); err != nil {
l.Fatal().Err(err).Msg("unable to start healthcheck router")
}
}()
Expand Down
11 changes: 11 additions & 0 deletions cmd/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"strconv"
"strings"

"github.com/rs/zerolog"
Expand Down Expand Up @@ -103,5 +104,15 @@ func NewConf() (*Conf, error) {
return conf, fmt.Errorf("unable to unmarshal conf: %w", err)
}

// Override port value for top-level declaration
p := os.Getenv("PORT")
if p != "" {
port, err := strconv.Atoi(p)
if err != nil {
return conf, fmt.Errorf("given port isn't an integer: %w", err)
}
conf.Port = port
}

return conf, nil
}
22 changes: 0 additions & 22 deletions healthcheck/healthcheck.go

This file was deleted.

3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/Depado/fox/bot"
"github.com/Depado/fox/cmd"
"github.com/Depado/fox/commands"
"github.com/Depado/fox/healthcheck"
"github.com/Depado/fox/player"
"github.com/Depado/fox/session"
sp "github.com/Depado/fox/soundcloud"
Expand Down Expand Up @@ -43,7 +42,7 @@ var versionCmd = &cobra.Command{
func run() {
fx.New(
fx.Provide(
cmd.NewConf, cmd.NewLogger, acl.NewACL, healthcheck.NewHealthcheck,
cmd.NewConf, cmd.NewLogger, acl.NewACL,
session.NewDiscordSession,
player.NewPlayer,
soundcloud.NewAutoIDClient, sp.NewSoundCloudProvider,
Expand Down

0 comments on commit 95ae550

Please sign in to comment.