enable custom headers for feeds (allow fetch from remote sites)
7 files changed, 37 insertions(+), 2 deletions(-)
M internal/builder/builder.go → internal/builder/builder.go
@@ -24,6 +24,12 @@ "github.com/Southclaws/fault/fmsg" mapset "github.com/deckarep/golang-set/v2" ) +var feedHeaders = map[string]string{ + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Methods": "GET, OPTIONS", + "Access-Control-Max-Age": "3600", +} + type Options struct { Source string `conf:"default:.,short:s,flag:src"` Development bool `conf:"default:false,flag:dev"`@@ -156,6 +162,7 @@ file := &storage.File{ Title: title, LastModified: matchingPosts[0].Date, ContentType: "application/xml", + Headers: feedHeaders, Path: path.Join("/tags", tag, "atom.xml"), FSPath: path.Join("/tags", tag, "atom.xml"), }@@ -189,6 +196,7 @@ file := &storage.File{ Title: config.Title, LastModified: cc.Posts[0].Date, ContentType: "application/xml", + Headers: feedHeaders, Path: "/atom.xml", FSPath: "/atom.xml", }
M internal/storage/file.go → internal/storage/file.go
@@ -19,6 +19,7 @@ LastModified time.Time Etag string Title string StyleHash string + Headers map[string]string Encodings map[string]*buffer.Buffer }
M internal/storage/sqlite/reader.go → internal/storage/sqlite/reader.go
@@ -3,6 +3,7 @@ import ( "context" "database/sql" + "encoding/json" "strings" "time"@@ -47,6 +48,14 @@ file.LastModified = time.Unix(row.File.LastModified, 0) file.Etag = row.File.Etag file.Title = row.File.Title file.StyleHash = row.File.StyleHash + + if len(row.File.Headers) > 2 { + err := json.Unmarshal(row.File.Headers, &file.Headers) + if err != nil { + return nil, fault.Wrap(err, fmsg.With("unmarshalling headers")) + } + } + file.Encodings[row.Content.Encoding] = buffer.NewBuffer(row.Content.Body) } if count == 0 {
M internal/storage/sqlite/writer.go → internal/storage/sqlite/writer.go
@@ -3,6 +3,7 @@ import ( "context" "database/sql" + "encoding/json" "fmt" "hash/fnv" "io"@@ -106,6 +107,15 @@ LastModified: file.LastModified.Unix(), Etag: file.Etag, StyleHash: file.StyleHash, Title: file.Title, + Headers: []byte{}, + } + if file.Headers != nil { + jso, err := json.Marshal(file.Headers) + if err != nil { + return 0, fault.Wrap(err, fmsg.With("marshalling headers to JSON")) + } + params.Headers = jso + s.log.Info("storing file", "file", file.Path, "headers", params.Headers) } id, err := s.queries.InsertFile(context.TODO(), params) if err != nil {
M internal/website/mux.go → internal/website/mux.go
@@ -90,6 +90,11 @@ if enc != "" { w.Header().Add("Content-Encoding", enc) } w.Header().Add("Content-Type", file.ContentType) + if file.Headers != nil { + for k, v := range file.Headers { + w.Header().Add(k, v) + } + } http.ServeContent(w, r, file.Path, file.LastModified, file.Encodings[enc]) return nil
M query.sql → query.sql
@@ -2,14 +2,15 @@ -- name: InsertURL :execlastid INSERT INTO url (path) VALUES (?); -- name: InsertFile :execlastid -INSERT INTO file (url_id, content_type, last_modified, etag, style_hash, title) +INSERT INTO file (url_id, content_type, last_modified, etag, style_hash, title, headers) VALUES ( @url_id, @content_type, @last_modified, @etag, @style_hash, - @title + @title, + @headers ); -- name: InsertContent :exec
M schema.sql → schema.sql
@@ -13,6 +13,7 @@ last_modified INTEGER NOT NULL, title TEXT NOT NULL, etag TEXT NOT NULL, style_hash TEXT NOT NULL, + headers BLOB NOT NULL, FOREIGN KEY (url_id) REFERENCES url (url_id) ) STRICT; CREATE UNIQUE INDEX IF NOT EXISTS file_url_content_type