Compare commits
10 Commits
f23188e99e
...
555b8cc333
Author | SHA1 | Date |
---|---|---|
Maxim Lebedev | 555b8cc333 | |
Maxim Lebedev | e96380307b | |
Maxim Lebedev | f9f75f079f | |
Maxim Lebedev | a61514a6e9 | |
Maxim Lebedev | 1df6dc3fdd | |
Maxim Lebedev | e940ba43cb | |
Maxim Lebedev | 05cc536b5a | |
Maxim Lebedev | 5ad5537b3e | |
Maxim Lebedev | 345c3890bb | |
Maxim Lebedev | 766424cbf0 |
|
@ -0,0 +1,130 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
LogFmtConfig struct {
|
||||
// Skipper defines a function to skip middleware.
|
||||
Skipper Skipper
|
||||
|
||||
// Output is a writer where logs in JSON format are written.
|
||||
// Optional. Default value os.Stdout.
|
||||
Output io.Writer
|
||||
}
|
||||
|
||||
logFmtResponse struct {
|
||||
http.ResponseWriter
|
||||
error error
|
||||
start time.Time
|
||||
end time.Time
|
||||
statusCode int
|
||||
responseLength int
|
||||
id uint64
|
||||
}
|
||||
)
|
||||
|
||||
//nolint:gochecknoglobals // default configuration
|
||||
var DefaultLogFmtConfig = LogFmtConfig{
|
||||
Skipper: DefaultSkipper,
|
||||
Output: os.Stdout,
|
||||
}
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
var globalConnID uint64
|
||||
|
||||
func LogFmt() Interceptor {
|
||||
c := DefaultLogFmtConfig
|
||||
|
||||
return LogFmtWithConfig(c)
|
||||
}
|
||||
|
||||
func LogFmtWithConfig(config LogFmtConfig) Interceptor {
|
||||
if config.Skipper == nil {
|
||||
config.Skipper = DefaultLogFmtConfig.Skipper
|
||||
}
|
||||
|
||||
if config.Output == nil {
|
||||
config.Output = DefaultLogFmtConfig.Output
|
||||
}
|
||||
|
||||
encoder := slog.New(slog.NewTextHandler(config.Output, nil))
|
||||
|
||||
return func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
||||
tx := &logFmtResponse{
|
||||
id: nextConnID(),
|
||||
responseLength: 0,
|
||||
ResponseWriter: w,
|
||||
start: time.Now().UTC(),
|
||||
statusCode: 0,
|
||||
}
|
||||
|
||||
next(tx, r)
|
||||
|
||||
tx.end = time.Now().UTC()
|
||||
payload := []any{
|
||||
slog.Int64("bytes_in", r.ContentLength),
|
||||
slog.Int("bytes_out", tx.responseLength),
|
||||
slog.Any("error", tx.error),
|
||||
slog.String("host", r.Host),
|
||||
slog.Uint64("id", tx.id),
|
||||
slog.Int64("latency", tx.end.Sub(tx.start).Nanoseconds()),
|
||||
slog.String("latency_human", tx.end.Sub(tx.start).String()),
|
||||
slog.String("method", r.Method),
|
||||
slog.String("path", r.URL.Path),
|
||||
slog.String("protocol", r.Proto),
|
||||
slog.String("referer", r.Referer()),
|
||||
slog.String("remote_ip", r.RemoteAddr),
|
||||
slog.Int("status", tx.statusCode),
|
||||
slog.String("time_rfc3339", tx.start.Format(time.RFC3339)),
|
||||
slog.String("time_rfc3339_nano", tx.start.Format(time.RFC3339Nano)),
|
||||
slog.Int64("time_unix", tx.start.Unix()),
|
||||
slog.Int64("time_unix_nano", tx.start.UnixNano()),
|
||||
slog.String("uri", r.RequestURI),
|
||||
slog.String("user_agent", r.UserAgent()),
|
||||
}
|
||||
|
||||
for name, src := range map[string]map[string][]string{
|
||||
"form": r.PostForm,
|
||||
"header": r.Header,
|
||||
"query": r.URL.Query(),
|
||||
} {
|
||||
values := make([]any, 0)
|
||||
|
||||
for k, v := range src {
|
||||
values = append(values, slog.String(strings.ReplaceAll(strings.ToLower(k), " ", "_"),
|
||||
strings.Join(v, " ")))
|
||||
}
|
||||
|
||||
payload = append(payload, slog.Group(name, values...))
|
||||
}
|
||||
|
||||
encoder.Log(r.Context(), slog.LevelInfo, "" /* msg */, payload...)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *logFmtResponse) WriteHeader(status int) {
|
||||
r.statusCode = status
|
||||
|
||||
r.ResponseWriter.WriteHeader(status)
|
||||
}
|
||||
|
||||
func (r *logFmtResponse) Write(src []byte) (int, error) {
|
||||
var length int
|
||||
|
||||
length, r.error = r.ResponseWriter.Write(src)
|
||||
r.responseLength += length
|
||||
|
||||
return length, r.error
|
||||
}
|
||||
|
||||
func nextConnID() uint64 {
|
||||
return atomic.AddUint64(&globalConnID, 1)
|
||||
}
|
|
@ -7,19 +7,19 @@ import (
|
|||
|
||||
"source.toby3d.me/toby3d/home/internal/domain"
|
||||
"source.toby3d.me/toby3d/home/internal/page"
|
||||
"source.toby3d.me/toby3d/home/internal/static"
|
||||
"source.toby3d.me/toby3d/home/internal/resource"
|
||||
"source.toby3d.me/toby3d/home/internal/urlutil"
|
||||
)
|
||||
|
||||
type pageUseCase struct {
|
||||
pages page.Repository
|
||||
statics static.Repository
|
||||
pages page.Repository
|
||||
resources resource.Repository
|
||||
}
|
||||
|
||||
func NewPageUseCase(pages page.Repository, statics static.Repository) page.UseCase {
|
||||
func NewPageUseCase(pages page.Repository, resources resource.Repository) page.UseCase {
|
||||
return &pageUseCase{
|
||||
pages: pages,
|
||||
statics: statics,
|
||||
pages: pages,
|
||||
resources: resources,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,7 +47,7 @@ func (ucase *pageUseCase) Do(ctx context.Context, lang domain.Language, p string
|
|||
continue
|
||||
}
|
||||
|
||||
if out.Resources, _, err = ucase.statics.Fetch(ctx, out.File.Dir()+"*"); err != nil {
|
||||
if out.Resources, _, err = ucase.resources.Fetch(ctx, out.File.Dir()+"*"); err != nil {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
"source.toby3d.me/toby3d/home/internal/domain"
|
||||
pagefsrepo "source.toby3d.me/toby3d/home/internal/page/repository/fs"
|
||||
"source.toby3d.me/toby3d/home/internal/page/usecase"
|
||||
"source.toby3d.me/toby3d/home/internal/static"
|
||||
"source.toby3d.me/toby3d/home/internal/resource"
|
||||
)
|
||||
|
||||
func TestDo(t *testing.T) {
|
||||
|
@ -25,7 +25,7 @@ func TestDo(t *testing.T) {
|
|||
filepath.Join("index.md"): &fstest.MapFile{Data: []byte(`index.md`)},
|
||||
})
|
||||
|
||||
ucase := usecase.NewPageUseCase(pages, static.NewDummyRepository())
|
||||
ucase := usecase.NewPageUseCase(pages, resource.NewDummyRepository())
|
||||
|
||||
for name, tc := range map[string]struct {
|
||||
input string
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"source.toby3d.me/toby3d/home/internal/domain"
|
||||
)
|
||||
|
||||
type (
|
||||
Repository interface {
|
||||
// Get returns Resource on path if exists.
|
||||
Get(ctx context.Context, path string) (*domain.Resource, error)
|
||||
|
||||
// Fetch returns all resources from dir recursevly.
|
||||
Fetch(ctx context.Context, pattern string) (domain.Resources, int, error)
|
||||
}
|
||||
|
||||
dummyRepository struct{}
|
||||
)
|
||||
|
||||
func NewDummyRepository() dummyRepository {
|
||||
return dummyRepository{}
|
||||
}
|
||||
|
||||
func (dummyRepository) Get(ctx context.Context, path string) (*domain.Resource, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (dummyRepository) Fetch(ctx context.Context, pattern string) (domain.Resources, int, error) {
|
||||
return nil, 0, nil
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
_ "image/gif"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"io/fs"
|
||||
|
||||
_ "golang.org/x/image/bmp"
|
||||
_ "golang.org/x/image/webp"
|
||||
|
||||
"source.toby3d.me/toby3d/home/internal/domain"
|
||||
"source.toby3d.me/toby3d/home/internal/resource"
|
||||
)
|
||||
|
||||
type fileServerResourceRepository struct {
|
||||
root fs.FS
|
||||
}
|
||||
|
||||
func NewFileServerResourceRepository(root fs.FS) resource.Repository {
|
||||
return &fileServerResourceRepository{
|
||||
root: root,
|
||||
}
|
||||
}
|
||||
|
||||
func (repo *fileServerResourceRepository) Get(ctx context.Context, p string) (*domain.Resource, error) {
|
||||
info, err := fs.Stat(repo.root, p)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot stat resource on path '%s': %w", p, err)
|
||||
}
|
||||
|
||||
f, err := repo.root.Open(p)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot open resource on path '%s': %w", p, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
content, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot read resource content on path '%s': %w", p, err)
|
||||
}
|
||||
|
||||
return domain.NewResource(info.ModTime(), content, p), nil
|
||||
}
|
||||
|
||||
func (repo *fileServerResourceRepository) Fetch(ctx context.Context, pattern string) (domain.Resources, int, error) {
|
||||
var (
|
||||
err error
|
||||
matches []string
|
||||
)
|
||||
|
||||
if pattern != "" {
|
||||
if matches, err = fs.Glob(repo.root, pattern); err != nil {
|
||||
return nil, 0, fmt.Errorf("cannot match any resource by pattern '%s': %w", pattern, err)
|
||||
}
|
||||
} else {
|
||||
if err = fs.WalkDir(repo.root, ".", func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("catched error while walk: %w", err)
|
||||
}
|
||||
|
||||
if d.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
matches = append(matches, path)
|
||||
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, 0, fmt.Errorf("cannot walk through resource directories: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
out := make(domain.Resources, 0, len(matches))
|
||||
|
||||
for i := range matches {
|
||||
if r, err := repo.Get(ctx, matches[i]); err == nil {
|
||||
out = append(out, r)
|
||||
}
|
||||
}
|
||||
|
||||
return out, len(out), nil
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"source.toby3d.me/toby3d/home/internal/domain"
|
||||
)
|
||||
|
||||
type UseCase interface {
|
||||
Do(ctx context.Context, path string) (*domain.Resource, error)
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package usecase
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"source.toby3d.me/toby3d/home/internal/domain"
|
||||
"source.toby3d.me/toby3d/home/internal/resource"
|
||||
)
|
||||
|
||||
type resourceUseCase struct {
|
||||
resources resource.Repository
|
||||
}
|
||||
|
||||
func NewResourceUseCase(resources resource.Repository) resource.UseCase {
|
||||
return &resourceUseCase{
|
||||
resources: resources,
|
||||
}
|
||||
}
|
||||
|
||||
func (ucase *resourceUseCase) Do(ctx context.Context, p string) (*domain.Resource, error) {
|
||||
p = strings.TrimPrefix(path.Clean(p), "/")
|
||||
|
||||
r, err := ucase.resources.Get(ctx, p)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot get resource file: %w", err)
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
|
@ -5,19 +5,19 @@ import (
|
|||
"fmt"
|
||||
|
||||
"source.toby3d.me/toby3d/home/internal/domain"
|
||||
"source.toby3d.me/toby3d/home/internal/resource"
|
||||
"source.toby3d.me/toby3d/home/internal/site"
|
||||
"source.toby3d.me/toby3d/home/internal/static"
|
||||
)
|
||||
|
||||
type siteUseCase struct {
|
||||
sites site.Repository
|
||||
statics static.Repository
|
||||
sites site.Repository
|
||||
resources resource.Repository
|
||||
}
|
||||
|
||||
func NewSiteUseCase(sites site.Repository, statics static.Repository) site.UseCase {
|
||||
func NewSiteUseCase(sites site.Repository, resources resource.Repository) site.UseCase {
|
||||
return &siteUseCase{
|
||||
sites: sites,
|
||||
statics: statics,
|
||||
sites: sites,
|
||||
resources: resources,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -27,7 +27,7 @@ func (ucase *siteUseCase) Do(ctx context.Context, lang domain.Language) (*domain
|
|||
return nil, fmt.Errorf("cannot find base site data: %w", err)
|
||||
}
|
||||
|
||||
if out.Resources, _, err = ucase.statics.Fetch(ctx, ""); err == nil {
|
||||
if out.Resources, _, err = ucase.resources.Fetch(ctx, ""); err == nil {
|
||||
for _, res := range out.Resources.Match("index.*.md") {
|
||||
out.Languages = append(out.Languages, res.File.Language)
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ package usecase
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
|
@ -24,10 +23,6 @@ func NewStaticUseCase(statics static.Repository) static.UseCase {
|
|||
func (ucase *staticUseCase) Do(ctx context.Context, p string) (*domain.Resource, error) {
|
||||
p = strings.TrimPrefix(path.Clean(p), "/")
|
||||
|
||||
if ext := path.Ext(p); ext == ".html" || ext == ".md" {
|
||||
return nil, fs.ErrNotExist
|
||||
}
|
||||
|
||||
f, err := ucase.statics.Get(ctx, p)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot get static file: %w", err)
|
||||
|
|
230
main.go
230
main.go
|
@ -28,9 +28,12 @@ import (
|
|||
|
||||
"source.toby3d.me/toby3d/home/internal/common"
|
||||
"source.toby3d.me/toby3d/home/internal/domain"
|
||||
"source.toby3d.me/toby3d/home/internal/middleware"
|
||||
"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 +65,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,124 +76,125 @@ 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)
|
||||
pager := pageucase.NewPageUseCase(pages, resources)
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO(toby3d): use exists static use case or split that on static and resource modules?
|
||||
// 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 := staticer.Do(r.Context(), strings.TrimPrefix(r.URL.Path, "/"))
|
||||
if err == nil {
|
||||
http.ServeContent(w, r, static.Name(), domain.ResourceModTime(static), static)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
lang := domain.LanguageUnd
|
||||
|
||||
s, err := siter.Do(r.Context(), lang)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if s.IsMultiLingual() {
|
||||
head, tail := urlutil.ShiftPath(r.URL.Path)
|
||||
|
||||
if head == "" {
|
||||
supported := make([]language.Tag, len(s.Languages))
|
||||
for i := range s.Languages {
|
||||
supported[i] = language.Make(s.Languages[i].Lang())
|
||||
}
|
||||
|
||||
if s.DefaultLanguage != domain.LanguageUnd {
|
||||
supported = append(
|
||||
[]language.Tag{language.Make(s.DefaultLanguage.Code())},
|
||||
supported...,
|
||||
)
|
||||
}
|
||||
|
||||
requested, _, err := language.ParseAcceptLanguage(
|
||||
r.Header.Get(common.HeaderAcceptLanguage))
|
||||
if err != nil || len(requested) == 0 {
|
||||
requested = append(requested, language.English)
|
||||
}
|
||||
|
||||
matched, _, _ := language.NewMatcher(supported).Match(requested...)
|
||||
lang = domain.NewLanguage(matched.String())
|
||||
|
||||
http.Redirect(w, r, "/"+lang.Lang()+"/", http.StatusSeeOther)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
lang = domain.NewLanguage(head)
|
||||
r.URL.Path = tail
|
||||
}
|
||||
|
||||
if s, err = siter.Do(r.Context(), lang); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
p, err := pager.Do(r.Context(), lang, r.URL.Path)
|
||||
if err != nil {
|
||||
if !errors.Is(err, page.ErrNotExist) {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
res, err := resourcer.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, res.Name(), domain.ResourceModTime(res), res)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
contentLanguage := make([]string, len(p.Translations))
|
||||
for i := range p.Translations {
|
||||
contentLanguage[i] = p.Translations[i].Language.Code()
|
||||
}
|
||||
|
||||
w.Header().Set(common.HeaderContentLanguage, strings.Join(contentLanguage, ", "))
|
||||
|
||||
tpl, err := themer.Do(r.Context())
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set(common.HeaderContentType, common.MIMETextHTMLCharsetUTF8)
|
||||
if err = tpl.Execute(w, &Context{
|
||||
Site: s,
|
||||
Page: p,
|
||||
}); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
})
|
||||
chain := middleware.Chain{middleware.LogFmt()}
|
||||
server := &http.Server{
|
||||
Addr: config.AddrPort().String(),
|
||||
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO(toby3d): use exists static use case or split that on static and resource modules?
|
||||
// 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, "/"))
|
||||
if err == nil {
|
||||
http.ServeContent(w, r, static.Name(), domain.ResourceModTime(static), static)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
lang := domain.LanguageUnd
|
||||
|
||||
s, err := siter.Do(r.Context(), lang)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if s.IsMultiLingual() {
|
||||
head, tail := urlutil.ShiftPath(r.URL.Path)
|
||||
|
||||
if head == "" {
|
||||
supported := make([]language.Tag, len(s.Languages))
|
||||
for i := range s.Languages {
|
||||
supported[i] = language.Make(s.Languages[i].Lang())
|
||||
}
|
||||
|
||||
if s.DefaultLanguage != domain.LanguageUnd {
|
||||
supported = append(
|
||||
[]language.Tag{language.Make(s.DefaultLanguage.Code())},
|
||||
supported...,
|
||||
)
|
||||
}
|
||||
|
||||
requested, _, err := language.ParseAcceptLanguage(
|
||||
r.Header.Get(common.HeaderAcceptLanguage))
|
||||
if err != nil || len(requested) == 0 {
|
||||
requested = append(requested, language.English)
|
||||
}
|
||||
|
||||
matched, _, _ := language.NewMatcher(supported).Match(requested...)
|
||||
lang = domain.NewLanguage(matched.String())
|
||||
|
||||
http.Redirect(w, r, "/"+lang.Lang()+"/", http.StatusSeeOther)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
lang = domain.NewLanguage(head)
|
||||
r.URL.Path = tail
|
||||
}
|
||||
|
||||
if s, err = siter.Do(r.Context(), lang); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
p, err := pager.Do(r.Context(), lang, r.URL.Path)
|
||||
if err != nil {
|
||||
if !errors.Is(err, page.ErrNotExist) {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
res, err := resourcer.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, res.Name(), domain.ResourceModTime(res), res)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
contentLanguage := make([]string, len(p.Translations))
|
||||
for i := range p.Translations {
|
||||
contentLanguage[i] = p.Translations[i].Language.Code()
|
||||
}
|
||||
|
||||
w.Header().Set(common.HeaderContentLanguage, strings.Join(contentLanguage, ", "))
|
||||
|
||||
tpl, err := themer.Do(r.Context())
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set(common.HeaderContentType, common.MIMETextHTMLCharsetUTF8)
|
||||
if err = tpl.Execute(w, &Context{
|
||||
Site: s,
|
||||
Page: p,
|
||||
}); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}),
|
||||
Addr: config.AddrPort().String(),
|
||||
Handler: chain.Handler(handler),
|
||||
ErrorLog: logger,
|
||||
BaseContext: func(ln net.Listener) context.Context { return ctx },
|
||||
WriteTimeout: 500 * time.Millisecond,
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package main_test
|
||||
|
||||
/* TODO(toby3d): provide testdata
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
|
@ -53,15 +53,14 @@ func TestApp(t *testing.T) {
|
|||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet,
|
||||
fmt.Sprintf("http://localhost:%d/", testConfig.Port), nil)
|
||||
req, err := http.NewRequest(http.MethodGet, "http://"+testConfig.AddrPort().String()+"/", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
req.Header.Set(common.HeaderAcceptLanguage, tc.AcceptLanguage)
|
||||
|
||||
client := http.Client{Timeout: 500 * time.Millisecond}
|
||||
client := http.Client{Timeout: 1 * time.Second}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
|
@ -79,3 +78,4 @@ func TestApp(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue