main: Handle initialization and shutdown

This commit is contained in:
Michael Aldridge 2020-08-23 15:31:20 -07:00
parent 3c3d6979bc
commit 6d4b4de38a
2 changed files with 23 additions and 12 deletions

View file

@ -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
}

24
main.go
View file

@ -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!")
}