Compare commits

...

3 Commits

Author SHA1 Message Date
Maxim Lebedev 5124c7cc99
🎨 Added authorization headers contants
/ docker (push) Successful in 1m53s Details
2024-02-13 06:34:29 +06:00
Maxim Lebedev ddccf36b08
📝 More comments in home cmd 2024-02-04 22:33:41 +06:00
Maxim Lebedev 4b9ce7b192
🚚 Renamed Page domain back to Entry 2024-02-04 22:33:06 +06:00
12 changed files with 73 additions and 52 deletions

View File

@ -59,9 +59,10 @@ func NewApp(logger *log.Logger, config *domain.Config) (*App, error) {
entrier := pageucase.NewEntryUseCase(entries, resources)
serverer := servercase.NewServerUseCase(sites)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 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.
// NOTE(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 protected by
// middleware or something else.
if static, err := staticer.Do(r.Context(), strings.TrimPrefix(r.URL.Path, "/")); err == nil {
http.ServeContent(w, r, static.Name(), static.ModTime(), static)
@ -70,6 +71,8 @@ func NewApp(logger *log.Logger, config *domain.Config) (*App, error) {
lang := domain.LanguageUnd
// NOTE(toby3d): read $HOME_CONTENT_DIR/index.md as a source of
// truth and global settings for any child entry.
s, err := siter.Do(r.Context(), lang)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
@ -78,8 +81,13 @@ func NewApp(logger *log.Logger, config *domain.Config) (*App, error) {
}
if s.IsMultiLingual() {
// NOTE(toby3d): $HOME_CONTENT_DIR contains at least two
// index.md with different language codes.
head, tail := urlutil.ShiftPath(r.URL.Path)
if head == "" {
// NOTE(toby3d): client request just '/', try to
// understand which language subdirectory is
// need to redirect.
supported := make([]language.Tag, len(s.Languages))
for i := range s.Languages {
supported[i] = language.Make(s.Languages[i].Lang())
@ -104,17 +112,24 @@ func NewApp(logger *log.Logger, config *domain.Config) (*App, error) {
return
}
// NOTE(toby3d): client request '/:something/...', try
// to understand which language code in subdir is
// requested.
if lang = domain.NewLanguage(head); lang != domain.LanguageUnd {
r.URL.Path = tail
}
// NOTE(toby3d): get localized site config for requested
// subdir if exists.
if s, err = siter.Do(r.Context(), lang); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
if s, err = siter.Do(r.Context(), lang); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// NOTE(toby3d): search entry for requested URL and language
// code in subdir.
e, err := entrier.Do(r.Context(), lang, r.URL.Path)
if err != nil {
if !errors.Is(err, entry.ErrNotExist) {
@ -123,6 +138,8 @@ func NewApp(logger *log.Logger, config *domain.Config) (*App, error) {
return
}
// NOTE(toby3d): maybe it is not a entry, but is't
// resource?
res, err := resourcer.Do(r.Context(), r.URL.Path)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
@ -158,6 +175,8 @@ func NewApp(logger *log.Logger, config *domain.Config) (*App, error) {
return
}
// NOTE(toby3d): wrap founded entry into theme template and
// answer to client.
contentLanguage := make([]string, len(e.Translations))
for i := range e.Translations {
contentLanguage[i] = e.Translations[i].Language.Code()

View File

@ -2,8 +2,10 @@ package common
const (
HeaderAcceptLanguage string = "Accept-Language"
HeaderAuthorization string = "Authorization"
HeaderContentLanguage string = "Content-Language"
HeaderContentType string = "Content-Type"
HeaderCookie string = "Cookie"
)
const (

View File

@ -3,5 +3,5 @@ package domain
// Context represent a all-in-one theme template context.
type Context struct {
Site *Site
*Page
Page *Entry
}

20
internal/domain/entry.go Normal file
View File

@ -0,0 +1,20 @@
package domain
type Entry struct {
Language Language
Params map[string]any
File Path
Description string
Title string
Content []byte
Resources Resources
Translations []*Entry
}
func (e Entry) IsHome() bool {
return e.File.dir == "./" && e.File.translationBaseName == "index"
}
func (e Entry) IsTranslated() bool {
return 1 < len(e.Translations)
}

View File

@ -1,20 +0,0 @@
package domain
type Page struct {
Language Language
Params map[string]any
File Path
Description string
Title string
Content []byte
Resources Resources
Translations []*Page
}
func (p Page) IsHome() bool {
return p.File.dir == "./" && p.File.translationBaseName == "index"
}
func (p Page) IsTranslated() bool {
return 1 < len(p.Translations)
}

View File

@ -8,7 +8,7 @@ import (
)
type Repository interface {
Get(ctx context.Context, lang domain.Language, path string) (*domain.Page, error)
Get(ctx context.Context, lang domain.Language, path string) (*domain.Entry, error)
// Stat checks for the existence of a page on the specified path without
// parsing its contents.

View File

@ -36,7 +36,7 @@ func NewFileSystemPageRepository(dir fs.FS) entry.Repository {
}
}
func (repo *fileSystemPageRepository) Get(ctx context.Context, lang domain.Language, p string) (*domain.Page, error) {
func (repo *fileSystemPageRepository) Get(ctx context.Context, lang domain.Language, p string) (*domain.Entry, error) {
ext := ".md"
if lang != domain.LanguageUnd {
ext = "." + lang.Lang() + ext
@ -57,7 +57,7 @@ func (repo *fileSystemPageRepository) Get(ctx context.Context, lang domain.Langu
return nil, fmt.Errorf("cannot parse entry content as FrontMatter: %w", err)
}
return &domain.Page{
return &domain.Entry{
File: domain.NewPath(target),
Language: lang,
Title: data.Title,
@ -65,7 +65,7 @@ func (repo *fileSystemPageRepository) Get(ctx context.Context, lang domain.Langu
Description: data.Description,
Params: data.Params,
Resources: make([]*domain.Resource, 0),
Translations: make([]*domain.Page, 0),
Translations: make([]*domain.Entry, 0),
}, nil
}

View File

@ -27,52 +27,52 @@ func TestGet(t *testing.T) {
repo := repository.NewFileSystemPageRepository(testData)
for name, tc := range map[string]struct {
expect *domain.Page
expect *domain.Entry
input string
}{
"index": {
input: path.Join("index"),
expect: &domain.Page{
expect: &domain.Entry{
Content: []byte("index.md"),
Params: make(map[string]any),
Resources: make([]*domain.Resource, 0),
Translations: make([]*domain.Page, 0),
Translations: make([]*domain.Entry, 0),
},
},
"file": {
input: path.Join("file"),
expect: &domain.Page{
expect: &domain.Entry{
Content: []byte("file.md"),
Params: make(map[string]any),
Resources: make([]*domain.Resource, 0),
Translations: make([]*domain.Page, 0),
Translations: make([]*domain.Entry, 0),
},
},
"folder": {
input: path.Join("folder", "index"),
expect: &domain.Page{
expect: &domain.Entry{
Content: []byte("folder/index.md"),
Params: make(map[string]any),
Resources: make([]*domain.Resource, 0),
Translations: make([]*domain.Page, 0),
Translations: make([]*domain.Entry, 0),
},
},
"both-file": {
input: path.Join("both"),
expect: &domain.Page{
expect: &domain.Entry{
Content: []byte("both.md"),
Params: make(map[string]any),
Resources: make([]*domain.Resource, 0),
Translations: make([]*domain.Page, 0),
Translations: make([]*domain.Entry, 0),
},
},
"both-folder": {
input: path.Join("both", "index"),
expect: &domain.Page{
expect: &domain.Entry{
Content: []byte("both/index.md"),
Params: make(map[string]any),
Resources: make([]*domain.Resource, 0),
Translations: make([]*domain.Page, 0),
Translations: make([]*domain.Entry, 0),
},
},
} {

View File

@ -7,5 +7,5 @@ import (
)
type UseCase interface {
Do(ctx context.Context, lang domain.Language, path string) (*domain.Page, error)
Do(ctx context.Context, lang domain.Language, path string) (*domain.Entry, error)
}

View File

@ -24,7 +24,7 @@ func NewEntryUseCase(entries entry.Repository, resources resource.Repository) en
}
}
func (ucase *entryUseCase) Do(ctx context.Context, lang domain.Language, p string) (*domain.Page, error) {
func (ucase *entryUseCase) Do(ctx context.Context, lang domain.Language, p string) (*domain.Entry, error) {
targets := make([]string, 0)
hasExt := path.Ext(p) != ""
head, tail := urlutil.ShiftPath(p)

View File

@ -11,6 +11,6 @@ type (
Writer func(w io.Writer) error
UseCase interface {
Do(ctx context.Context, site *domain.Site, page *domain.Page) (Writer, error)
Do(ctx context.Context, site *domain.Site, page *domain.Entry) (Writer, error)
}
)

View File

@ -23,16 +23,16 @@ func NewThemeUseCase(partials fs.FS, themes theme.Repository) theme.UseCase {
}
}
func (ucase *themeUseCase) Do(ctx context.Context, site *domain.Site, page *domain.Page) (theme.Writer, error) {
out, err := ucase.themes.Get(ctx, templateutil.New(ucase.partials, site))
func (ucase *themeUseCase) Do(ctx context.Context, s *domain.Site, e *domain.Entry) (theme.Writer, error) {
out, err := ucase.themes.Get(ctx, templateutil.New(ucase.partials, s))
if err != nil {
return nil, fmt.Errorf("cannot find theme: %w", err)
}
return func(w io.Writer) error {
return out.Lookup("baseof.html").Execute(w, &domain.Context{
Site: site,
Page: page,
Site: s,
Page: e,
})
}, nil
}