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:
Mechiel Lukkien
2024-04-21 21:32:24 +02:00
parent ed0c520562
commit a3f5fd26a6
5 changed files with 236 additions and 336 deletions

View File

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