mirror of
https://github.com/mjl-/mox.git
synced 2025-07-10 07:54:40 +03:00
webmail: less boilerplate code for api functions
open the account at the beginning of the api handler, and close accounts there too
This commit is contained in:
@ -51,8 +51,9 @@ type ctxKey string
|
||||
var requestInfoCtxKey ctxKey = "requestInfo"
|
||||
|
||||
type requestInfo struct {
|
||||
Log mlog.Log
|
||||
LoginAddress string
|
||||
AccountName string
|
||||
Account *store.Account // Nil only for methods Login and LoginPrep.
|
||||
SessionToken store.SessionToken
|
||||
Response http.ResponseWriter
|
||||
Request *http.Request // For Proto and TLS connection state during message submit.
|
||||
@ -266,7 +267,22 @@ func handle(apiHandler http.Handler, isForwarded bool, accountPath string, w htt
|
||||
}
|
||||
|
||||
if isAPI {
|
||||
reqInfo := requestInfo{loginAddress, accName, sessionToken, w, r}
|
||||
var acc *store.Account
|
||||
if accName != "" {
|
||||
log = log.With(slog.String("account", accName))
|
||||
var err error
|
||||
acc, err = store.OpenAccount(log, accName)
|
||||
if err != nil {
|
||||
log.Errorx("open account", err)
|
||||
http.Error(w, "500 - internal server error - error opening account", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
err := acc.Close()
|
||||
log.Check(err, "closing account")
|
||||
}()
|
||||
}
|
||||
reqInfo := requestInfo{log, loginAddress, acc, sessionToken, w, r}
|
||||
ctx = context.WithValue(ctx, requestInfoCtxKey, reqInfo)
|
||||
apiHandler.ServeHTTP(w, r.WithContext(ctx))
|
||||
return
|
||||
|
Reference in New Issue
Block a user