🏗️ Used middleware.Chain in main with LogFmt logger

This commit is contained in:
Maxim Lebedev 2023-11-18 19:29:41 +06:00
parent f9f75f079f
commit e96380307b
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
1 changed files with 112 additions and 110 deletions

12
main.go
View File

@ -28,6 +28,7 @@ import (
"source.toby3d.me/toby3d/home/internal/common"
"source.toby3d.me/toby3d/home/internal/domain"
"source.toby3d.me/toby3d/home/internal/middleware"
"source.toby3d.me/toby3d/home/internal/page"
pagefsrepo "source.toby3d.me/toby3d/home/internal/page/repository/fs"
pageucase "source.toby3d.me/toby3d/home/internal/page/usecase"
@ -81,10 +82,7 @@ func NewApp(ctx context.Context, config *domain.Config) (*App, error) {
themer := themeucase.NewThemeUseCase(themes)
pages := pagefsrepo.NewFileSystemPageRepository(contentDir)
pager := pageucase.NewPageUseCase(pages, resources)
server := &http.Server{
Addr: config.AddrPort().String(),
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// TODO(toby3d): use exists static use case or split that on static and resource modules?
// INFO(toby3d): any static file is public and unprotected by design, so it's safe to search it
// first before deep down to any page or it's resource which might be secured by middleware or
@ -192,7 +190,11 @@ func NewApp(ctx context.Context, config *domain.Config) (*App, error) {
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}),
})
chain := middleware.Chain{middleware.LogFmt()}
server := &http.Server{
Addr: config.AddrPort().String(),
Handler: chain.Handler(handler),
ErrorLog: logger,
BaseContext: func(ln net.Listener) context.Context { return ctx },
WriteTimeout: 500 * time.Millisecond,