🏗️ Use site module in main server

This commit is contained in:
Maxim Lebedev 2023-11-08 04:57:19 +06:00
parent 6d94eddd3d
commit c8a5e147c5
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
1 changed files with 17 additions and 3 deletions

20
main.go
View File

@ -13,7 +13,7 @@ import (
"net/http"
"os"
"os/signal"
"path"
"path/filepath"
"runtime"
"runtime/pprof"
"syscall"
@ -25,6 +25,8 @@ import (
"source.toby3d.me/toby3d/home/internal/common"
"source.toby3d.me/toby3d/home/internal/domain"
sitefsrepo "source.toby3d.me/toby3d/home/internal/site/repository/fs"
siteucase "source.toby3d.me/toby3d/home/internal/site/usecase"
"source.toby3d.me/toby3d/home/web/template"
)
@ -45,7 +47,9 @@ func init() {
logger.Fatalln("cannot unmarshal configuration into domain:", err)
}
config.ContentDir = path.Clean(config.ContentDir)
if config.ContentDir, err = filepath.Abs(filepath.Clean(config.ContentDir)); err != nil {
logger.Fatalf("cannot format '%s' content dir path into absolute path: %s", config.ContentDir, err)
}
if _, err = os.Stat(config.ContentDir); err != nil {
if errors.Is(err, os.ErrExist) {
@ -66,6 +70,9 @@ func main() {
ctx := context.Background()
matcher := language.NewMatcher(message.DefaultCatalog.Languages())
sites := sitefsrepo.NewFileSystemSiteRepository(os.DirFS(config.ContentDir))
siter := siteucase.NewSiteUseCase(sites)
server := &http.Server{
Addr: config.AddrPort().String(),
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@ -76,9 +83,16 @@ func main() {
lang, _, _ := matcher.Match(tags...)
site, err := siter.Do(r.Context(), lang)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set(common.HeaderContentType, common.MIMETextHTMLCharsetUTF8)
template.WriteTemplate(w, template.NewPage(
template.NewBaseOf(lang),
template.NewBaseOf(*site),
language.English,
[]byte(`hello, world!`),
`toby3d`,