summaryrefslogtreecommitdiff
path: root/internal
diff refs
from: back
to: back
| flip
diff options
context:
space:
mode:
Diffstat (limited to 'internal')
-rw-r--r--internal/blog/index.go32
-rw-r--r--internal/blog/s3.go173
-rw-r--r--internal/router/handlers/root.go3
-rw-r--r--internal/router/router.go36
4 files changed, 28 insertions, 216 deletions
diff --git a/internal/blog/index.go b/internal/blog/index.go
deleted file mode 100644
index 861a90e..0000000
--- a/internal/blog/index.go
+++ /dev/null
@@ -1,32 +0,0 @@
-package blog
-
-import "time"
-
-const IndexFileName = "index.json"
-
-const IndexSchemaVersion = 1
-
-type IndexEntry struct {
- Link string `json:"link"`
- ModifiedTime time.Time `json:"modifiedTime"`
- Title string `json:"title"`
- ShortDescription string `json:"shortDescription"`
- ActionDate string `json:"actionDate"`
- PublishedTime time.Time `json:"publishedTime"`
- Thumbnail string `json:"thumbnail"`
- Tags []string `json:"tags"`
- Geolocation string `json:"geolocation"`
- Medley string `json:"medley,omitempty"`
- MedleyPart int `json:"medleyPart,omitempty"`
-}
-
-type IndexCategory struct {
- GeneratedAt time.Time `json:"generatedAt"`
- Pages []IndexEntry `json:"pages"`
-}
-
-type Index struct {
- SchemaVersion int `json:"schemaVersion"`
- GeneratedAt time.Time `json:"generatedAt"`
- Categories map[string]IndexCategory `json:"categories"`
-}
diff --git a/internal/blog/s3.go b/internal/blog/s3.go
index bbd5238..86a6463 100644
--- a/internal/blog/s3.go
+++ b/internal/blog/s3.go
@@ -2,15 +2,10 @@ package blog
import (
"context"
- "encoding/json"
- "errors"
"fmt"
"io"
- "log/slog"
- "net/url"
"slices"
"strings"
- "sync"
"time"
"github.com/SayaAndy/saya-today-web/config"
@@ -19,11 +14,8 @@ import (
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
- s3types "github.com/aws/aws-sdk-go-v2/service/s3/types"
)
-const s3ScanConcurrency = 32
-
type S3Client struct {
prefix string
bucketName string
@@ -58,7 +50,6 @@ func NewS3Client(cfg *config.StorageConfig) (Client, error) {
}
s3Opts = append(s3Opts, func(o *s3.Options) {
o.UsePathStyle = s3cfg.UsePathStyle
- o.DisableLogOutputChecksumValidationSkipped = true
})
s3cl := s3.NewFromConfig(awsCfg, s3Opts...)
@@ -67,195 +58,79 @@ func NewS3Client(cfg *config.StorageConfig) (Client, error) {
}
func (c *S3Client) Scan(prefix string) ([]*Page, error) {
- pages, err := c.scanFromIndex(prefix)
- if err == nil {
- return pages, nil
- }
-
- var nsk *s3types.NoSuchKey
- if !errors.As(err, &nsk) {
- return nil, err
- }
-
- slog.Warn("index.json missing, falling back to listing", slog.String("prefix", c.prefix))
- return c.scanByListing(prefix)
-}
-
-func (c *S3Client) scanFromIndex(prefix string) ([]*Page, error) {
- out, err := c.s3cl.GetObject(context.Background(), &s3.GetObjectInput{
- Bucket: aws.String(c.bucketName),
- Key: aws.String(IndexFileName),
- })
- if err != nil {
- return nil, fmt.Errorf("get index.json: %w", err)
- }
- defer out.Body.Close()
-
- raw, err := io.ReadAll(out.Body)
- if err != nil {
- return nil, fmt.Errorf("read index.json: %w", err)
- }
-
- var idx Index
- if err := json.Unmarshal(raw, &idx); err != nil {
- return nil, fmt.Errorf("unmarshal index.json: %w", err)
- }
-
- wantLang := ""
- if i := strings.Index(prefix, "/"); i > 0 {
- wantLang = prefix[:i]
- }
+ pages := []*Page{}
fullPrefix := c.prefix + prefix
- pages := make([]*Page, 0)
- for catKey, cat := range idx.Categories {
- lang, ok := strings.CutPrefix(catKey, c.prefix)
- if !ok {
- continue
- }
- if wantLang != "" && wantLang != lang {
- continue
- }
- for _, e := range cat.Pages {
- if !strings.HasPrefix(e.Link, fullPrefix) {
- continue
- }
- linkParts := strings.Split(e.Link, "/")
- nameParts := strings.Split(linkParts[len(linkParts)-1], ".")
- fileName := strings.Join(nameParts[:len(nameParts)-1], ".")
- pages = append(pages, &Page{
- Link: e.Link,
- FileName: fileName,
- Lang: lang,
- ModifiedTime: e.ModifiedTime,
- Metadata: &frontmatter.Metadata{
- Title: e.Title,
- ShortDescription: e.ShortDescription,
- ActionDate: e.ActionDate,
- PublishedTime: e.PublishedTime,
- Thumbnail: e.Thumbnail,
- Tags: e.Tags,
- Geolocation: e.Geolocation,
- Medley: e.Medley,
- MedleyPart: e.MedleyPart,
- },
- })
- }
+ input := &s3.ListObjectsV2Input{
+ Bucket: aws.String(c.bucketName),
+ Prefix: aws.String(fullPrefix),
}
- return pages, nil
-}
-func (c *S3Client) scanByListing(prefix string) ([]*Page, error) {
- fullPrefix := c.prefix + prefix
-
- type candidate struct {
- key string
- lastModified time.Time
- }
- var candidates []candidate
+ paginator := s3.NewListObjectsV2Paginator(c.s3cl, input)
- paginator := s3.NewListObjectsV2Paginator(c.s3cl, &s3.ListObjectsV2Input{
- Bucket: aws.String(c.bucketName),
- Prefix: aws.String(fullPrefix),
- })
for paginator.HasMorePages() {
output, err := paginator.NextPage(context.Background())
if err != nil {
return nil, fmt.Errorf("list S3 objects: %w", err)
}
+
for _, obj := range output.Contents {
key := aws.ToString(obj.Key)
+
if !strings.HasSuffix(key, ".md") {
continue
}
- candidates = append(candidates, candidate{key, aws.ToTime(obj.LastModified)})
- }
- }
-
- pages := make([]*Page, len(candidates))
- sem := make(chan struct{}, s3ScanConcurrency)
- var wg sync.WaitGroup
- var firstErr error
- var errMu sync.Mutex
-
- for i, cand := range candidates {
- sem <- struct{}{}
- wg.Go(func() {
- defer func() { <-sem }()
head, err := c.s3cl.HeadObject(context.Background(), &s3.HeadObjectInput{
Bucket: aws.String(c.bucketName),
- Key: aws.String(cand.key),
+ Key: aws.String(key),
})
if err != nil {
- errMu.Lock()
- if firstErr == nil {
- firstErr = fmt.Errorf("head S3 object %s: %w", cand.key, err)
- }
- errMu.Unlock()
- return
+ return nil, fmt.Errorf("head S3 object %s: %w", key, err)
}
if head.ContentType == nil || !strings.Contains(*head.ContentType, "text/markdown") {
- return
+ continue
}
+
meta := head.Metadata
if meta["title"] == "" {
- return
+ continue
}
publishedTime, err := time.Parse(time.RFC3339, meta["published-time"])
if err != nil {
- errMu.Lock()
- if firstErr == nil {
- firstErr = fmt.Errorf("failed to parse published time metadata field: %w", err)
- }
- errMu.Unlock()
- return
+ return nil, fmt.Errorf("failed to parse published time metadata field: %w", err)
}
- linkParts := strings.Split(cand.key, "/")
+ linkParts := strings.Split(key, "/")
nameParts := strings.Split(linkParts[len(linkParts)-1], ".")
fileName := strings.Join(nameParts[:len(nameParts)-1], ".")
- lang, _ := strings.CutPrefix(linkParts[0], c.prefix)
tags := strings.Split(meta["tags"], ",")
slices.Sort(tags)
- title, _ := url.QueryUnescape(meta["title"])
- shortDescription, _ := url.QueryUnescape(meta["short-description"])
- thumbnail, _ := url.QueryUnescape(meta["thumbnail"])
+ lang, _ := strings.CutPrefix(linkParts[0], c.prefix)
- pages[i] = &Page{
- Link: cand.key,
+ pages = append(pages, &Page{
+ Link: key,
FileName: fileName,
Lang: lang,
- ModifiedTime: cand.lastModified,
+ ModifiedTime: aws.ToTime(obj.LastModified),
Metadata: &frontmatter.Metadata{
- Title: title,
- ShortDescription: shortDescription,
+ Title: meta["title"],
+ ShortDescription: meta["short-description"],
ActionDate: meta["action-date"],
PublishedTime: publishedTime,
- Thumbnail: thumbnail,
+ Thumbnail: meta["thumbnail"],
Tags: tags,
Geolocation: meta["geolocation"],
},
- }
- })
- }
- wg.Wait()
-
- if firstErr != nil {
- return nil, firstErr
- }
-
- out := pages[:0]
- for _, p := range pages {
- if p != nil {
- out = append(out, p)
+ })
}
}
- return out, nil
+
+ return pages, nil
}
func (c *S3Client) ReadAll(path string) ([]byte, error) {
diff --git a/internal/router/handlers/root.go b/internal/router/handlers/root.go
index 6a011e4..a8890fa 100644
--- a/internal/router/handlers/root.go
+++ b/internal/router/handlers/root.go
@@ -50,9 +50,6 @@ func (r *RootHandler) AddMeta(c *fiber.Ctx, supplements *router.Supplements, lan
if supplements.Meta.GoogleSiteVerification != "" {
meta = append(meta, router.MetaField{Name: "google-site-verification", Content: supplements.Meta.GoogleSiteVerification})
}
- if supplements.Meta.YandexVerification != "" {
- meta = append(meta, router.MetaField{Name: "yandex-verification", Content: supplements.Meta.YandexVerification})
- }
return meta, nil
}
diff --git a/internal/router/router.go b/internal/router/router.go
index 65e35a4..5230e74 100644
--- a/internal/router/router.go
+++ b/internal/router/router.go
@@ -7,11 +7,8 @@ import (
"fmt"
"html/template"
"log/slog"
- "net"
"net/url"
- "os"
"slices"
- "strconv"
"strings"
"time"
@@ -115,7 +112,6 @@ type Router struct {
templatedRoutes map[string]map[string]Route
templatedPathMatcher *PathMatcher
canonicalEndpoint string
- endpoint config.EndpointConfig
}
func NewRouter(cfg *config.Config) (*Router, error) {
@@ -260,7 +256,7 @@ func NewRouter(cfg *config.Config) (*Router, error) {
templatedRoutes := make(map[string]map[string]Route)
templatedPathMatcher := NewPathMatcher()
- return &Router{supplements, app, templatedRoutes, templatedPathMatcher, cfg.CanonicalEndpoint, cfg.Endpoint}, nil
+ return &Router{supplements, app, templatedRoutes, templatedPathMatcher, cfg.CanonicalEndpoint}, nil
}
func (r *Router) InitRoutes() (err error) {
@@ -309,8 +305,6 @@ func (r *Router) InitRoutes() (err error) {
cacheKey = fmt.Sprintf("%s.full-page.%s", method, trimmedPath)
case ByUrlAndQuery:
cacheKey = fmt.Sprintf("%s.full-page.%s.%s", method, trimmedPath, queryString)
- case Disabled:
- c.Set("Cache-Control", "no-store, no-cache, must-revalidate")
}
defaultMap := fiber.Map{
@@ -373,7 +367,6 @@ func (r *Router) InitRoutes() (err error) {
c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8)
return c.Status(fiber.ErrBadRequest.Code).SendString(fmt.Sprintf("unknown segment '%s'", part))
}
- c.Set("Cache-Control", "no-store, no-cache, must-revalidate")
err := r.generalPageSegment(c, part)
return err
})
@@ -389,30 +382,10 @@ func (r *Router) InitRoutes() (err error) {
return nil
}
-func (r *Router) Listen() error {
- switch r.endpoint.Type {
- case "unix":
- unixConfig := r.endpoint.Config.(*config.UnixConfig)
- endpoint, _ := strings.CutPrefix(unixConfig.Path, "unix://")
- ln, err := net.Listen("unix", endpoint)
- if err != nil {
- return fmt.Errorf("error while initializing unix listener: %w", err)
- }
- chmod, _ := strconv.ParseUint(unixConfig.Chmod[1:], 8, 32)
- os.Chmod(unixConfig.Path, os.FileMode(chmod))
- if err := r.app.Listener(ln); err != nil {
- return fmt.Errorf("error while running fiber server: %w", err)
- }
- case "http":
- httpConfig := r.endpoint.Config.(*config.HttpConfig)
- fmt.Print(httpConfig)
- if err := r.app.Listen(httpConfig.ListenOn); err != nil {
- return fmt.Errorf("error while running fiber server: %w", err)
- }
- default:
- return fmt.Errorf("error with initializing fiber server: invalid endpoint type (supported are unix and http)")
+func (r *Router) Listen(endpoint string) error {
+ if err := r.app.Listen(endpoint); err != nil {
+ return fmt.Errorf("error while running fiber server: %w", err)
}
-
return nil
}
@@ -436,7 +409,6 @@ func (r *Router) Close() (err error) {
}
slog.Debug("closing page cache")
r.supplements.PageCache.Close()
-
return errors.Join(allErrors...)
}