Skip to content

Commit

Permalink
Wire all the things together
Browse files Browse the repository at this point in the history
  • Loading branch information
pcarranza committed Sep 23, 2018
1 parent ff729f5 commit 1e1b98e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
2 changes: 1 addition & 1 deletion configuration/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Load(in []byte) error {
c := Configuration{}
err := yaml.Unmarshal(in, &c)
if err != nil {
return fmt.Errorf("failed to parse configuration: %s", err)
return fmt.Errorf("failed to parse: %s", err)
}

config = c
Expand Down
60 changes: 55 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package main

import (
"flag"
"io/ioutil"
"os"
"os/signal"
"syscall"

"gitlab.com/yakshaving.art/propaganda/configuration"
"gitlab.com/yakshaving.art/propaganda/core"
"gitlab.com/yakshaving.art/propaganda/github"
"gitlab.com/yakshaving.art/propaganda/gitlab"
Expand Down Expand Up @@ -31,15 +35,46 @@ func main() {
gitlab.NewParser(args.MatchString),
})

logrus.Fatal(s.ListenAndServe(args.Address))
go func() {
logrus.Fatal(s.ListenAndServe(args.Address))
}()

signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGUSR1, syscall.SIGUSR2)

for sig := range signalCh {
switch sig {
case syscall.SIGHUP:
logrus.Info("Reloading the configuration")
loadConfiguration(args)

case syscall.SIGUSR1:
toggleLogLevel()

case syscall.SIGINT:
logrus.Info("Shutting down gracefully")
s.Shutdown()
os.Exit(0)
}
}
}

func setupLogger() {
logrus.AddHook(filename.NewHook())
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
})
logrus.SetLevel(logrus.DebugLevel)
}

func toggleLogLevel() {
switch logrus.GetLevel() {
case logrus.DebugLevel:
logrus.Infof("setting info log level")
logrus.SetLevel(logrus.InfoLevel)
default:
logrus.Infof("settings debug log level")
logrus.SetLevel(logrus.DebugLevel)
}
}

// Args represents the commandline arguments
Expand All @@ -51,6 +86,7 @@ type Args struct {
MatchString string

ConfigFile string
Debug bool
}

func parseArgs() Args {
Expand All @@ -61,15 +97,29 @@ func parseArgs() Args {
flag.StringVar(&args.WebhookURL, "webhook-url", os.Getenv("SLACK_WEBHOOK_URL"), "slack webhook url")
flag.StringVar(&args.MatchString, "match-pattern", "\\[announce\\]", "match string")
flag.StringVar(&args.ConfigFile, "config", "propaganda.yml", "configuration file to use")
flag.BoolVar(&args.Debug, "debug", false, "enable debug logging")
flag.Parse()

if args.Debug {
toggleLogLevel()
}

if args.WebhookURL == "" {
logrus.Fatalf("no slack webhook url, define it through -webhook-url argument or SLACK_WEBHOOK_URL env var")
}

if _, err := os.Stat(args.ConfigFile); err != nil {
logrus.Fatalf("failed to stat configuration file %s: %s", args.ConfigFile, err)
}
loadConfiguration(args)

return args
}

func loadConfiguration(args Args) {
content, err := ioutil.ReadFile(args.ConfigFile)
if err != nil {
logrus.Errorf("failed to read configuration file %s: %s", args.ConfigFile, err)
}

if err = configuration.Load(content); err != nil {
logrus.Errorf("failed to load configuration file %s: %s", args.ConfigFile, err)
}
}
16 changes: 12 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

// New returns a new Server with the provided parsers
func New(announcer core.Announcer, parsers []core.Parser) Server {
return Server{
func New(announcer core.Announcer, parsers []core.Parser) *Server {
return &Server{
parsers: parsers,
announcer: announcer,
}
Expand All @@ -23,15 +23,23 @@ func New(announcer core.Announcer, parsers []core.Parser) Server {
type Server struct {
parsers []core.Parser
announcer core.Announcer
server *http.Server
}

// ListenAndServe starts listening and serving traffic
func (s Server) ListenAndServe(addr string) error {
func (s *Server) ListenAndServe(addr string) error {
http.HandleFunc("/", s.handle)

metrics.Up.Set(1)
logrus.Infof("listening on %s", addr)
return http.ListenAndServe(addr, nil)

s.server = &http.Server{Addr: addr}
return s.server.ListenAndServe()
}

// Shutdown closes the server so it stops listening
func (s *Server) Shutdown() error {
return s.server.Close()
}

func (s Server) handle(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit 1e1b98e

Please sign in to comment.