From 6d4b4de38a14e28b1747dab1b5947e0cd94082f7 Mon Sep 17 00:00:00 2001 From: Michael Aldridge Date: Sun, 23 Aug 2020 15:31:20 -0700 Subject: [PATCH] main: Handle initialization and shutdown --- internal/ldap/ldap.go | 11 ++--------- main.go | 24 +++++++++++++++++++++--- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/internal/ldap/ldap.go b/internal/ldap/ldap.go index caf1563..46771bb 100644 --- a/internal/ldap/ldap.go +++ b/internal/ldap/ldap.go @@ -1,10 +1,7 @@ package ldap import ( - "os" - "os/signal" "strings" - "syscall" "github.com/hashicorp/go-hclog" ldap "github.com/ps78674/ldapserver" @@ -36,18 +33,14 @@ func New(l hclog.Logger, nacl naClient) *server { // Serve serves a plaintext DSA on the provided bind string. func (s *server) Serve(bind string) error { chErr := make(chan error) - defer close(chErr) go s.ListenAndServe(bind, chErr) if err := <-chErr; err != nil { s.l.Error("Error from main server thread", "error", err) return err } + return nil +} - ch := make(chan os.Signal, 5) - signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) - <-ch - close(ch) - s.Stop() return nil } diff --git a/main.go b/main.go index 093bb09..b139e5d 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,8 @@ package main import ( "log" "os" + "os/signal" + "syscall" "github.com/hashicorp/go-hclog" "github.com/netauth/ldap/internal/ldap" @@ -23,7 +25,16 @@ func main() { appLogger = hclog.NewNullLogger() } - log.SetOutput(appLogger.Named("ldap.protocol").StandardWriter(&hclog.StandardLoggerOptions{ForceLevel: hclog.Trace})) + // Take over the built in logger and set it up for Trace level + // priority. The only thing that logs at this priority are + // protocol messages from the underlying ldap server mux. + log.SetOutput(appLogger.Named("ldap.protocol"). + StandardWriter( + &hclog.StandardLoggerOptions{ + ForceLevel: hclog.Trace, + }, + ), + ) log.SetPrefix("") log.SetFlags(0) @@ -43,11 +54,18 @@ func main() { ls := ldap.New(appLogger, nacl) - ls.SetDomain("netauth.org") + ls.SetDomain(viper.GetString("ldap.domain")) - if err := ls.Serve("localhost:10389"); err != nil { + if err := ls.Serve(viper.GetString("ldap.bind")); err != nil { appLogger.Error("Error serving", "error", err) return } + + // Sit here and wait for a signal to shutdown. + ch := make(chan os.Signal, 5) + signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) + <-ch + ls.Stop() + appLogger.Info("Goodbye!") }