🏗️ Use static module in main
This commit is contained in:
parent
e0b8a6efcb
commit
f3c4118286
41
main.go
41
main.go
|
@ -5,9 +5,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"io/fs"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
|
@ -25,10 +27,13 @@ import (
|
|||
|
||||
"source.toby3d.me/toby3d/home/internal/common"
|
||||
"source.toby3d.me/toby3d/home/internal/domain"
|
||||
"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"
|
||||
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"
|
||||
staticucase "source.toby3d.me/toby3d/home/internal/static/usecase"
|
||||
themefsrepo "source.toby3d.me/toby3d/home/internal/theme/repository/fs"
|
||||
themeucase "source.toby3d.me/toby3d/home/internal/theme/usecase"
|
||||
)
|
||||
|
@ -86,14 +91,17 @@ func main() {
|
|||
contentDir := os.DirFS(config.ContentDir)
|
||||
themeDir := os.DirFS(config.ThemeDir)
|
||||
|
||||
statics := staticfsrepo.NewFileServerStaticRepository(contentDir)
|
||||
staticer := staticucase.NewStaticUseCase(statics)
|
||||
|
||||
themes := themefsrepo.NewFileSystemThemeRepository(themeDir)
|
||||
themer := themeucase.NewThemeUseCase(themes)
|
||||
|
||||
sites := sitefsrepo.NewFileSystemSiteRepository(contentDir)
|
||||
siter := siteucase.NewSiteUseCase(sites)
|
||||
siter := siteucase.NewSiteUseCase(sites, statics)
|
||||
|
||||
pages := pagefsrepo.NewFileSystemPageRepository(contentDir)
|
||||
pager := pageucase.NewPageUseCase(pages)
|
||||
pager := pageucase.NewPageUseCase(pages, statics)
|
||||
|
||||
server := &http.Server{
|
||||
Addr: config.AddrPort().String(),
|
||||
|
@ -105,16 +113,35 @@ func main() {
|
|||
|
||||
lang, _, _ := matcher.Match(tags...)
|
||||
|
||||
site, err := siter.Do(r.Context(), lang)
|
||||
s, err := siter.Do(r.Context(), lang)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
page, err := pager.Do(r.Context(), lang, r.URL.Path)
|
||||
p, err := pager.Do(r.Context(), lang, r.URL.Path)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
if !errors.Is(err, page.ErrNotExist) {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
f, err := staticer.Do(r.Context(), r.URL.Path)
|
||||
if err != nil {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
http.ServeContent(w, r, f.LogicalName(), f.Updated, bytes.NewReader(f.Content))
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -128,8 +155,8 @@ func main() {
|
|||
|
||||
w.Header().Set(common.HeaderContentType, common.MIMETextHTMLCharsetUTF8)
|
||||
if err = tpl.Execute(w, &Context{
|
||||
Site: site,
|
||||
Page: page,
|
||||
Site: s,
|
||||
Page: p,
|
||||
}); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue