Skip to content

Commit

Permalink
refactor: removed zap in favor for wrapped slog
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan committed Nov 29, 2024
1 parent 6b92d52 commit 37d5f45
Show file tree
Hide file tree
Showing 77 changed files with 821 additions and 681 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TRXX_DEBUG_MODE=true #enables debug mode
DEBUG_LOG=true #enables debug logging
21 changes: 10 additions & 11 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@ import (
"github.com/eisenwinter/gotrxx/config"
"github.com/eisenwinter/gotrxx/i18n"
"github.com/eisenwinter/gotrxx/manage"
"github.com/eisenwinter/gotrxx/pkg/logging"
"github.com/eisenwinter/gotrxx/tokens"
"github.com/eisenwinter/gotrxx/user"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/jwtauth/v5"

ar "github.com/eisenwinter/gotrxx/api/app/account"

"go.uber.org/zap"
)

var tokenAuth *jwtauth.JWTAuth

func compose(logger *zap.Logger,
func compose(logger logging.Logger,
cfg *config.Configuration,
issuer *tokens.TokenIssuer,
signInService *user.SigninService,
Expand Down Expand Up @@ -74,7 +73,7 @@ func compose(logger *zap.Logger,
}

connectRessource := connect.NewConnnectRessource(
logger.Named("connect_ressource"),
logger.WithGroup("connect_ressource"),
tokenAuth,
issuer,
rotator,
Expand All @@ -84,13 +83,13 @@ func compose(logger *zap.Logger,
verifier,
)
netlifyRessource := netlify.NewNetlifyRessource(
logger.Named("netlify_ressource"),
logger.WithGroup("netlify_ressource"),
tokenAuth,
connectRessource,
rotator,
)
accountRessource := ar.NewAccountRessource(
logger.Named("account_ressource"),
logger.WithGroup("account_ressource"),
signInService,
cfg.Behaviour,
userService,
Expand All @@ -102,11 +101,11 @@ func compose(logger *zap.Logger,
fileSystems,
verifier,
)
metaRessource := meta.NewMetaRessource(logger.Named("meta_ressource"), cfg.Behaviour, issuer)
metaRessource := meta.NewMetaRessource(logger.WithGroup("meta_ressource"), cfg.Behaviour, issuer)

if cfg.ManageEndpoint.Enable {
manageRessource := management.NewManagementRessource(
logger.Named("management_ressource"),
logger.WithGroup("management_ressource"),
*cfg,
tokenAuth,
manageUserService,
Expand Down Expand Up @@ -135,17 +134,17 @@ func compose(logger *zap.Logger,
buffer := make([]byte, s.Size())
_, err = favicon.Read(buffer)
if err != nil {
logger.Warn("Unable to load favicon", zap.Error(err))
logger.Warn("Unable to load favicon", "err", err)
}
_, err = w.Write(buffer)
if err != nil {
logger.Warn("Unable to write favicon", zap.Error(err))
logger.Warn("Unable to write favicon", "err", err)
}
return
}

}
logger.Warn("No favicon found", zap.Error(err))
logger.Warn("no favicon found", "err", err)
})

r.Get("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
Expand Down
121 changes: 67 additions & 54 deletions api/app/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
"github.com/eisenwinter/gotrxx/authorization"
"github.com/eisenwinter/gotrxx/config"
"github.com/eisenwinter/gotrxx/i18n"
"github.com/eisenwinter/gotrxx/pkg/logging"
"github.com/eisenwinter/gotrxx/tokens"
"github.com/eisenwinter/gotrxx/user"
"github.com/go-chi/chi/v5"
"github.com/google/safehtml/template"
"github.com/google/uuid"
"github.com/gorilla/csrf"
"github.com/lestrrat-go/jwx/v2/jwt"
"go.uber.org/zap"
)

const gotrxxClientID = "$.gotrxx"
Expand All @@ -45,7 +45,7 @@ type AccountRessource struct {

registry *i18n.TranslationRegistry

log *zap.Logger
log logging.Logger
userSignIn SignIner
userService UserService
autService AuthorizationService
Expand Down Expand Up @@ -135,10 +135,10 @@ func (a *AccountRessource) getTranslatorFor(ctx context.Context, page string) *i
t, err := a.registry.TranslatorFor(locale, res)
if err != nil {
if errors.Is(i18n.ErrLanguageDoesntExist, err) {
a.log.Error("[i18n] languages doesnt exist", zap.String("iso", locale))
a.log.Error("[i18n] languages doesnt exist", "iso", locale)
}
if errors.Is(i18n.ErrRessourceDoesNotExist, err) {
a.log.Error("[i18n] ressource doesnt exist", zap.String("ressource", res))
a.log.Error("[i18n] ressource doesnt exist", "ressource", res)
}
return a.registry.CreateVoidTranslator(locale, res)
}
Expand All @@ -149,7 +149,7 @@ func (a *AccountRessource) signedInUser(w http.ResponseWriter, r *http.Request)
tokenCookie, err := r.Cookie(jwtCookie)
if err != nil {
if errors.Is(http.ErrNoCookie, err) {
a.log.Debug("account ressource: no jwt cookie", zap.Error(err))
a.log.Debug("account ressource: no jwt cookie", "err", err)
rememberMe, err := r.Cookie(rememberMeCookie)
if err != nil {
return false, nil
Expand All @@ -160,7 +160,7 @@ func (a *AccountRessource) signedInUser(w http.ResponseWriter, r *http.Request)
}
return true, token
}
a.log.Debug("account ressource: error reading cookie", zap.Error(err))
a.log.Debug("account ressource: error reading cookie", "err", err)
return false, nil
}
a.log.Debug("account ressource: reading jwt")
Expand Down Expand Up @@ -202,7 +202,7 @@ func (a *AccountRessource) signedInUser(w http.ResponseWriter, r *http.Request)
return true, token
}
}
a.log.Error("account ressrouce: unexpected JWT error after reading cookie", zap.Error(err))
a.log.Error("account ressrouce: unexpected JWT error after reading cookie", "err", err)
clearCookie()
return false, nil
}
Expand Down Expand Up @@ -257,12 +257,12 @@ func (a *AccountRessource) issueUserCookie(
auth.Scopes(),
)
if err != nil {
a.log.Error("user login page: failed to issue a new access token", zap.Error(err))
a.log.Error("user login page: failed to issue a new access token", "err", err)
return nil, err
}
signed, err := a.issuer.Sign(t)
if err != nil {
a.log.Error("user login page: failed to sign a access token", zap.Error(err))
a.log.Error("user login page: failed to sign a access token", "err", err)
return nil, err
}
expires := int(t.Expiration().Sub(time.Now().UTC()).Seconds())
Expand Down Expand Up @@ -322,16 +322,16 @@ func (a *AccountRessource) view(
if err != nil {
a.log.Error(
"unable to render template for page",
zap.String("template", name),
zap.Error(err),
"template", name,
"err", err,
)
}
}

func (a *AccountRessource) changeLanguage(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
a.log.Error("change lang: ParseForm failed", zap.Error(err))
a.log.Error("change lang: ParseForm failed", "err", err)
}
returnURL := r.FormValue("return_url")
lang := strings.ToLower(r.FormValue("lang"))
Expand All @@ -352,7 +352,7 @@ func (a *AccountRessource) changeLanguage(w http.ResponseWriter, r *http.Request
func (a *AccountRessource) signout(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
a.log.Error("signin: ParseForm failed", zap.Error(err))
a.log.Error("signin: ParseForm failed", "err", err)
}

jwtc, err := r.Cookie(jwtCookie)
Expand Down Expand Up @@ -416,7 +416,7 @@ func (a *AccountRessource) signout(w http.ResponseWriter, r *http.Request) {
if errors.Is(tokens.ErrTokenNotFound, err) {
a.log.Warn("could not find remember me token to revoke")
} else {
a.log.Error("could not revoke remember me token on sign out", zap.Error(err))
a.log.Error("could not revoke remember me token on sign out", "err", err)
}

}
Expand All @@ -433,7 +433,7 @@ func sanitizeReturnURL(returnURL string, fallback string) string {
return parsed.RequestURI()
}

func NewAccountRessource(log *zap.Logger,
func NewAccountRessource(log logging.Logger,
userSignIn SignIner,
cfg *config.BehaviourConfiguration,
userService UserService,
Expand All @@ -447,100 +447,110 @@ func NewAccountRessource(log *zap.Logger,

loginTmpl, err := mustLoadTemplate(fsConfig.Pages, "signin.html", log)
if err != nil {
log.Fatal(
log.Error(
"unable to load required template file",
zap.String("file", "signin.html"),
zap.Error(err),
"file", "signin.html",
"err", err,
)
panic("unable to load required template file")
}
signUpTmpl, err := mustLoadTemplate(fsConfig.Pages, "signup.html", log)
if err != nil {
log.Fatal(
log.Error(
"unable to load required template file",
zap.String("file", "signup.html"),
zap.Error(err),
"file", "signup.html",
"err", err,
)
panic("unable to load required template file")
}
userPageTmpl, err := mustLoadTemplate(fsConfig.Pages, "user.html", log)
if err != nil {
log.Fatal(
log.Error(
"unable to load required template file",
zap.String("file", "user.html"),
zap.Error(err),
"file", "user.html",
"err", err,
)
panic("unable to load required template file")
}
confirmTemplate, err := mustLoadTemplate(fsConfig.Pages, "confirm.html", log)
if err != nil {
log.Fatal(
log.Error(
"unable to load required template file",
zap.String("file", "confirm.html"),
zap.Error(err),
"file", "confirm.html",
"err", err,
)
panic("unable to load required template file")
}
recoverTemplate, err := mustLoadTemplate(
fsConfig.Pages,
"recover_password.html",
log,
)
if err != nil {
log.Fatal(
log.Error(
"unable to load required template file",
zap.String("file", "recover_password.html"),
zap.Error(err),
"file", "recover_password.html",
"err", err,
)
panic("unable to load required template file")
}
requestRecoverTmpl, err := mustLoadTemplate(
fsConfig.Pages,
"request_password_recovery.html",
log,
)
if err != nil {
log.Fatal(
log.Error(
"unable to load required template file",
zap.String("file", "request_password_recovery.html"),
zap.Error(err),
"file", "request_password_recovery.html",
"err", err,
)
panic("unable to load required template file")
}
errorTemplate, err := mustLoadTemplate(fsConfig.Pages, "error.html", log)
if err != nil {
log.Fatal(
log.Error(
"unable to load required template file",
zap.String("file", "error.html"),
zap.Error(err),
"file", "error.html",
"err", err,
)
panic("unable to load required template file")
}
changePasswordTemplate, err := mustLoadTemplate(
fsConfig.Pages,
"change_password.html",
log,
)
if err != nil {
log.Fatal(
log.Error(
"unable to load required template file",
zap.String("file", "change_password.html"),
zap.Error(err),
"file", "change_password.html",
"err", err,
)
panic("unable to load required template file")
}
changeEmailTemplate, err := mustLoadTemplate(
fsConfig.Pages,
"change_email.html",
log,
)
if err != nil {
log.Fatal(
log.Error(
"unable to load required template file",
zap.String("file", "change_email.html"),
zap.Error(err),
"file", "change_email.html",
"err", err,
)
panic("unable to load required template file")
}

changeMfaTemplate, err := mustLoadTemplate(fsConfig.Pages, "change_mfa.html", log)
if err != nil {
log.Fatal(
log.Error(
"unable to load required template file",
zap.String("file", "change_mfa.html"),
zap.Error(err),
"file", "change_mfa.html",
"err", err,
)
panic("unable to load required template file")
}

provisionMfaTemplate, err := mustLoadTemplate(
Expand All @@ -549,29 +559,32 @@ func NewAccountRessource(log *zap.Logger,
log,
)
if err != nil {
log.Fatal(
log.Error(
"unable to load required template file",
zap.String("file", "provision_mfa.html"),
zap.Error(err),
"file", "provision_mfa.html",
"err", err,
)
panic("unable to load required template file")
}

inviteTemplate, err := mustLoadTemplate(fsConfig.Pages, "invite.html", log)
if err != nil {
log.Fatal(
log.Error(
"unable to load required template file",
zap.String("file", "invite.html"),
zap.Error(err),
"file", "invite.html",
"err", err,
)
panic("unable to load required template file")
}

fourOFour, err := mustLoadTemplate(fsConfig.Pages, "404.html", log)
if err != nil {
log.Fatal(
log.Error(
"unable to load required template file",
zap.String("file", "404.html"),
zap.Error(err),
"file", "404.html",
"err", err,
)
panic("unable to load required template file")
}

return &AccountRessource{
Expand Down
Loading

0 comments on commit 37d5f45

Please sign in to comment.