cmd/koushin: add -debug flag

This commit is contained in:
Simon Ser 2020-02-11 18:39:52 +01:00
parent e59ad57e32
commit 13f5414691
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
4 changed files with 18 additions and 3 deletions

View file

@ -23,9 +23,10 @@ func main() {
var addr string var addr string
flag.StringVar(&options.Theme, "theme", "", "default theme") flag.StringVar(&options.Theme, "theme", "", "default theme")
flag.StringVar(&addr, "addr", ":1323", "listening address") flag.StringVar(&addr, "addr", ":1323", "listening address")
flag.BoolVar(&options.Debug, "debug", false, "enable debug logs")
flag.Usage = func() { flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "usage: koushin [options...] <upstream server...>\n") fmt.Fprintf(flag.CommandLine.Output(), "usage: koushin [options...] <upstream servers...>\n")
flag.PrintDefaults() flag.PrintDefaults()
} }
@ -48,6 +49,10 @@ func main() {
} }
e.Use(middleware.Recover()) e.Use(middleware.Recover())
if options.Debug {
e.Logger.SetLevel(log.DEBUG)
}
sigs := make(chan os.Signal, 1) sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGUSR1) signal.Notify(sigs, syscall.SIGUSR1)
go func() { go func() {

View file

@ -33,6 +33,8 @@ The following URL schemes are supported:
**-addr**: listening address (default: ":1323") **-addr**: listening address (default: ":1323")
**-debug**: enable debug logs and print IMAP network activity
**-h**, **--help**: show help message and exit **-h**, **--help**: show help message and exit
# SIGNALS # SIGNALS

View file

@ -59,7 +59,7 @@ func newServer(e *echo.Echo, options *Options) (*Server, error) {
return nil, err return nil, err
} }
s.Sessions = newSessionManager(s.dialIMAP, s.dialSMTP, e.Logger) s.Sessions = newSessionManager(s.dialIMAP, s.dialSMTP, e.Logger, options.Debug)
return s, nil return s, nil
} }
@ -283,6 +283,7 @@ func handleUnauthenticated(next echo.HandlerFunc, ctx *Context) error {
type Options struct { type Options struct {
Upstreams []string Upstreams []string
Theme string Theme string
Debug bool
} }
// New creates a new server. // New creates a new server.

View file

@ -6,6 +6,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
"os"
"sync" "sync"
"time" "time"
@ -142,17 +143,19 @@ type SessionManager struct {
dialIMAP DialIMAPFunc dialIMAP DialIMAPFunc
dialSMTP DialSMTPFunc dialSMTP DialSMTPFunc
logger echo.Logger logger echo.Logger
debug bool
locker sync.Mutex locker sync.Mutex
sessions map[string]*Session // protected by locker sessions map[string]*Session // protected by locker
} }
func newSessionManager(dialIMAP DialIMAPFunc, dialSMTP DialSMTPFunc, logger echo.Logger) *SessionManager { func newSessionManager(dialIMAP DialIMAPFunc, dialSMTP DialSMTPFunc, logger echo.Logger, debug bool) *SessionManager {
return &SessionManager{ return &SessionManager{
sessions: make(map[string]*Session), sessions: make(map[string]*Session),
dialIMAP: dialIMAP, dialIMAP: dialIMAP,
dialSMTP: dialSMTP, dialSMTP: dialSMTP,
logger: logger, logger: logger,
debug: debug,
} }
} }
@ -167,6 +170,10 @@ func (sm *SessionManager) connectIMAP(username, password string) (*imapclient.Cl
return nil, AuthError{err} return nil, AuthError{err}
} }
if sm.debug {
c.SetDebug(os.Stderr)
}
return c, nil return c, nil
} }