Compare commits
3 Commits
6aae1ffa48
...
5124c7cc99
Author | SHA1 | Date |
---|---|---|
Maxim Lebedev | 5124c7cc99 | |
Maxim Lebedev | ddccf36b08 | |
Maxim Lebedev | 4b9ce7b192 |
|
@ -59,9 +59,10 @@ func NewApp(logger *log.Logger, config *domain.Config) (*App, error) {
|
||||||
entrier := pageucase.NewEntryUseCase(entries, resources)
|
entrier := pageucase.NewEntryUseCase(entries, resources)
|
||||||
serverer := servercase.NewServerUseCase(sites)
|
serverer := servercase.NewServerUseCase(sites)
|
||||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
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
|
// NOTE(toby3d): any static file is public and unprotected by
|
||||||
// first before deep down to any page or it's resource which might be secured by middleware or
|
// design, so it's safe to search it first before deep down to
|
||||||
// something else.
|
// 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 {
|
if static, err := staticer.Do(r.Context(), strings.TrimPrefix(r.URL.Path, "/")); err == nil {
|
||||||
http.ServeContent(w, r, static.Name(), static.ModTime(), static)
|
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
|
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)
|
s, err := siter.Do(r.Context(), lang)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
@ -78,8 +81,13 @@ func NewApp(logger *log.Logger, config *domain.Config) (*App, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.IsMultiLingual() {
|
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)
|
head, tail := urlutil.ShiftPath(r.URL.Path)
|
||||||
if head == "" {
|
if head == "" {
|
||||||
|
// NOTE(toby3d): client request just '/', try to
|
||||||
|
// understand which language subdirectory is
|
||||||
|
// need to redirect.
|
||||||
supported := make([]language.Tag, len(s.Languages))
|
supported := make([]language.Tag, len(s.Languages))
|
||||||
for i := range s.Languages {
|
for i := range s.Languages {
|
||||||
supported[i] = language.Make(s.Languages[i].Lang())
|
supported[i] = language.Make(s.Languages[i].Lang())
|
||||||
|
@ -104,17 +112,24 @@ func NewApp(logger *log.Logger, config *domain.Config) (*App, error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTE(toby3d): client request '/:something/...', try
|
||||||
|
// to understand which language code in subdir is
|
||||||
|
// requested.
|
||||||
if lang = domain.NewLanguage(head); lang != domain.LanguageUnd {
|
if lang = domain.NewLanguage(head); lang != domain.LanguageUnd {
|
||||||
r.URL.Path = tail
|
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 {
|
// NOTE(toby3d): search entry for requested URL and language
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
// code in subdir.
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
e, err := entrier.Do(r.Context(), lang, r.URL.Path)
|
e, err := entrier.Do(r.Context(), lang, r.URL.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !errors.Is(err, entry.ErrNotExist) {
|
if !errors.Is(err, entry.ErrNotExist) {
|
||||||
|
@ -123,6 +138,8 @@ func NewApp(logger *log.Logger, config *domain.Config) (*App, error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTE(toby3d): maybe it is not a entry, but is't
|
||||||
|
// resource?
|
||||||
res, err := resourcer.Do(r.Context(), r.URL.Path)
|
res, err := resourcer.Do(r.Context(), r.URL.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, fs.ErrNotExist) {
|
if errors.Is(err, fs.ErrNotExist) {
|
||||||
|
@ -158,6 +175,8 @@ func NewApp(logger *log.Logger, config *domain.Config) (*App, error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTE(toby3d): wrap founded entry into theme template and
|
||||||
|
// answer to client.
|
||||||
contentLanguage := make([]string, len(e.Translations))
|
contentLanguage := make([]string, len(e.Translations))
|
||||||
for i := range e.Translations {
|
for i := range e.Translations {
|
||||||
contentLanguage[i] = e.Translations[i].Language.Code()
|
contentLanguage[i] = e.Translations[i].Language.Code()
|
||||||
|
|
|
@ -2,8 +2,10 @@ package common
|
||||||
|
|
||||||
const (
|
const (
|
||||||
HeaderAcceptLanguage string = "Accept-Language"
|
HeaderAcceptLanguage string = "Accept-Language"
|
||||||
|
HeaderAuthorization string = "Authorization"
|
||||||
HeaderContentLanguage string = "Content-Language"
|
HeaderContentLanguage string = "Content-Language"
|
||||||
HeaderContentType string = "Content-Type"
|
HeaderContentType string = "Content-Type"
|
||||||
|
HeaderCookie string = "Cookie"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -3,5 +3,5 @@ package domain
|
||||||
// Context represent a all-in-one theme template context.
|
// Context represent a all-in-one theme template context.
|
||||||
type Context struct {
|
type Context struct {
|
||||||
Site *Site
|
Site *Site
|
||||||
*Page
|
Page *Entry
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
|
@ -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)
|
|
||||||
}
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Repository interface {
|
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
|
// Stat checks for the existence of a page on the specified path without
|
||||||
// parsing its contents.
|
// parsing its contents.
|
||||||
|
|
|
@ -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"
|
ext := ".md"
|
||||||
if lang != domain.LanguageUnd {
|
if lang != domain.LanguageUnd {
|
||||||
ext = "." + lang.Lang() + ext
|
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 nil, fmt.Errorf("cannot parse entry content as FrontMatter: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &domain.Page{
|
return &domain.Entry{
|
||||||
File: domain.NewPath(target),
|
File: domain.NewPath(target),
|
||||||
Language: lang,
|
Language: lang,
|
||||||
Title: data.Title,
|
Title: data.Title,
|
||||||
|
@ -65,7 +65,7 @@ func (repo *fileSystemPageRepository) Get(ctx context.Context, lang domain.Langu
|
||||||
Description: data.Description,
|
Description: data.Description,
|
||||||
Params: data.Params,
|
Params: data.Params,
|
||||||
Resources: make([]*domain.Resource, 0),
|
Resources: make([]*domain.Resource, 0),
|
||||||
Translations: make([]*domain.Page, 0),
|
Translations: make([]*domain.Entry, 0),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,52 +27,52 @@ func TestGet(t *testing.T) {
|
||||||
repo := repository.NewFileSystemPageRepository(testData)
|
repo := repository.NewFileSystemPageRepository(testData)
|
||||||
|
|
||||||
for name, tc := range map[string]struct {
|
for name, tc := range map[string]struct {
|
||||||
expect *domain.Page
|
expect *domain.Entry
|
||||||
input string
|
input string
|
||||||
}{
|
}{
|
||||||
"index": {
|
"index": {
|
||||||
input: path.Join("index"),
|
input: path.Join("index"),
|
||||||
expect: &domain.Page{
|
expect: &domain.Entry{
|
||||||
Content: []byte("index.md"),
|
Content: []byte("index.md"),
|
||||||
Params: make(map[string]any),
|
Params: make(map[string]any),
|
||||||
Resources: make([]*domain.Resource, 0),
|
Resources: make([]*domain.Resource, 0),
|
||||||
Translations: make([]*domain.Page, 0),
|
Translations: make([]*domain.Entry, 0),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"file": {
|
"file": {
|
||||||
input: path.Join("file"),
|
input: path.Join("file"),
|
||||||
expect: &domain.Page{
|
expect: &domain.Entry{
|
||||||
Content: []byte("file.md"),
|
Content: []byte("file.md"),
|
||||||
Params: make(map[string]any),
|
Params: make(map[string]any),
|
||||||
Resources: make([]*domain.Resource, 0),
|
Resources: make([]*domain.Resource, 0),
|
||||||
Translations: make([]*domain.Page, 0),
|
Translations: make([]*domain.Entry, 0),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"folder": {
|
"folder": {
|
||||||
input: path.Join("folder", "index"),
|
input: path.Join("folder", "index"),
|
||||||
expect: &domain.Page{
|
expect: &domain.Entry{
|
||||||
Content: []byte("folder/index.md"),
|
Content: []byte("folder/index.md"),
|
||||||
Params: make(map[string]any),
|
Params: make(map[string]any),
|
||||||
Resources: make([]*domain.Resource, 0),
|
Resources: make([]*domain.Resource, 0),
|
||||||
Translations: make([]*domain.Page, 0),
|
Translations: make([]*domain.Entry, 0),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"both-file": {
|
"both-file": {
|
||||||
input: path.Join("both"),
|
input: path.Join("both"),
|
||||||
expect: &domain.Page{
|
expect: &domain.Entry{
|
||||||
Content: []byte("both.md"),
|
Content: []byte("both.md"),
|
||||||
Params: make(map[string]any),
|
Params: make(map[string]any),
|
||||||
Resources: make([]*domain.Resource, 0),
|
Resources: make([]*domain.Resource, 0),
|
||||||
Translations: make([]*domain.Page, 0),
|
Translations: make([]*domain.Entry, 0),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"both-folder": {
|
"both-folder": {
|
||||||
input: path.Join("both", "index"),
|
input: path.Join("both", "index"),
|
||||||
expect: &domain.Page{
|
expect: &domain.Entry{
|
||||||
Content: []byte("both/index.md"),
|
Content: []byte("both/index.md"),
|
||||||
Params: make(map[string]any),
|
Params: make(map[string]any),
|
||||||
Resources: make([]*domain.Resource, 0),
|
Resources: make([]*domain.Resource, 0),
|
||||||
Translations: make([]*domain.Page, 0),
|
Translations: make([]*domain.Entry, 0),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
|
|
|
@ -7,5 +7,5 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type UseCase interface {
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
targets := make([]string, 0)
|
||||||
hasExt := path.Ext(p) != ""
|
hasExt := path.Ext(p) != ""
|
||||||
head, tail := urlutil.ShiftPath(p)
|
head, tail := urlutil.ShiftPath(p)
|
||||||
|
|
|
@ -11,6 +11,6 @@ type (
|
||||||
Writer func(w io.Writer) error
|
Writer func(w io.Writer) error
|
||||||
|
|
||||||
UseCase interface {
|
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)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
@ -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) {
|
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, site))
|
out, err := ucase.themes.Get(ctx, templateutil.New(ucase.partials, s))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot find theme: %w", err)
|
return nil, fmt.Errorf("cannot find theme: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return func(w io.Writer) error {
|
return func(w io.Writer) error {
|
||||||
return out.Lookup("baseof.html").Execute(w, &domain.Context{
|
return out.Lookup("baseof.html").Execute(w, &domain.Context{
|
||||||
Site: site,
|
Site: s,
|
||||||
Page: page,
|
Page: e,
|
||||||
})
|
})
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue