🏗️ Refactored static and resources usage in main

This commit is contained in:
Maxim Lebedev 2023-11-18 19:03:04 +06:00
parent 05cc536b5a
commit e940ba43cb
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
1 changed files with 6 additions and 4 deletions

10
main.go
View File

@ -31,6 +31,8 @@ import (
"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"
resourcefsrepo "source.toby3d.me/toby3d/home/internal/resource/repository/fs"
resourceucase "source.toby3d.me/toby3d/home/internal/resource/usecase"
sitefsrepo "source.toby3d.me/toby3d/home/internal/site/repository/fs"
siteucase "source.toby3d.me/toby3d/home/internal/site/usecase"
staticfsrepo "source.toby3d.me/toby3d/home/internal/static/repository/fs"
@ -62,7 +64,7 @@ var cpuProfilePath, memProfilePath string
func NewApp(ctx context.Context, config *domain.Config) (*App, error) {
themeDir := os.DirFS(config.ThemeDir)
contentDir := os.DirFS(config.ContentDir)
resources := staticfsrepo.NewFileServerStaticRepository(contentDir)
resources := resourcefsrepo.NewFileServerResourceRepository(contentDir)
sites := sitefsrepo.NewFileSystemSiteRepository(contentDir)
siter := siteucase.NewSiteUseCase(sites, resources)
@ -73,8 +75,8 @@ func NewApp(ctx context.Context, config *domain.Config) (*App, error) {
staticDir := os.DirFS(config.StaticDir)
statics := staticfsrepo.NewFileServerStaticRepository(staticDir)
// TODO(toby3d): use exists static use case or split that on static and resource modules?
resourcer := staticucase.NewStaticUseCase(resources)
staticer := staticucase.NewStaticUseCase(statics)
resourcer := resourceucase.NewResourceUseCase(resources)
themes := themefsrepo.NewFileSystemThemeRepository(themeDir, funcMap)
themer := themeucase.NewThemeUseCase(themes)
pages := pagefsrepo.NewFileSystemPageRepository(contentDir)
@ -87,7 +89,7 @@ func NewApp(ctx context.Context, config *domain.Config) (*App, error) {
// 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
// something else.
static, err := statics.Get(r.Context(), strings.TrimPrefix(r.URL.Path, "/"))
static, err := staticer.Do(r.Context(), strings.TrimPrefix(r.URL.Path, "/"))
if err == nil {
http.ServeContent(w, r, static.Name(), domain.ResourceModTime(static), static)