From c5afd1a61b06b3d5bd7287d1596b05d4c18ac138 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 9 Dec 2019 19:25:45 +0100 Subject: Reconnect to IMAP server when logged out The session manager has been upgraded to deal with reconnections. Each session has its own expiration timer. Each time a request is received, the expiration timer is reset. A session can be closed (this is used when the user wants to logout). When the IMAP connection is closed by the server, it's set to nil in the session. The next time an IMAP command needs to be issued, the connection is re-established. Closes: https://todo.sr.ht/~sircmpwn/koushin/30 --- handlers.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'handlers.go') diff --git a/handlers.go b/handlers.go index 869e1c1..4e127b4 100644 --- a/handlers.go +++ b/handlers.go @@ -81,14 +81,14 @@ func handleLogin(ectx echo.Context) error { username := ctx.FormValue("username") password := ctx.FormValue("password") if username != "" && password != "" { - token, err := ctx.server.sessions.Put(username, password) + s, err := ctx.server.sessions.Put(username, password) if err != nil { if _, ok := err.(AuthError); ok { return ctx.Render(http.StatusOK, "login.html", nil) } return fmt.Errorf("failed to put connection in pool: %v", err) } - ctx.setToken(token) + ctx.setToken(s.Token) return ctx.Redirect(http.StatusFound, "/mailbox/INBOX") } @@ -99,13 +99,7 @@ func handleLogin(ectx echo.Context) error { func handleLogout(ectx echo.Context) error { ctx := ectx.(*context) - err := ctx.session.Do(func(c *imapclient.Client) error { - return c.Logout() - }) - if err != nil { - return fmt.Errorf("failed to logout: %v", err) - } - + ctx.session.Close() ctx.setToken("") return ctx.Redirect(http.StatusFound, "/login") } -- cgit v1.2.3-59-g8ed1b