From cec16e71c40cd39da8123fe36aab527531233dc0 Mon Sep 17 00:00:00 2001 From: Alex Russell-Saw Date: Sat, 14 Nov 2020 22:06:54 +0000 Subject: [PATCH] refactor article generation to use async queues, add new poll endpoint --- dao/dao.go | 66 +- domain/edition.go | 56 +- domain/feeds.go | 168 - domain/publisher.go | 78 + domain/source.go | 32 +- go.mod | 6 +- go.sum | 12 + handler/handle_article.go | 30 +- handler/handle_generate_edition.go | 49 +- handler/handle_news.go | 8 +- handler/handle_poll.go | 20 + handler/pubsub_article.go | 127 + handler/pubsub_source.go | 63 + handler/router.go | 33 +- idgen/idgen.go | 30 + main.go | 27 +- pkg/util/slog.go | 27 + static/darkmode.js | 2 +- static/main.css | 47 +- static/turbolinks.js | 6 + tmpl/article-tile.html | 67 +- tmpl/article.html | 47 +- tmpl/frame.html | 122 + tmpl/frontpage-1.html | 235 + tmpl/index.html | 351 -- vendor/cloud.google.com/go/iam/iam.go | 387 ++ vendor/cloud.google.com/go/pubsub/CHANGES.md | 36 + vendor/cloud.google.com/go/pubsub/LICENSE | 202 + vendor/cloud.google.com/go/pubsub/README.md | 46 + .../cloud.google.com/go/pubsub/apiv1/doc.go | 100 + .../cloud.google.com/go/pubsub/apiv1/iam.go | 36 + .../go/pubsub/apiv1/path_funcs.go | 95 + .../go/pubsub/apiv1/publisher_client.go | 534 +++ .../go/pubsub/apiv1/subscriber_client.go | 808 ++++ vendor/cloud.google.com/go/pubsub/debug.go | 72 + vendor/cloud.google.com/go/pubsub/doc.go | 140 + .../go/pubsub/flow_controller.go | 122 + vendor/cloud.google.com/go/pubsub/go.mod | 19 + vendor/cloud.google.com/go/pubsub/go.sum | 373 ++ .../go/pubsub/go_mod_tidy_hack.go | 22 + .../internal/distribution/distribution.go | 79 + vendor/cloud.google.com/go/pubsub/iterator.go | 533 +++ vendor/cloud.google.com/go/pubsub/message.go | 120 + vendor/cloud.google.com/go/pubsub/nodebug.go | 25 + vendor/cloud.google.com/go/pubsub/pubsub.go | 112 + .../cloud.google.com/go/pubsub/pullstream.go | 189 + vendor/cloud.google.com/go/pubsub/service.go | 100 + vendor/cloud.google.com/go/pubsub/snapshot.go | 160 + .../go/pubsub/subscription.go | 801 ++++ vendor/cloud.google.com/go/pubsub/topic.go | 558 +++ vendor/cloud.google.com/go/pubsub/trace.go | 217 + .../github.com/bwmarrin/snowflake/.travis.yml | 12 + vendor/github.com/bwmarrin/snowflake/LICENSE | 23 + .../github.com/bwmarrin/snowflake/README.md | 143 + .../bwmarrin/snowflake/snowflake.go | 365 ++ vendor/github.com/fatih/color/LICENSE.md | 20 + vendor/github.com/fatih/color/README.md | 173 + vendor/github.com/fatih/color/color.go | 603 +++ vendor/github.com/fatih/color/doc.go | 133 + vendor/github.com/fatih/color/go.mod | 8 + vendor/github.com/fatih/color/go.sum | 7 + .../github.com/mattn/go-colorable/.travis.yml | 15 + vendor/github.com/mattn/go-colorable/LICENSE | 21 + .../github.com/mattn/go-colorable/README.md | 48 + .../mattn/go-colorable/colorable_appengine.go | 37 + .../mattn/go-colorable/colorable_others.go | 38 + .../mattn/go-colorable/colorable_windows.go | 1043 +++++ vendor/github.com/mattn/go-colorable/go.mod | 8 + vendor/github.com/mattn/go-colorable/go.sum | 5 + .../github.com/mattn/go-colorable/go.test.sh | 12 + .../mattn/go-colorable/noncolorable.go | 55 + vendor/github.com/mattn/go-isatty/.travis.yml | 14 + vendor/github.com/mattn/go-isatty/LICENSE | 9 + vendor/github.com/mattn/go-isatty/README.md | 50 + vendor/github.com/mattn/go-isatty/doc.go | 2 + vendor/github.com/mattn/go-isatty/go.mod | 5 + vendor/github.com/mattn/go-isatty/go.sum | 2 + vendor/github.com/mattn/go-isatty/go.test.sh | 12 + .../github.com/mattn/go-isatty/isatty_bsd.go | 18 + .../mattn/go-isatty/isatty_others.go | 15 + .../mattn/go-isatty/isatty_plan9.go | 22 + .../mattn/go-isatty/isatty_solaris.go | 22 + .../mattn/go-isatty/isatty_tcgets.go | 18 + .../mattn/go-isatty/isatty_windows.go | 125 + .../github.com/mattn/go-isatty/renovate.json | 8 + .../golang.org/x/sync/semaphore/semaphore.go | 136 + .../api/support/bundler/bundler.go | 402 ++ .../googleapis/iam/v1/iam_policy.pb.go | 453 ++ .../genproto/googleapis/iam/v1/options.pb.go | 98 + .../genproto/googleapis/iam/v1/policy.pb.go | 569 +++ .../googleapis/pubsub/v1/pubsub.pb.go | 4168 +++++++++++++++++ .../genproto/googleapis/type/expr/expr.pb.go | 127 + .../protobuf/field_mask/field_mask.pb.go | 284 ++ vendor/modules.txt | 19 + 94 files changed, 16286 insertions(+), 656 deletions(-) delete mode 100644 domain/feeds.go create mode 100644 domain/publisher.go create mode 100644 handler/handle_poll.go create mode 100644 handler/pubsub_article.go create mode 100644 handler/pubsub_source.go create mode 100644 idgen/idgen.go create mode 100644 static/turbolinks.js create mode 100644 tmpl/frame.html create mode 100644 tmpl/frontpage-1.html delete mode 100644 tmpl/index.html create mode 100644 vendor/cloud.google.com/go/iam/iam.go create mode 100644 vendor/cloud.google.com/go/pubsub/CHANGES.md create mode 100644 vendor/cloud.google.com/go/pubsub/LICENSE create mode 100644 vendor/cloud.google.com/go/pubsub/README.md create mode 100644 vendor/cloud.google.com/go/pubsub/apiv1/doc.go create mode 100644 vendor/cloud.google.com/go/pubsub/apiv1/iam.go create mode 100644 vendor/cloud.google.com/go/pubsub/apiv1/path_funcs.go create mode 100644 vendor/cloud.google.com/go/pubsub/apiv1/publisher_client.go create mode 100644 vendor/cloud.google.com/go/pubsub/apiv1/subscriber_client.go create mode 100644 vendor/cloud.google.com/go/pubsub/debug.go create mode 100644 vendor/cloud.google.com/go/pubsub/doc.go create mode 100644 vendor/cloud.google.com/go/pubsub/flow_controller.go create mode 100644 vendor/cloud.google.com/go/pubsub/go.mod create mode 100644 vendor/cloud.google.com/go/pubsub/go.sum create mode 100644 vendor/cloud.google.com/go/pubsub/go_mod_tidy_hack.go create mode 100644 vendor/cloud.google.com/go/pubsub/internal/distribution/distribution.go create mode 100644 vendor/cloud.google.com/go/pubsub/iterator.go create mode 100644 vendor/cloud.google.com/go/pubsub/message.go create mode 100644 vendor/cloud.google.com/go/pubsub/nodebug.go create mode 100644 vendor/cloud.google.com/go/pubsub/pubsub.go create mode 100644 vendor/cloud.google.com/go/pubsub/pullstream.go create mode 100644 vendor/cloud.google.com/go/pubsub/service.go create mode 100644 vendor/cloud.google.com/go/pubsub/snapshot.go create mode 100644 vendor/cloud.google.com/go/pubsub/subscription.go create mode 100644 vendor/cloud.google.com/go/pubsub/topic.go create mode 100644 vendor/cloud.google.com/go/pubsub/trace.go create mode 100644 vendor/github.com/bwmarrin/snowflake/.travis.yml create mode 100644 vendor/github.com/bwmarrin/snowflake/LICENSE create mode 100644 vendor/github.com/bwmarrin/snowflake/README.md create mode 100644 vendor/github.com/bwmarrin/snowflake/snowflake.go create mode 100644 vendor/github.com/fatih/color/LICENSE.md create mode 100644 vendor/github.com/fatih/color/README.md create mode 100644 vendor/github.com/fatih/color/color.go create mode 100644 vendor/github.com/fatih/color/doc.go create mode 100644 vendor/github.com/fatih/color/go.mod create mode 100644 vendor/github.com/fatih/color/go.sum create mode 100644 vendor/github.com/mattn/go-colorable/.travis.yml create mode 100644 vendor/github.com/mattn/go-colorable/LICENSE create mode 100644 vendor/github.com/mattn/go-colorable/README.md create mode 100644 vendor/github.com/mattn/go-colorable/colorable_appengine.go create mode 100644 vendor/github.com/mattn/go-colorable/colorable_others.go create mode 100644 vendor/github.com/mattn/go-colorable/colorable_windows.go create mode 100644 vendor/github.com/mattn/go-colorable/go.mod create mode 100644 vendor/github.com/mattn/go-colorable/go.sum create mode 100644 vendor/github.com/mattn/go-colorable/go.test.sh create mode 100644 vendor/github.com/mattn/go-colorable/noncolorable.go create mode 100644 vendor/github.com/mattn/go-isatty/.travis.yml create mode 100644 vendor/github.com/mattn/go-isatty/LICENSE create mode 100644 vendor/github.com/mattn/go-isatty/README.md create mode 100644 vendor/github.com/mattn/go-isatty/doc.go create mode 100644 vendor/github.com/mattn/go-isatty/go.mod create mode 100644 vendor/github.com/mattn/go-isatty/go.sum create mode 100644 vendor/github.com/mattn/go-isatty/go.test.sh create mode 100644 vendor/github.com/mattn/go-isatty/isatty_bsd.go create mode 100644 vendor/github.com/mattn/go-isatty/isatty_others.go create mode 100644 vendor/github.com/mattn/go-isatty/isatty_plan9.go create mode 100644 vendor/github.com/mattn/go-isatty/isatty_solaris.go create mode 100644 vendor/github.com/mattn/go-isatty/isatty_tcgets.go create mode 100644 vendor/github.com/mattn/go-isatty/isatty_windows.go create mode 100644 vendor/github.com/mattn/go-isatty/renovate.json create mode 100644 vendor/golang.org/x/sync/semaphore/semaphore.go create mode 100644 vendor/google.golang.org/api/support/bundler/bundler.go create mode 100644 vendor/google.golang.org/genproto/googleapis/iam/v1/iam_policy.pb.go create mode 100644 vendor/google.golang.org/genproto/googleapis/iam/v1/options.pb.go create mode 100644 vendor/google.golang.org/genproto/googleapis/iam/v1/policy.pb.go create mode 100644 vendor/google.golang.org/genproto/googleapis/pubsub/v1/pubsub.pb.go create mode 100644 vendor/google.golang.org/genproto/googleapis/type/expr/expr.pb.go create mode 100644 vendor/google.golang.org/genproto/protobuf/field_mask/field_mask.pb.go diff --git a/dao/dao.go b/dao/dao.go index 124bdc3..65695db 100644 --- a/dao/dao.go +++ b/dao/dao.go @@ -34,6 +34,7 @@ type storedEdition struct { Date string StartTime time.Time EndTime time.Time + Created time.Time Sources []domain.Source Articles []string Categories []string @@ -42,6 +43,7 @@ type storedEdition struct { func GetEditionForTime(ctx context.Context, t time.Time, allowRecent bool) (*domain.Edition, error) { iter := client.Collection("editions").Documents(ctx) + candidates := []*domain.Edition{} var maxEdition storedEdition for { doc, err := iter.Next() @@ -61,13 +63,28 @@ func GetEditionForTime(ctx context.Context, t time.Time, allowRecent bool) (*dom } if s.EndTime.After(t) { e, err := editionFromStored(ctx, s) - return e, err + if err != nil { + return nil, err + } + candidates = append(candidates, e) } } - if maxEdition.ID != "" && allowRecent { - return editionFromStored(ctx, maxEdition) + if len(candidates) == 0 { + if maxEdition.ID != "" && allowRecent { + return editionFromStored(ctx, maxEdition) + } + } + + selected := &domain.Edition{} + for _, e := range candidates { + if e.Created.After(selected.Created) { + selected = e + } + } + if selected.ID == "" { + return nil, nil } - return nil, nil + return selected, nil } func SetEdition(ctx context.Context, e *domain.Edition) error { @@ -90,6 +107,7 @@ func editionToStored(e *domain.Edition) storedEdition { Sources: e.Sources, StartTime: e.StartTime, EndTime: e.EndTime, + Created: e.Created, Categories: e.Categories, Metadata: e.Metadata, } @@ -110,6 +128,7 @@ func editionFromStored(ctx context.Context, s storedEdition) (*domain.Edition, e Sources: s.Sources, StartTime: s.StartTime, EndTime: s.EndTime, + Created: s.Created, Categories: s.Categories, Metadata: s.Metadata, } @@ -162,3 +181,42 @@ func GetArticle(ctx context.Context, id string) (*domain.Article, error) { mu.Unlock() return &a, err } + +func GetArticleByURL(ctx context.Context, url string) (*domain.Article, error) { + iter := client.Collection("articles").Where("Link", "==", url).Documents(ctx) + docs, err := iter.GetAll() + if err != nil { + return nil, err + } + if len(docs) == 0 { + return nil, nil + } + a := domain.Article{} + err = docs[0].DataTo(&a) + mu.Lock() + articleCache[a.ID] = a + mu.Unlock() + return &a, err +} + +func GetArticlesByTime(ctx context.Context, start, end time.Time) ([]domain.Article, error) { + iter := client.Collection("articles"). + Where("Timestamp", ">", start). + Where("Timestamp", "<", end). + Documents(ctx) + docs, err := iter.GetAll() + if err != nil { + return nil, err + } + + out := []domain.Article{} + for _, doc := range docs { + a := domain.Article{} + err = doc.DataTo(&a) + mu.Lock() + articleCache[a.ID] = a + mu.Unlock() + out = append(out, a) + } + return out, nil +} diff --git a/domain/edition.go b/domain/edition.go index 87e28bb..18c1087 100644 --- a/domain/edition.go +++ b/domain/edition.go @@ -2,13 +2,8 @@ package domain import ( "context" - "sort" + "github.com/arussellsaw/news/idgen" "time" - "unicode/utf8" - - "github.com/pkg/errors" - - "github.com/google/uuid" ) var ( @@ -26,6 +21,7 @@ type Edition struct { StartTime time.Time EndTime time.Time + Created time.Time Metadata map[string]string @@ -56,7 +52,6 @@ top: } return "" }() - a.Trim(size) a.Layout = Layout{} return a } @@ -87,9 +82,10 @@ func NewEdition(ctx context.Context, now time.Time) (*Edition, error) { } e := Edition{ - ID: uuid.New().String(), + ID: idgen.New("edt"), Sources: sources, Categories: cats, + Created: time.Now(), } e.Date = time.Now().Format("Monday January 02 2006") @@ -105,49 +101,5 @@ func NewEdition(ctx context.Context, now time.Time) (*Edition, error) { e.Name = "Evening Edition" } - articles, err := FetchArticles(ctx) - if err != nil { - return nil, errors.Wrap(err, "error fetching articles") - } - - newArticles := []Article{} -L: - for _, a := range articles { - if time.Since(a.Timestamp) > 100*time.Hour { - continue - } - for _, e := range a.Content { - if !utf8.Valid([]byte(e.Value)) { - continue L - } - } - newArticles = append(newArticles, a) - } - e.Articles = newArticles - - bySource := make(map[string][]Article) - for _, a := range e.Articles { - bySource[a.Source.Name] = append(bySource[a.Source.Name], a) - } - newArticles = nil - for _, as := range bySource { - sort.Slice(as, func(i, j int) bool { - return as[i].Timestamp.After(as[j].Timestamp) - }) - } -top: - for s, as := range bySource { - newArticles = append(newArticles, as[0]) - bySource[s] = as[1:] - if len(bySource[s]) == 0 { - delete(bySource, s) - goto top - } - } - if len(bySource) != 0 { - goto top - } - e.Articles = newArticles - return &e, nil } diff --git a/domain/feeds.go b/domain/feeds.go deleted file mode 100644 index 728c610..0000000 --- a/domain/feeds.go +++ /dev/null @@ -1,168 +0,0 @@ -package domain - -import ( - "context" - "hash/fnv" - "io/ioutil" - "net/http" - "net/http/cookiejar" - "sort" - "strings" - - "github.com/google/uuid" - "github.com/monzo/slog" - - "github.com/mmcdole/gofeed" - "github.com/thatguystone/swan" - "golang.org/x/sync/errgroup" -) - -var ( - space = uuid.MustParse("45e990eb-e8d4-4a13-8e74-e544bd11e45d") - jar, _ = cookiejar.New(&cookiejar.Options{}) - c = http.Client{ - Jar: jar, //binks - } -) - -func FetchArticles(ctx context.Context) ([]Article, error) { - eg := errgroup.Group{} - articles := make(chan Article, 1024^2) - bucket := make(chan struct{}, 10) - for i := 0; i < 10; i++ { - bucket <- struct{}{} - } - for _, s := range sources { - s := s - eg.Go(func() error { - fp := gofeed.NewParser() - feed, err := fp.ParseURL(s.FeedURL) - if err != nil { - return err - } - - g := errgroup.Group{} - for _, item := range feed.Items { - item := item - s := s - <-bucket - g.Go(func() error { - defer func() { bucket <- struct{}{} }() - var imageURL string - if item.Image != nil { - imageURL = item.Image.URL - } - slogParams := map[string]string{ - "url": item.Link, - "image_url": imageURL, - } - var content string - if !s.DisableFetch { - res, err := c.Get(item.Link) - if err != nil { - slog.Error(ctx, "Error fetching article: %s", err, slogParams) - return nil - } - buf, err := ioutil.ReadAll(res.Body) - if err != nil { - slog.Error(ctx, "Error reading article: %s", err, slogParams) - return nil - } - article, err := swan.FromHTML(item.Link, buf) - if err != nil { - slog.Error(ctx, "Error parsing article: %s", err, slogParams) - return nil - } - content = article.CleanedText - if article.Img != nil { - imageURL = article.Img.Src - } - if content == "" { - content = item.Content - } - } - - var author string - if item.Author != nil { - author = item.Author.Name - } - - articles <- Article{ - ID: uuid.NewHash(fnv.New32(), space, []byte(item.Link), 4).String(), - Title: item.Title, - Description: item.Description, - Content: toElements(content, "\n"), - ImageURL: imageURL, - Link: item.Link, - Author: author, - Source: s, - Timestamp: *item.PublishedParsed, - TS: item.PublishedParsed.Format("15:04 02-01-2006"), - } - return nil - }) - } - return g.Wait() - }) - } - err := eg.Wait() - if err != nil { - return nil, err - } - close(articles) - out := []Article{} - for a := range articles { - out = append(out, a) - } - sort.Slice(out, func(i, j int) bool { - return out[i].Timestamp.Before(out[j].Timestamp) - }) - return out, nil -} - -func matchClass(class string, exclude []string) bool { - for _, e := range exclude { - if strings.Contains(class, e) { - return true - } - } - return false -} - -func FilterArticles(aa []Article, cat string) []Article { - out := []Article{} -L: - for _, a := range aa { - for _, c := range a.Source.Categories { - if c == cat || cat == "" { - out = append(out, a) - continue L - } - } - } - return out -} - -func toElements(s, br string) []Element { - var ( - lines = strings.Split(s, br) - out []Element - row string - ) - for _, line := range lines { - if line == "p" { - continue - } - if line == "" { - out = append(out, Element{Type: "text", Value: row}) - row = "" - continue - } - row += line - - } - if row != "" { - out = append(out, Element{Type: "text", Value: row}) - } - return out -} diff --git a/domain/publisher.go b/domain/publisher.go new file mode 100644 index 0000000..987b18f --- /dev/null +++ b/domain/publisher.go @@ -0,0 +1,78 @@ +package domain + +import ( + "bytes" + "cloud.google.com/go/pubsub" + "context" + "encoding/json" + "github.com/monzo/slog" + "net/http" +) + +// PubSubMessage is the payload of a Pub/Sub event. +type PubSubMessage struct { + Message pubsub.Message `json:"message"` + Subscription string `json:"subscription"` +} + +type Publisher interface { + Publish(ctx context.Context, topic string, payload interface{}) error +} + +func NewPubSubPublisher(ctx context.Context) (Publisher, error) { + ps, err := pubsub.NewClient(ctx, "russellsaw") + if err != nil { + return nil, err + } + return &PubSubPublisher{ + ps: ps, + }, nil +} + +type PubSubPublisher struct { + ps *pubsub.Client +} + +func (p *PubSubPublisher) Publish(ctx context.Context, topic string, payload interface{}) error { + t := p.ps.Topic(topic) + msg, err := json.Marshal(payload) + if err != nil { + return err + } + result := t.Publish(ctx, &pubsub.Message{ + Data: msg, + }) + _, err = result.Get(ctx) + return err +} + +type HTTPPublisher struct { + ArticleURL string + SourceURL string +} + +func (p *HTTPPublisher) Publish(ctx context.Context, topic string, payload interface{}) error { + var url string + switch topic { + case "articles": + url = p.ArticleURL + case "sources": + url = p.SourceURL + } + + pbuf, err := json.Marshal(payload) + if err != nil { + return err + } + buf, err := json.Marshal(PubSubMessage{Message: pubsub.Message{Data: pbuf}}) + if err != nil { + return err + } + go func() { + _, err := http.Post(url, "application/json", bytes.NewReader(buf)) + if err != nil { + slog.Error(ctx, "Error publishing event: %s", err) + } + }() + return nil +} diff --git a/domain/source.go b/domain/source.go index 326c437..b342c7c 100644 --- a/domain/source.go +++ b/domain/source.go @@ -1,5 +1,10 @@ package domain +import ( + "net/http" + "net/http/cookiejar" +) + type Source struct { Name string URL string @@ -9,6 +14,13 @@ type Source struct { DisableFetch bool } +var ( + jar, _ = cookiejar.New(&cookiejar.Options{}) + c = http.Client{ + Jar: jar, //binks + } +) + func GetSources() []Source { return sources } @@ -37,7 +49,7 @@ var sources = []Source{ Name: "Polygon", URL: "https://polygon.com", FeedURL: "https://www.polygon.com/rss/index.xml", - Categories: []string{"tech", "games"}, + Categories: []string{"games"}, }, { Name: "The Guardian", @@ -76,4 +88,22 @@ var sources = []Source{ Categories: []string{"opinion", "news"}, DisableFetch: true, }, + { + Name: "Eurogamer", + URL: "https://eurogamer.net", + FeedURL: "https://www.eurogamer.net/?format=rss", + Categories: []string{"games"}, + }, + { + Name: "Bon Appetit", + URL: "https://bonappetit.com", + FeedURL: "https://www.bonappetit.com/feed/rss", + Categories: []string{"food"}, + }, + { + Name: "Food52", + URL: "https://food52.com", + FeedURL: "https://food52.com/blog.rss", + Categories: []string{"food"}, + }, } diff --git a/go.mod b/go.mod index e84a543..2d28ff7 100644 --- a/go.mod +++ b/go.mod @@ -5,15 +5,19 @@ go 1.13 require ( cloud.google.com/go/bigquery v1.5.0 cloud.google.com/go/firestore v1.2.0 + cloud.google.com/go/pubsub v1.3.1 github.com/PuerkitoBio/goquery v1.5.1 github.com/araddon/dateparse v0.0.0-20180729174819-cfd92a431d0e github.com/boombuler/barcode v1.0.0 + github.com/bwmarrin/snowflake v0.3.0 github.com/esimov/dithergo v0.0.0-20190411040508-1672f44e9674 + github.com/fatih/color v1.10.0 github.com/fatih/set v0.2.1 github.com/gigawattio/window v0.0.0-20180317192513-0f5467e35573 github.com/golang/protobuf v1.4.0 // indirect github.com/google/uuid v1.1.1 github.com/jaytaylor/html2text v0.0.0-20180606194806-57d518f124b0 + github.com/mattheath/kala v0.0.0-20171219141654-d6276794bf0e // indirect github.com/mattn/go-runewidth v0.0.3 // indirect github.com/mmcdole/gofeed v1.0.0-beta2 github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf // indirect @@ -26,7 +30,7 @@ require ( github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect github.com/thatguystone/swan v0.0.0-20190904205542-d1079a5d0c05 golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e - golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a + golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a // indirect golang.org/x/text v0.3.2 google.golang.org/api v0.20.0 gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect diff --git a/go.sum b/go.sum index 7e3ad24..f7a76b2 100644 --- a/go.sum +++ b/go.sum @@ -24,6 +24,7 @@ cloud.google.com/go/firestore v1.2.0/go.mod h1:iISCjWnTpnoJT1R287xRdjvQHJrxQOpea cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I= cloud.google.com/go/pubsub v1.1.0/go.mod h1:EwwdRX2sKPjnvnqCa270oGRyludottCI76h+R3AArQw= cloud.google.com/go/pubsub v1.2.0/go.mod h1:jhfEVHT8odbXTkndysNHCcx0awwzvfOlguIAii9o8iA= +cloud.google.com/go/pubsub v1.3.1 h1:ukjixP1wl0LpnZ6LWtZJ0mX5tBmjp1f8Sqer8Z2OMUU= cloud.google.com/go/pubsub v1.3.1/go.mod h1:i+ucay31+CNRpDW4Lu78I4xXG+O1r/MAHgjpRVR+TSU= cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw= cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0ZeosJ0Rtdos= @@ -42,6 +43,8 @@ github.com/araddon/dateparse v0.0.0-20180729174819-cfd92a431d0e h1:s05JG2GwtJMHa github.com/araddon/dateparse v0.0.0-20180729174819-cfd92a431d0e/go.mod h1:SLqhdZcd+dF3TEVL2RMoob5bBP5R1P1qkox+HtCBgGI= github.com/boombuler/barcode v1.0.0 h1:s1TvRnXwL2xJRaccrdcBQMZxq6X7DvsMogtmJeHDdrc= github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= +github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0= +github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927/go.mod h1:h/aW8ynjgkuj+NQRlZcDbAbM1ORAbXjXX77sX7T289U= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= @@ -58,6 +61,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/esimov/dithergo v0.0.0-20190411040508-1672f44e9674 h1:aGL1DV221m50hQC8Ci71iIBB3iNRPEESEKguyGDF9pM= github.com/esimov/dithergo v0.0.0-20190411040508-1672f44e9674/go.mod h1:IuLZhv47vctnAic87c5rwf6Q1zAZ7MP8bfePn9sLs70= +github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= +github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= github.com/fatih/set v0.2.1 h1:nn2CaJyknWE/6txyUDGwysr3G5QC6xWB/PtVjPBbeaA= github.com/fatih/set v0.2.1/go.mod h1:+RKtMCH+favT2+3YecHGxcc0b4KyVWA1QWWJUs4E0CI= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= @@ -121,6 +126,12 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs= +github.com/mattheath/kala v0.0.0-20171219141654-d6276794bf0e h1:cj+w63ez19o7y7vunA8Q3rUIWwKEOUx7foqjnr4qbtI= +github.com/mattheath/kala v0.0.0-20171219141654-d6276794bf0e/go.mod h1:OFd5kqMcMRr5LyBjZulGPJxaEK9r/jQ6JCV4Xi32bKo= +github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8= +github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8BzLR4= github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/mmcdole/gofeed v1.0.0-beta2 h1:CjQ0ADhAwNSb08zknAkGOEYqr8zfZKfrzgk9BxpWP2E= @@ -251,6 +262,7 @@ golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/handler/handle_article.go b/handler/handle_article.go index 7c93f3f..3f1898b 100644 --- a/handler/handle_article.go +++ b/handler/handle_article.go @@ -1,8 +1,11 @@ package handler import ( + "github.com/arussellsaw/news/domain" "html/template" "net/http" + "strings" + "time" "github.com/arussellsaw/news/dao" "github.com/monzo/slog" @@ -11,24 +14,41 @@ import ( func handleArticle(w http.ResponseWriter, r *http.Request) { ctx := r.Context() - t := template.New("article.html") - t, err := t.ParseFiles("tmpl/article.html") + t := template.New("frame.html") + t, err := t.ParseFiles("tmpl/frame.html", "tmpl/article.html") if err != nil { slog.Error(ctx, "Error parsing template: %s", err) http.Error(w, err.Error(), 500) return } - article, err := dao.GetArticle(ctx, r.URL.Query().Get("id")) + edition, err := dao.GetEditionForTime(ctx, time.Now(), true) if err != nil { http.Error(w, err.Error(), 500) return } - - err = t.Execute(w, article) + article, err := dao.GetArticle(ctx, strings.TrimPrefix(r.URL.Query().Get("id"), "art_")) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + a := articlePage{ + Article: article, + Categories: edition.Categories, + Name: edition.Name, + Date: edition.Date, + } + err = t.Execute(w, a) if err != nil { slog.Error(ctx, "Error executing template: %s", err) http.Error(w, err.Error(), 500) return } } + +type articlePage struct { + *domain.Article + Categories []string + Name string + Date string +} diff --git a/handler/handle_generate_edition.go b/handler/handle_generate_edition.go index 83ac9ce..abf80e4 100644 --- a/handler/handle_generate_edition.go +++ b/handler/handle_generate_edition.go @@ -2,7 +2,9 @@ package handler import ( "net/http" + "sort" "time" + "unicode/utf8" "github.com/arussellsaw/news/dao" "github.com/arussellsaw/news/domain" @@ -17,7 +19,7 @@ func handleGenerateEdition(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), 500) return } - if e != nil { + if e != nil && r.URL.Query().Get("force") == "" { slog.Info(ctx, "Edition %s - %s already exists and is within window", e.ID, e.Name) w.Write([]byte(e.ID)) return @@ -29,6 +31,51 @@ func handleGenerateEdition(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), 500) return } + + start := time.Now().Add(-120 * time.Hour) + end := time.Now() + articles, err := dao.GetArticlesByTime(ctx, start, end) + if err != nil { + httpError(ctx, w, "error getting articles", err) + return + } + + newArticles := []domain.Article{} +L: + for _, a := range articles { + for _, e := range a.Content { + if !utf8.Valid([]byte(e.Value)) { + continue L + } + } + newArticles = append(newArticles, a) + } + e.Articles = newArticles + + bySource := make(map[string][]domain.Article) + for _, a := range e.Articles { + bySource[a.Source.Name] = append(bySource[a.Source.Name], a) + } + newArticles = nil + for _, as := range bySource { + sort.Slice(as, func(i, j int) bool { + return as[i].Timestamp.After(as[j].Timestamp) + }) + } +top: + for s, as := range bySource { + newArticles = append(newArticles, as[0]) + bySource[s] = as[1:] + if len(bySource[s]) == 0 { + delete(bySource, s) + goto top + } + } + if len(bySource) != 0 { + goto top + } + e.Articles = newArticles + err = dao.SetEdition(ctx, e) if err != nil { slog.Error(ctx, "Error storing edition: %s", err) diff --git a/handler/handle_news.go b/handler/handle_news.go index d62949b..e463dbb 100644 --- a/handler/handle_news.go +++ b/handler/handle_news.go @@ -13,8 +13,8 @@ import ( func handleNews(w http.ResponseWriter, r *http.Request) { ctx := r.Context() - t := template.New("index.html") - t, err := t.ParseFiles("tmpl/index.html", "tmpl/article-tile.html") + t := template.New("frame.html") + t, err := t.ParseFiles("tmpl/frame.html", "tmpl/frontpage-1.html", "tmpl/article-tile.html") if err != nil { slog.Error(ctx, "Error parsing template: %s", err) http.Error(w, err.Error(), 500) @@ -59,6 +59,10 @@ func handleNews(w http.ResponseWriter, r *http.Request) { e.Articles = newArticles } + for i := range e.Articles { + e.Articles[i].ID = "art_" + e.Articles[i].ID + } + err = t.Execute(w, &e) if err != nil { slog.Error(ctx, "Error executing template: %s", err) diff --git a/handler/handle_poll.go b/handler/handle_poll.go new file mode 100644 index 0000000..29af18b --- /dev/null +++ b/handler/handle_poll.go @@ -0,0 +1,20 @@ +package handler + +import ( + "github.com/arussellsaw/news/domain" + "github.com/monzo/slog" + "net/http" +) + +func handlePoll(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + for _, s := range domain.GetSources() { + err := p.Publish(ctx, "sources", SourceEvent{Source: s}) + if err != nil { + httpError(ctx, w, "Error marshaling pubsub event", err) + return + } + slog.Info(ctx, "Dispatched source %s", s.FeedURL) + } +} diff --git a/handler/pubsub_article.go b/handler/pubsub_article.go new file mode 100644 index 0000000..3ccd17c --- /dev/null +++ b/handler/pubsub_article.go @@ -0,0 +1,127 @@ +package handler + +import ( + "context" + "encoding/json" + "github.com/arussellsaw/news/dao" + "github.com/arussellsaw/news/idgen" + "github.com/thatguystone/swan" + "io/ioutil" + "net/http" + "strings" + "time" + "unicode/utf8" + + "github.com/monzo/slog" + + "github.com/arussellsaw/news/domain" +) + +type ArticleEvent struct { + Article domain.Article + Source domain.Source +} + +func handlePubsubArticle(w http.ResponseWriter, r *http.Request) { + var ( + m domain.PubSubMessage + e ArticleEvent + ctx = r.Context() + ) + if err := json.NewDecoder(r.Body).Decode(&m); err != nil { + slog.Error(ctx, "Error decoding JSON: %s", err) + return + } + + if err := json.Unmarshal(m.Message.Data, &e); err != nil { + slog.Error(ctx, "Error decoding JSON: %s", err) + return + } + + existing, err := dao.GetArticleByURL(ctx, e.Article.Link) + if err != nil { + slog.Error(ctx, "Error decoding JSON: %s", err) + return + } + if existing != nil { + slog.Debug(ctx, "Article already exists: %s - %s", existing.ID, existing.Title) + return + } + + slogParams := map[string]string{ + "url": e.Article.Link, + } + var content string + res, err := c.Get(e.Article.Link) + if err != nil { + slog.Error(ctx, "Error fetching article: %s", err, slogParams) + return + } + buf, err := ioutil.ReadAll(res.Body) + if err != nil { + slog.Error(ctx, "Error reading article: %s", err, slogParams) + return + } + article, err := swan.FromHTML(e.Article.Link, buf) + if err != nil { + slog.Error(ctx, "Error parsing article: %s", err, slogParams) + return + } + content = article.CleanedText + if !utf8.Valid([]byte(content)) { + slog.Error(ctx, "Skipping invalid utf8 in document: %s - %s", e.Article.Link, e.Article.Title) + return + } + + a := domain.Article{ + ID: idgen.New("art"), + Title: e.Article.Title, + Description: e.Article.Description, + Content: toElements(content, "\n"), + ImageURL: func() string { + if article.Img != nil { + return article.Img.Src + } + return "" + }(), + Link: e.Article.Link, + Source: e.Article.Source, + Timestamp: time.Now(), + TS: time.Now().Format("Mon Jan 2 15:04"), + } + err = dao.SetArticle(ctx, &a) + if err != nil { + slog.Error(ctx, "Error storing article: %s", err, slogParams) + return + } + slog.Debug(ctx, "Stored new article: %s - %s", a.ID, a.Title) +} + +func httpError(ctx context.Context, w http.ResponseWriter, msg string, err error) { + slog.Error(ctx, "%s: %s", msg, err) + http.Error(w, err.Error(), 500) + return +} + +func toElements(s, br string) []domain.Element { + var ( + lines = strings.Split(s, br) + out []domain.Element + row string + ) + for _, line := range lines { + if line == "p" { + continue + } + if line == "" { + out = append(out, domain.Element{Type: "text", Value: row}) + row = "" + continue + } + row += line + } + if row != "" { + out = append(out, domain.Element{Type: "text", Value: row}) + } + return out +} diff --git a/handler/pubsub_source.go b/handler/pubsub_source.go new file mode 100644 index 0000000..13ffdd2 --- /dev/null +++ b/handler/pubsub_source.go @@ -0,0 +1,63 @@ +package handler + +import ( + "encoding/json" + "net/http" + + "github.com/mmcdole/gofeed" + "github.com/monzo/slog" + + "github.com/arussellsaw/news/domain" +) + +var Prefix string + +type SourceEvent struct { + Source domain.Source +} + +func handlePubsubSource(w http.ResponseWriter, r *http.Request) { + var ( + m domain.PubSubMessage + e SourceEvent + ctx = r.Context() + ) + if err := json.NewDecoder(r.Body).Decode(&m); err != nil { + httpError(ctx, w, "Error decoding event", err) + return + } + + if err := json.Unmarshal(m.Message.Data, &e); err != nil { + httpError(ctx, w, "Error decoding feed", err) + return + } + + fp := gofeed.NewParser() + feed, err := fp.ParseURL(e.Source.FeedURL) + if err != nil { + httpError(ctx, w, "Error parsing rss feed", err) + return + } + + for _, item := range feed.Items { + err := p.Publish(ctx, "articles", ArticleEvent{ + Article: domain.Article{ + Title: item.Title, + Description: item.Description, + ImageURL: func() string { + if item.Image != nil { + return item.Image.URL + } + return "" + }(), + Link: item.Link, + Source: e.Source, + }, + }) + if err != nil { + httpError(ctx, w, "Error marshaling pubsub event", err) + return + } + slog.Info(ctx, "Dispatched article %s: %s", item.Link, item.Title) + } +} diff --git a/handler/router.go b/handler/router.go index 8998097..ed948e8 100644 --- a/handler/router.go +++ b/handler/router.go @@ -1,13 +1,26 @@ package handler import ( + "context" "github.com/arussellsaw/news/domain" + "github.com/monzo/slog" "net/http" + "net/http/cookiejar" + "os" "github.com/arussellsaw/news/pkg/util" ) -func Init() http.Handler { +var ( + jar, _ = cookiejar.New(&cookiejar.Options{}) + c = http.Client{ + Jar: jar, //binks + } + + p domain.Publisher +) + +func Init(ctx context.Context) http.Handler { m := http.NewServeMux() m.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/")))) m.Handle("/image", http.HandlerFunc(handleDitherImage)) @@ -15,6 +28,9 @@ func Init() http.Handler { m.Handle("/favicon.ico", http.NotFoundHandler()) m.Handle("/barcode", http.HandlerFunc(handleQRCode)) m.Handle("/generate-edition", http.HandlerFunc(handleGenerateEdition)) + m.Handle("/events/source", http.HandlerFunc(handlePubsubSource)) + m.Handle("/events/article", http.HandlerFunc(handlePubsubArticle)) + m.Handle("/poll", http.HandlerFunc(handlePoll)) m.Handle("/", http.HandlerFunc(handleNews)) h := util.CloudContextMiddleware( @@ -26,5 +42,20 @@ func Init() http.Handler { if err != nil { panic(err) } + + if os.Getenv("USER") == "alexrussell-saw" { + slog.Info(ctx, "Using HTTP Publisher") + p = &domain.HTTPPublisher{ + SourceURL: "http://localhost:8080/events/source", + ArticleURL: "http://localhost:8080/events/article", + } + } else { + slog.Info(ctx, "Using PubSub Publisher") + p, err = domain.NewPubSubPublisher(ctx) + if err != nil { + panic(err) + } + } + return h } diff --git a/idgen/idgen.go b/idgen/idgen.go new file mode 100644 index 0000000..57eec27 --- /dev/null +++ b/idgen/idgen.go @@ -0,0 +1,30 @@ +package idgen + +import ( + "context" + "fmt" + "strings" + + "github.com/bwmarrin/snowflake" +) + +var node *snowflake.Node + +func Init(ctx context.Context) error { + var err error + node, err = snowflake.NewNode(137) + return err +} + +func New(prefix string) string { + id := node.Generate() + return fmt.Sprintf("%s_%s", prefix, id.Base58()) +} + +func Parse(id string) (snowflake.ID, error) { + parts := strings.Split(id, "_") + if len(parts) != 2 { + return -1, fmt.Errorf("expected ID formatted as prefix_id, got %s", id) + } + return snowflake.ParseBase58([]byte(parts[1])) +} diff --git a/main.go b/main.go index ec4d150..29eac47 100644 --- a/main.go +++ b/main.go @@ -2,24 +2,37 @@ package main import ( "context" - "fmt" "net/http" "os" + "github.com/monzo/slog" + "github.com/arussellsaw/news/dao" "github.com/arussellsaw/news/handler" + "github.com/arussellsaw/news/idgen" "github.com/arussellsaw/news/pkg/util" - "github.com/google/uuid" - "github.com/monzo/slog" ) func main() { ctx := context.Background() - logger := util.ContextParamLogger{Logger: &util.StackDriverLogger{}} + + err := idgen.Init(ctx) + if err != nil { + slog.Error(ctx, "Error initialising idgen: %s", err) + os.Exit(1) + } + + var logger slog.Logger + logger = util.ContextParamLogger{Logger: &util.StackDriverLogger{}} + + if os.Getenv("USER") == "alexrussell-saw" { + logger = util.ColourLogger{Writer: os.Stdout} + handler.Prefix = "dev-" + } + slog.SetDefaultLogger(logger) - fmt.Println(uuid.New()) - err := dao.Init(ctx) + err = dao.Init(ctx) if err != nil { slog.Error(ctx, "error initialising dao: %s", err) os.Exit(1) @@ -33,5 +46,5 @@ func main() { } slog.Info(ctx, "ready, listening on addr: %s", addr) - slog.Error(ctx, "serving: %s", http.ListenAndServe(addr, handler.Init())) + slog.Error(ctx, "serving: %s", http.ListenAndServe(addr, handler.Init(ctx))) } diff --git a/pkg/util/slog.go b/pkg/util/slog.go index f389911..ceb91e6 100644 --- a/pkg/util/slog.go +++ b/pkg/util/slog.go @@ -3,11 +3,38 @@ package util import ( "encoding/json" "fmt" + "io" "sync" + "github.com/fatih/color" "github.com/monzo/slog" ) +type ColourLogger struct { + Writer io.Writer +} + +func (l ColourLogger) Log(evs ...slog.Event) { + for _, e := range evs { + switch e.Severity { + case slog.TraceSeverity: + fmt.Fprintf(l.Writer, "%s: %s\n", color.WhiteString("%s TRC", e.Timestamp.Format("15:04:05.000")), e.Message) + case slog.DebugSeverity: + fmt.Fprintf(l.Writer, "%s: %s\n", color.CyanString("%s DBG", e.Timestamp.Format("15:04:05.000")), e.Message) + case slog.InfoSeverity: + fmt.Fprintf(l.Writer, "%s: %s\n", color.BlueString("%s INF", e.Timestamp.Format("15:04:05.000")), e.Message) + case slog.WarnSeverity: + fmt.Fprintf(l.Writer, "%s: %s\n", color.YellowString("%s WRN", e.Timestamp.Format("15:04:05.000")), e.Message) + case slog.ErrorSeverity: + fmt.Fprintf(l.Writer, "%s: %s\n", color.RedString("%s ERR", e.Timestamp.Format("15:04:05.000")), e.Message) + case slog.CriticalSeverity: + fmt.Fprintf(l.Writer, "%s: %s\n", color.RedString("%s CRT", e.Timestamp.Format("15:04:05.000")), e.Message) + } + } +} + +func (l ColourLogger) Flush() error { return nil } + type ContextParamLogger struct { slog.Logger } diff --git a/static/darkmode.js b/static/darkmode.js index 829a1d6..f0b17a9 100644 --- a/static/darkmode.js +++ b/static/darkmode.js @@ -13,7 +13,7 @@ if (window.CSS && CSS.supports("color", "var(--fg)")) { document.documentElement.setAttribute("color-mode", "dark"); // Sets the user's preference in local storage localStorage.setItem("color-mode", "dark"); - }; // Get the buttons in the DOM + }; var toggleColorButtons = document.querySelectorAll(".color-mode__btn"); // Set up event listeners diff --git a/static/main.css b/static/main.css index e4b81dc..482a16c 100644 --- a/static/main.css +++ b/static/main.css @@ -1,3 +1,10 @@ +:root[color-mode="dark"] { + --fg: #ffffff; + --bg: #1f1f1f; + --thin-line: 0.5px solid white; + --line: 1px solid white; +} + :root[color-mode="light"] { --fg: #1f1f1f; --bg: #ffffff; @@ -5,12 +12,6 @@ --line: 1px solid black; } -:root[color-mode="dark"] { - --fg: #ffffff; - --bg: #1f1f1f; - --thin-line: 0.5px solid white; - --line: 1px solid white; -} :root[color-mode="dark"] .dark-invert { filter: invert(100%); @@ -64,7 +65,7 @@ .color-mode__btn:focus svg, .color-mode__btn:focus { outline: none; fill: #fff7d6; - fill: var(--tertiary, #fff7d6); + fill: var(--fg, #fff7d6); } body,html { @@ -81,6 +82,9 @@ a { color: var(--fg); } +a:hover { + color: var(--fg); +} @font-face { font-family: "NewYorker"; @@ -172,6 +176,7 @@ h5 { font-size: 0.7rem; overflow-x: hidden; border-right: var(--thin-line); + max-height: inherit; } .divider { @@ -218,3 +223,31 @@ h5 { font-size: 1.5rem; } +.tile { + flex-basis: inherit !important; + flex-grow: initial !important; +} + +.h100 {max-height: 100px !important;} +.h150 {max-height: 150px !important;} +.h200 {max-height: 200px !important;} +.h250 {max-height: 250px !important;} +.h300 {max-height: 300px !important;} +.h350 {max-height: 350px !important;} +.h400 {max-height: 400px !important;} +.h450 {max-height: 450px !important;} +.h500 {max-height: 500px !important;} +.h550 {max-height: 550px !important;} +.h600 {max-height: 600px !important;} +.h650 {max-height: 650px !important;} +.h700 {max-height: 700px !important;} +.h750 {max-height: 750px !important;} +.h800 {max-height: 800px !important;} +.h850 {max-height: 850px !important;} +.h900 {max-height: 900px !important;} +.h950 {max-height: 950px !important;} +.h1000 {max-height: 1000px !important;} +.h1050 {max-height: 1050px !important;} +.h1100 {max-height: 1100px !important;} +.h1150 {max-height: 1150px !important;} +.h1200 {max-height: 1200px !important;} diff --git a/static/turbolinks.js b/static/turbolinks.js new file mode 100644 index 0000000..644af9b --- /dev/null +++ b/static/turbolinks.js @@ -0,0 +1,6 @@ +/* +Turbolinks 5.2.0 +Copyright © 2018 Basecamp, LLC + */ +(function(){var t=this;(function(){(function(){this.Turbolinks={supported:function(){return null!=window.history.pushState&&null!=window.requestAnimationFrame&&null!=window.addEventListener}(),visit:function(t,r){return e.controller.visit(t,r)},clearCache:function(){return e.controller.clearCache()},setProgressBarDelay:function(t){return e.controller.setProgressBarDelay(t)}}}).call(this)}).call(t);var e=t.Turbolinks;(function(){(function(){var t,r,n,o=[].slice;e.copyObject=function(t){var e,r,n;r={};for(e in t)n=t[e],r[e]=n;return r},e.closest=function(e,r){return t.call(e,r)},t=function(){var t,e;return t=document.documentElement,null!=(e=t.closest)?e:function(t){var e;for(e=this;e;){if(e.nodeType===Node.ELEMENT_NODE&&r.call(e,t))return e;e=e.parentNode}}}(),e.defer=function(t){return setTimeout(t,1)},e.throttle=function(t){var e;return e=null,function(){var r;return r=1<=arguments.length?o.call(arguments,0):[],null!=e?e:e=requestAnimationFrame(function(n){return function(){return e=null,t.apply(n,r)}}(this))}},e.dispatch=function(t,e){var r,o,i,s,a,u;return a=null!=e?e:{},u=a.target,r=a.cancelable,o=a.data,i=document.createEvent("Events"),i.initEvent(t,!0,r===!0),i.data=null!=o?o:{},i.cancelable&&!n&&(s=i.preventDefault,i.preventDefault=function(){return this.defaultPrevented||Object.defineProperty(this,"defaultPrevented",{get:function(){return!0}}),s.call(this)}),(null!=u?u:document).dispatchEvent(i),i},n=function(){var t;return t=document.createEvent("Events"),t.initEvent("test",!0,!0),t.preventDefault(),t.defaultPrevented}(),e.match=function(t,e){return r.call(t,e)},r=function(){var t,e,r,n;return t=document.documentElement,null!=(e=null!=(r=null!=(n=t.matchesSelector)?n:t.webkitMatchesSelector)?r:t.msMatchesSelector)?e:t.mozMatchesSelector}(),e.uuid=function(){var t,e,r;for(r="",t=e=1;36>=e;t=++e)r+=9===t||14===t||19===t||24===t?"-":15===t?"4":20===t?(Math.floor(4*Math.random())+8).toString(16):Math.floor(15*Math.random()).toString(16);return r}}).call(this),function(){e.Location=function(){function t(t){var e,r;null==t&&(t=""),r=document.createElement("a"),r.href=t.toString(),this.absoluteURL=r.href,e=r.hash.length,2>e?this.requestURL=this.absoluteURL:(this.requestURL=this.absoluteURL.slice(0,-e),this.anchor=r.hash.slice(1))}var e,r,n,o;return t.wrap=function(t){return t instanceof this?t:new this(t)},t.prototype.getOrigin=function(){return this.absoluteURL.split("/",3).join("/")},t.prototype.getPath=function(){var t,e;return null!=(t=null!=(e=this.requestURL.match(/\/\/[^\/]*(\/[^?;]*)/))?e[1]:void 0)?t:"/"},t.prototype.getPathComponents=function(){return this.getPath().split("/").slice(1)},t.prototype.getLastPathComponent=function(){return this.getPathComponents().slice(-1)[0]},t.prototype.getExtension=function(){var t,e;return null!=(t=null!=(e=this.getLastPathComponent().match(/\.[^.]*$/))?e[0]:void 0)?t:""},t.prototype.isHTML=function(){return this.getExtension().match(/^(?:|\.(?:htm|html|xhtml))$/)},t.prototype.isPrefixedBy=function(t){var e;return e=r(t),this.isEqualTo(t)||o(this.absoluteURL,e)},t.prototype.isEqualTo=function(t){return this.absoluteURL===(null!=t?t.absoluteURL:void 0)},t.prototype.toCacheKey=function(){return this.requestURL},t.prototype.toJSON=function(){return this.absoluteURL},t.prototype.toString=function(){return this.absoluteURL},t.prototype.valueOf=function(){return this.absoluteURL},r=function(t){return e(t.getOrigin()+t.getPath())},e=function(t){return n(t,"/")?t:t+"/"},o=function(t,e){return t.slice(0,e.length)===e},n=function(t,e){return t.slice(-e.length)===e},t}()}.call(this),function(){var t=function(t,e){return function(){return t.apply(e,arguments)}};e.HttpRequest=function(){function r(r,n,o){this.delegate=r,this.requestCanceled=t(this.requestCanceled,this),this.requestTimedOut=t(this.requestTimedOut,this),this.requestFailed=t(this.requestFailed,this),this.requestLoaded=t(this.requestLoaded,this),this.requestProgressed=t(this.requestProgressed,this),this.url=e.Location.wrap(n).requestURL,this.referrer=e.Location.wrap(o).absoluteURL,this.createXHR()}return r.NETWORK_FAILURE=0,r.TIMEOUT_FAILURE=-1,r.timeout=60,r.prototype.send=function(){var t;return this.xhr&&!this.sent?(this.notifyApplicationBeforeRequestStart(),this.setProgress(0),this.xhr.send(),this.sent=!0,"function"==typeof(t=this.delegate).requestStarted?t.requestStarted():void 0):void 0},r.prototype.cancel=function(){return this.xhr&&this.sent?this.xhr.abort():void 0},r.prototype.requestProgressed=function(t){return t.lengthComputable?this.setProgress(t.loaded/t.total):void 0},r.prototype.requestLoaded=function(){return this.endRequest(function(t){return function(){var e;return 200<=(e=t.xhr.status)&&300>e?t.delegate.requestCompletedWithResponse(t.xhr.responseText,t.xhr.getResponseHeader("Turbolinks-Location")):(t.failed=!0,t.delegate.requestFailedWithStatusCode(t.xhr.status,t.xhr.responseText))}}(this))},r.prototype.requestFailed=function(){return this.endRequest(function(t){return function(){return t.failed=!0,t.delegate.requestFailedWithStatusCode(t.constructor.NETWORK_FAILURE)}}(this))},r.prototype.requestTimedOut=function(){return this.endRequest(function(t){return function(){return t.failed=!0,t.delegate.requestFailedWithStatusCode(t.constructor.TIMEOUT_FAILURE)}}(this))},r.prototype.requestCanceled=function(){return this.endRequest()},r.prototype.notifyApplicationBeforeRequestStart=function(){return e.dispatch("turbolinks:request-start",{data:{url:this.url,xhr:this.xhr}})},r.prototype.notifyApplicationAfterRequestEnd=function(){return e.dispatch("turbolinks:request-end",{data:{url:this.url,xhr:this.xhr}})},r.prototype.createXHR=function(){return this.xhr=new XMLHttpRequest,this.xhr.open("GET",this.url,!0),this.xhr.timeout=1e3*this.constructor.timeout,this.xhr.setRequestHeader("Accept","text/html, application/xhtml+xml"),this.xhr.setRequestHeader("Turbolinks-Referrer",this.referrer),this.xhr.onprogress=this.requestProgressed,this.xhr.onload=this.requestLoaded,this.xhr.onerror=this.requestFailed,this.xhr.ontimeout=this.requestTimedOut,this.xhr.onabort=this.requestCanceled},r.prototype.endRequest=function(t){return this.xhr?(this.notifyApplicationAfterRequestEnd(),null!=t&&t.call(this),this.destroy()):void 0},r.prototype.setProgress=function(t){var e;return this.progress=t,"function"==typeof(e=this.delegate).requestProgressed?e.requestProgressed(this.progress):void 0},r.prototype.destroy=function(){var t;return this.setProgress(1),"function"==typeof(t=this.delegate).requestFinished&&t.requestFinished(),this.delegate=null,this.xhr=null},r}()}.call(this),function(){var t=function(t,e){return function(){return t.apply(e,arguments)}};e.ProgressBar=function(){function e(){this.trickle=t(this.trickle,this),this.stylesheetElement=this.createStylesheetElement(),this.progressElement=this.createProgressElement()}var r;return r=300,e.defaultCSS=".turbolinks-progress-bar {\n position: fixed;\n display: block;\n top: 0;\n left: 0;\n height: 3px;\n background: #0076ff;\n z-index: 9999;\n transition: width "+r+"ms ease-out, opacity "+r/2+"ms "+r/2+"ms ease-in;\n transform: translate3d(0, 0, 0);\n}",e.prototype.show=function(){return this.visible?void 0:(this.visible=!0,this.installStylesheetElement(),this.installProgressElement(),this.startTrickling())},e.prototype.hide=function(){return this.visible&&!this.hiding?(this.hiding=!0,this.fadeProgressElement(function(t){return function(){return t.uninstallProgressElement(),t.stopTrickling(),t.visible=!1,t.hiding=!1}}(this))):void 0},e.prototype.setValue=function(t){return this.value=t,this.refresh()},e.prototype.installStylesheetElement=function(){return document.head.insertBefore(this.stylesheetElement,document.head.firstChild)},e.prototype.installProgressElement=function(){return this.progressElement.style.width=0,this.progressElement.style.opacity=1,document.documentElement.insertBefore(this.progressElement,document.body),this.refresh()},e.prototype.fadeProgressElement=function(t){return this.progressElement.style.opacity=0,setTimeout(t,1.5*r)},e.prototype.uninstallProgressElement=function(){return this.progressElement.parentNode?document.documentElement.removeChild(this.progressElement):void 0},e.prototype.startTrickling=function(){return null!=this.trickleInterval?this.trickleInterval:this.trickleInterval=setInterval(this.trickle,r)},e.prototype.stopTrickling=function(){return clearInterval(this.trickleInterval),this.trickleInterval=null},e.prototype.trickle=function(){return this.setValue(this.value+Math.random()/100)},e.prototype.refresh=function(){return requestAnimationFrame(function(t){return function(){return t.progressElement.style.width=10+90*t.value+"%"}}(this))},e.prototype.createStylesheetElement=function(){var t;return t=document.createElement("style"),t.type="text/css",t.textContent=this.constructor.defaultCSS,t},e.prototype.createProgressElement=function(){var t;return t=document.createElement("div"),t.className="turbolinks-progress-bar",t},e}()}.call(this),function(){var t=function(t,e){return function(){return t.apply(e,arguments)}};e.BrowserAdapter=function(){function r(r){this.controller=r,this.showProgressBar=t(this.showProgressBar,this),this.progressBar=new e.ProgressBar}var n,o,i;return i=e.HttpRequest,n=i.NETWORK_FAILURE,o=i.TIMEOUT_FAILURE,r.prototype.visitProposedToLocationWithAction=function(t,e){return this.controller.startVisitToLocationWithAction(t,e)},r.prototype.visitStarted=function(t){return t.issueRequest(),t.changeHistory(),t.loadCachedSnapshot()},r.prototype.visitRequestStarted=function(t){return this.progressBar.setValue(0),t.hasCachedSnapshot()||"restore"!==t.action?this.showProgressBarAfterDelay():this.showProgressBar()},r.prototype.visitRequestProgressed=function(t){return this.progressBar.setValue(t.progress)},r.prototype.visitRequestCompleted=function(t){return t.loadResponse()},r.prototype.visitRequestFailedWithStatusCode=function(t,e){switch(e){case n:case o:return this.reload();default:return t.loadResponse()}},r.prototype.visitRequestFinished=function(t){return this.hideProgressBar()},r.prototype.visitCompleted=function(t){return t.followRedirect()},r.prototype.pageInvalidated=function(){return this.reload()},r.prototype.showProgressBarAfterDelay=function(){return this.progressBarTimeout=setTimeout(this.showProgressBar,this.controller.progressBarDelay)},r.prototype.showProgressBar=function(){return this.progressBar.show()},r.prototype.hideProgressBar=function(){return this.progressBar.hide(),clearTimeout(this.progressBarTimeout)},r.prototype.reload=function(){return window.location.reload()},r}()}.call(this),function(){var t=function(t,e){return function(){return t.apply(e,arguments)}};e.History=function(){function r(e){this.delegate=e,this.onPageLoad=t(this.onPageLoad,this),this.onPopState=t(this.onPopState,this)}return r.prototype.start=function(){return this.started?void 0:(addEventListener("popstate",this.onPopState,!1),addEventListener("load",this.onPageLoad,!1),this.started=!0)},r.prototype.stop=function(){return this.started?(removeEventListener("popstate",this.onPopState,!1),removeEventListener("load",this.onPageLoad,!1),this.started=!1):void 0},r.prototype.push=function(t,r){return t=e.Location.wrap(t),this.update("push",t,r)},r.prototype.replace=function(t,r){return t=e.Location.wrap(t),this.update("replace",t,r)},r.prototype.onPopState=function(t){var r,n,o,i;return this.shouldHandlePopState()&&(i=null!=(n=t.state)?n.turbolinks:void 0)?(r=e.Location.wrap(window.location),o=i.restorationIdentifier,this.delegate.historyPoppedToLocationWithRestorationIdentifier(r,o)):void 0},r.prototype.onPageLoad=function(t){return e.defer(function(t){return function(){return t.pageLoaded=!0}}(this))},r.prototype.shouldHandlePopState=function(){return this.pageIsLoaded()},r.prototype.pageIsLoaded=function(){return this.pageLoaded||"complete"===document.readyState},r.prototype.update=function(t,e,r){var n;return n={turbolinks:{restorationIdentifier:r}},history[t+"State"](n,null,e)},r}()}.call(this),function(){e.HeadDetails=function(){function t(t){var e,r,n,s,a,u;for(this.elements={},n=0,a=t.length;a>n;n++)u=t[n],u.nodeType===Node.ELEMENT_NODE&&(s=u.outerHTML,r=null!=(e=this.elements)[s]?e[s]:e[s]={type:i(u),tracked:o(u),elements:[]},r.elements.push(u))}var e,r,n,o,i;return t.fromHeadElement=function(t){var e;return new this(null!=(e=null!=t?t.childNodes:void 0)?e:[])},t.prototype.hasElementWithKey=function(t){return t in this.elements},t.prototype.getTrackedElementSignature=function(){var t,e;return function(){var r,n;r=this.elements,n=[];for(t in r)e=r[t].tracked,e&&n.push(t);return n}.call(this).join("")},t.prototype.getScriptElementsNotInDetails=function(t){return this.getElementsMatchingTypeNotInDetails("script",t)},t.prototype.getStylesheetElementsNotInDetails=function(t){return this.getElementsMatchingTypeNotInDetails("stylesheet",t)},t.prototype.getElementsMatchingTypeNotInDetails=function(t,e){var r,n,o,i,s,a;o=this.elements,s=[];for(n in o)i=o[n],a=i.type,r=i.elements,a!==t||e.hasElementWithKey(n)||s.push(r[0]);return s},t.prototype.getProvisionalElements=function(){var t,e,r,n,o,i,s;r=[],n=this.elements;for(e in n)o=n[e],s=o.type,i=o.tracked,t=o.elements,null!=s||i?t.length>1&&r.push.apply(r,t.slice(1)):r.push.apply(r,t);return r},t.prototype.getMetaValue=function(t){var e;return null!=(e=this.findMetaElementByName(t))?e.getAttribute("content"):void 0},t.prototype.findMetaElementByName=function(t){var r,n,o,i;r=void 0,i=this.elements;for(o in i)n=i[o].elements,e(n[0],t)&&(r=n[0]);return r},i=function(t){return r(t)?"script":n(t)?"stylesheet":void 0},o=function(t){return"reload"===t.getAttribute("data-turbolinks-track")},r=function(t){var e;return e=t.tagName.toLowerCase(),"script"===e},n=function(t){var e;return e=t.tagName.toLowerCase(),"style"===e||"link"===e&&"stylesheet"===t.getAttribute("rel")},e=function(t,e){var r;return r=t.tagName.toLowerCase(),"meta"===r&&t.getAttribute("name")===e},t}()}.call(this),function(){e.Snapshot=function(){function t(t,e){this.headDetails=t,this.bodyElement=e}return t.wrap=function(t){return t instanceof this?t:"string"==typeof t?this.fromHTMLString(t):this.fromHTMLElement(t)},t.fromHTMLString=function(t){var e;return e=document.createElement("html"),e.innerHTML=t,this.fromHTMLElement(e)},t.fromHTMLElement=function(t){var r,n,o,i;return o=t.querySelector("head"),r=null!=(i=t.querySelector("body"))?i:document.createElement("body"),n=e.HeadDetails.fromHeadElement(o),new this(n,r)},t.prototype.clone=function(){return new this.constructor(this.headDetails,this.bodyElement.cloneNode(!0))},t.prototype.getRootLocation=function(){var t,r;return r=null!=(t=this.getSetting("root"))?t:"/",new e.Location(r)},t.prototype.getCacheControlValue=function(){return this.getSetting("cache-control")},t.prototype.getElementForAnchor=function(t){try{return this.bodyElement.querySelector("[id='"+t+"'], a[name='"+t+"']")}catch(e){}},t.prototype.getPermanentElements=function(){return this.bodyElement.querySelectorAll("[id][data-turbolinks-permanent]")},t.prototype.getPermanentElementById=function(t){return this.bodyElement.querySelector("#"+t+"[data-turbolinks-permanent]")},t.prototype.getPermanentElementsPresentInSnapshot=function(t){var e,r,n,o,i;for(o=this.getPermanentElements(),i=[],r=0,n=o.length;n>r;r++)e=o[r],t.getPermanentElementById(e.id)&&i.push(e);return i},t.prototype.findFirstAutofocusableElement=function(){return this.bodyElement.querySelector("[autofocus]")},t.prototype.hasAnchor=function(t){return null!=this.getElementForAnchor(t)},t.prototype.isPreviewable=function(){return"no-preview"!==this.getCacheControlValue()},t.prototype.isCacheable=function(){return"no-cache"!==this.getCacheControlValue()},t.prototype.isVisitable=function(){return"reload"!==this.getSetting("visit-control")},t.prototype.getSetting=function(t){return this.headDetails.getMetaValue("turbolinks-"+t)},t}()}.call(this),function(){var t=[].slice;e.Renderer=function(){function e(){}var r;return e.render=function(){var e,r,n,o;return n=arguments[0],r=arguments[1],e=3<=arguments.length?t.call(arguments,2):[],o=function(t,e,r){r.prototype=t.prototype;var n=new r,o=t.apply(n,e);return Object(o)===o?o:n}(this,e,function(){}),o.delegate=n,o.render(r),o},e.prototype.renderView=function(t){return this.delegate.viewWillRender(this.newBody),t(),this.delegate.viewRendered(this.newBody)},e.prototype.invalidateView=function(){return this.delegate.viewInvalidated()},e.prototype.createScriptElement=function(t){var e;return"false"===t.getAttribute("data-turbolinks-eval")?t:(e=document.createElement("script"),e.textContent=t.textContent,e.async=!1,r(e,t),e)},r=function(t,e){var r,n,o,i,s,a,u;for(i=e.attributes,a=[],r=0,n=i.length;n>r;r++)s=i[r],o=s.name,u=s.value,a.push(t.setAttribute(o,u));return a},e}()}.call(this),function(){var t,r,n=function(t,e){function r(){this.constructor=t}for(var n in e)o.call(e,n)&&(t[n]=e[n]);return r.prototype=e.prototype,t.prototype=new r,t.__super__=e.prototype,t},o={}.hasOwnProperty;e.SnapshotRenderer=function(e){function o(t,e,r){this.currentSnapshot=t,this.newSnapshot=e,this.isPreview=r,this.currentHeadDetails=this.currentSnapshot.headDetails,this.newHeadDetails=this.newSnapshot.headDetails,this.currentBody=this.currentSnapshot.bodyElement,this.newBody=this.newSnapshot.bodyElement}return n(o,e),o.prototype.render=function(t){return this.shouldRender()?(this.mergeHead(),this.renderView(function(e){return function(){return e.replaceBody(),e.isPreview||e.focusFirstAutofocusableElement(),t()}}(this))):this.invalidateView()},o.prototype.mergeHead=function(){return this.copyNewHeadStylesheetElements(),this.copyNewHeadScriptElements(),this.removeCurrentHeadProvisionalElements(),this.copyNewHeadProvisionalElements()},o.prototype.replaceBody=function(){var t;return t=this.relocateCurrentBodyPermanentElements(),this.activateNewBodyScriptElements(),this.assignNewBody(),this.replacePlaceholderElementsWithClonedPermanentElements(t)},o.prototype.shouldRender=function(){return this.newSnapshot.isVisitable()&&this.trackedElementsAreIdentical()},o.prototype.trackedElementsAreIdentical=function(){return this.currentHeadDetails.getTrackedElementSignature()===this.newHeadDetails.getTrackedElementSignature()},o.prototype.copyNewHeadStylesheetElements=function(){var t,e,r,n,o;for(n=this.getNewHeadStylesheetElements(),o=[],e=0,r=n.length;r>e;e++)t=n[e],o.push(document.head.appendChild(t));return o},o.prototype.copyNewHeadScriptElements=function(){var t,e,r,n,o;for(n=this.getNewHeadScriptElements(),o=[],e=0,r=n.length;r>e;e++)t=n[e],o.push(document.head.appendChild(this.createScriptElement(t)));return o},o.prototype.removeCurrentHeadProvisionalElements=function(){var t,e,r,n,o;for(n=this.getCurrentHeadProvisionalElements(),o=[],e=0,r=n.length;r>e;e++)t=n[e],o.push(document.head.removeChild(t));return o},o.prototype.copyNewHeadProvisionalElements=function(){var t,e,r,n,o;for(n=this.getNewHeadProvisionalElements(),o=[],e=0,r=n.length;r>e;e++)t=n[e],o.push(document.head.appendChild(t));return o},o.prototype.relocateCurrentBodyPermanentElements=function(){var e,n,o,i,s,a,u;for(a=this.getCurrentBodyPermanentElements(),u=[],e=0,n=a.length;n>e;e++)i=a[e],s=t(i),o=this.newSnapshot.getPermanentElementById(i.id),r(i,s.element),r(o,i),u.push(s);return u},o.prototype.replacePlaceholderElementsWithClonedPermanentElements=function(t){var e,n,o,i,s,a,u;for(u=[],o=0,i=t.length;i>o;o++)a=t[o],n=a.element,s=a.permanentElement,e=s.cloneNode(!0),u.push(r(n,e));return u},o.prototype.activateNewBodyScriptElements=function(){var t,e,n,o,i,s;for(i=this.getNewBodyScriptElements(),s=[],e=0,o=i.length;o>e;e++)n=i[e],t=this.createScriptElement(n),s.push(r(n,t));return s},o.prototype.assignNewBody=function(){return document.body=this.newBody},o.prototype.focusFirstAutofocusableElement=function(){var t;return null!=(t=this.newSnapshot.findFirstAutofocusableElement())?t.focus():void 0},o.prototype.getNewHeadStylesheetElements=function(){return this.newHeadDetails.getStylesheetElementsNotInDetails(this.currentHeadDetails)},o.prototype.getNewHeadScriptElements=function(){return this.newHeadDetails.getScriptElementsNotInDetails(this.currentHeadDetails)},o.prototype.getCurrentHeadProvisionalElements=function(){return this.currentHeadDetails.getProvisionalElements()},o.prototype.getNewHeadProvisionalElements=function(){return this.newHeadDetails.getProvisionalElements()},o.prototype.getCurrentBodyPermanentElements=function(){return this.currentSnapshot.getPermanentElementsPresentInSnapshot(this.newSnapshot)},o.prototype.getNewBodyScriptElements=function(){return this.newBody.querySelectorAll("script")},o}(e.Renderer),t=function(t){var e;return e=document.createElement("meta"),e.setAttribute("name","turbolinks-permanent-placeholder"),e.setAttribute("content",t.id),{element:e,permanentElement:t}},r=function(t,e){var r;return(r=t.parentNode)?r.replaceChild(e,t):void 0}}.call(this),function(){var t=function(t,e){function n(){this.constructor=t}for(var o in e)r.call(e,o)&&(t[o]=e[o]);return n.prototype=e.prototype,t.prototype=new n,t.__super__=e.prototype,t},r={}.hasOwnProperty;e.ErrorRenderer=function(e){function r(t){var e;e=document.createElement("html"),e.innerHTML=t,this.newHead=e.querySelector("head"),this.newBody=e.querySelector("body")}return t(r,e),r.prototype.render=function(t){return this.renderView(function(e){return function(){return e.replaceHeadAndBody(),e.activateBodyScriptElements(),t()}}(this))},r.prototype.replaceHeadAndBody=function(){var t,e;return e=document.head,t=document.body,e.parentNode.replaceChild(this.newHead,e),t.parentNode.replaceChild(this.newBody,t)},r.prototype.activateBodyScriptElements=function(){var t,e,r,n,o,i;for(n=this.getScriptElements(),i=[],e=0,r=n.length;r>e;e++)o=n[e],t=this.createScriptElement(o),i.push(o.parentNode.replaceChild(t,o));return i},r.prototype.getScriptElements=function(){return document.documentElement.querySelectorAll("script")},r}(e.Renderer)}.call(this),function(){e.View=function(){function t(t){this.delegate=t,this.htmlElement=document.documentElement}return t.prototype.getRootLocation=function(){return this.getSnapshot().getRootLocation()},t.prototype.getElementForAnchor=function(t){return this.getSnapshot().getElementForAnchor(t)},t.prototype.getSnapshot=function(){return e.Snapshot.fromHTMLElement(this.htmlElement)},t.prototype.render=function(t,e){var r,n,o;return o=t.snapshot,r=t.error,n=t.isPreview,this.markAsPreview(n),null!=o?this.renderSnapshot(o,n,e):this.renderError(r,e)},t.prototype.markAsPreview=function(t){return t?this.htmlElement.setAttribute("data-turbolinks-preview",""):this.htmlElement.removeAttribute("data-turbolinks-preview")},t.prototype.renderSnapshot=function(t,r,n){return e.SnapshotRenderer.render(this.delegate,n,this.getSnapshot(),e.Snapshot.wrap(t),r)},t.prototype.renderError=function(t,r){return e.ErrorRenderer.render(this.delegate,r,t)},t}()}.call(this),function(){var t=function(t,e){return function(){return t.apply(e,arguments)}};e.ScrollManager=function(){function r(r){this.delegate=r,this.onScroll=t(this.onScroll,this),this.onScroll=e.throttle(this.onScroll)}return r.prototype.start=function(){return this.started?void 0:(addEventListener("scroll",this.onScroll,!1),this.onScroll(),this.started=!0)},r.prototype.stop=function(){return this.started?(removeEventListener("scroll",this.onScroll,!1),this.started=!1):void 0},r.prototype.scrollToElement=function(t){return t.scrollIntoView()},r.prototype.scrollToPosition=function(t){var e,r;return e=t.x,r=t.y,window.scrollTo(e,r)},r.prototype.onScroll=function(t){return this.updatePosition({x:window.pageXOffset,y:window.pageYOffset})},r.prototype.updatePosition=function(t){var e;return this.position=t,null!=(e=this.delegate)?e.scrollPositionChanged(this.position):void 0},r}()}.call(this),function(){e.SnapshotCache=function(){function t(t){this.size=t,this.keys=[],this.snapshots={}}var r;return t.prototype.has=function(t){var e;return e=r(t),e in this.snapshots},t.prototype.get=function(t){var e;if(this.has(t))return e=this.read(t),this.touch(t),e},t.prototype.put=function(t,e){return this.write(t,e),this.touch(t),e},t.prototype.read=function(t){var e;return e=r(t),this.snapshots[e]},t.prototype.write=function(t,e){var n;return n=r(t),this.snapshots[n]=e},t.prototype.touch=function(t){var e,n;return n=r(t),e=this.keys.indexOf(n),e>-1&&this.keys.splice(e,1),this.keys.unshift(n),this.trim()},t.prototype.trim=function(){var t,e,r,n,o;for(n=this.keys.splice(this.size),o=[],t=0,r=n.length;r>t;t++)e=n[t],o.push(delete this.snapshots[e]);return o},r=function(t){return e.Location.wrap(t).toCacheKey()},t}()}.call(this),function(){var t=function(t,e){return function(){return t.apply(e,arguments)}};e.Visit=function(){function r(r,n,o){this.controller=r,this.action=o,this.performScroll=t(this.performScroll,this),this.identifier=e.uuid(),this.location=e.Location.wrap(n),this.adapter=this.controller.adapter,this.state="initialized",this.timingMetrics={}}var n;return r.prototype.start=function(){return"initialized"===this.state?(this.recordTimingMetric("visitStart"),this.state="started",this.adapter.visitStarted(this)):void 0},r.prototype.cancel=function(){var t;return"started"===this.state?(null!=(t=this.request)&&t.cancel(),this.cancelRender(),this.state="canceled"):void 0},r.prototype.complete=function(){var t;return"started"===this.state?(this.recordTimingMetric("visitEnd"),this.state="completed","function"==typeof(t=this.adapter).visitCompleted&&t.visitCompleted(this),this.controller.visitCompleted(this)):void 0},r.prototype.fail=function(){var t;return"started"===this.state?(this.state="failed","function"==typeof(t=this.adapter).visitFailed?t.visitFailed(this):void 0):void 0},r.prototype.changeHistory=function(){var t,e;return this.historyChanged?void 0:(t=this.location.isEqualTo(this.referrer)?"replace":this.action,e=n(t),this.controller[e](this.location,this.restorationIdentifier),this.historyChanged=!0)},r.prototype.issueRequest=function(){return this.shouldIssueRequest()&&null==this.request?(this.progress=0,this.request=new e.HttpRequest(this,this.location,this.referrer),this.request.send()):void 0},r.prototype.getCachedSnapshot=function(){var t;return!(t=this.controller.getCachedSnapshotForLocation(this.location))||null!=this.location.anchor&&!t.hasAnchor(this.location.anchor)||"restore"!==this.action&&!t.isPreviewable()?void 0:t},r.prototype.hasCachedSnapshot=function(){return null!=this.getCachedSnapshot()},r.prototype.loadCachedSnapshot=function(){var t,e;return(e=this.getCachedSnapshot())?(t=this.shouldIssueRequest(),this.render(function(){var r;return this.cacheSnapshot(),this.controller.render({snapshot:e,isPreview:t},this.performScroll),"function"==typeof(r=this.adapter).visitRendered&&r.visitRendered(this),t?void 0:this.complete()})):void 0},r.prototype.loadResponse=function(){return null!=this.response?this.render(function(){var t,e;return this.cacheSnapshot(),this.request.failed?(this.controller.render({error:this.response},this.performScroll),"function"==typeof(t=this.adapter).visitRendered&&t.visitRendered(this),this.fail()):(this.controller.render({snapshot:this.response},this.performScroll),"function"==typeof(e=this.adapter).visitRendered&&e.visitRendered(this),this.complete())}):void 0},r.prototype.followRedirect=function(){return this.redirectedToLocation&&!this.followedRedirect?(this.location=this.redirectedToLocation,this.controller.replaceHistoryWithLocationAndRestorationIdentifier(this.redirectedToLocation,this.restorationIdentifier),this.followedRedirect=!0):void 0},r.prototype.requestStarted=function(){var t;return this.recordTimingMetric("requestStart"),"function"==typeof(t=this.adapter).visitRequestStarted?t.visitRequestStarted(this):void 0},r.prototype.requestProgressed=function(t){var e;return this.progress=t,"function"==typeof(e=this.adapter).visitRequestProgressed?e.visitRequestProgressed(this):void 0},r.prototype.requestCompletedWithResponse=function(t,r){return this.response=t,null!=r&&(this.redirectedToLocation=e.Location.wrap(r)),this.adapter.visitRequestCompleted(this)},r.prototype.requestFailedWithStatusCode=function(t,e){return this.response=e,this.adapter.visitRequestFailedWithStatusCode(this,t)},r.prototype.requestFinished=function(){var t;return this.recordTimingMetric("requestEnd"),"function"==typeof(t=this.adapter).visitRequestFinished?t.visitRequestFinished(this):void 0},r.prototype.performScroll=function(){return this.scrolled?void 0:("restore"===this.action?this.scrollToRestoredPosition()||this.scrollToTop():this.scrollToAnchor()||this.scrollToTop(),this.scrolled=!0)},r.prototype.scrollToRestoredPosition=function(){var t,e;return t=null!=(e=this.restorationData)?e.scrollPosition:void 0,null!=t?(this.controller.scrollToPosition(t),!0):void 0},r.prototype.scrollToAnchor=function(){return null!=this.location.anchor?(this.controller.scrollToAnchor(this.location.anchor),!0):void 0},r.prototype.scrollToTop=function(){return this.controller.scrollToPosition({x:0,y:0})},r.prototype.recordTimingMetric=function(t){var e;return null!=(e=this.timingMetrics)[t]?e[t]:e[t]=(new Date).getTime()},r.prototype.getTimingMetrics=function(){return e.copyObject(this.timingMetrics)},n=function(t){switch(t){case"replace":return"replaceHistoryWithLocationAndRestorationIdentifier";case"advance":case"restore":return"pushHistoryWithLocationAndRestorationIdentifier"}},r.prototype.shouldIssueRequest=function(){return"restore"===this.action?!this.hasCachedSnapshot():!0},r.prototype.cacheSnapshot=function(){return this.snapshotCached?void 0:(this.controller.cacheSnapshot(),this.snapshotCached=!0)},r.prototype.render=function(t){return this.cancelRender(),this.frame=requestAnimationFrame(function(e){return function(){return e.frame=null,t.call(e)}}(this))},r.prototype.cancelRender=function(){return this.frame?cancelAnimationFrame(this.frame):void 0},r}()}.call(this),function(){var t=function(t,e){return function(){return t.apply(e,arguments)}};e.Controller=function(){function r(){this.clickBubbled=t(this.clickBubbled,this),this.clickCaptured=t(this.clickCaptured,this),this.pageLoaded=t(this.pageLoaded,this),this.history=new e.History(this),this.view=new e.View(this),this.scrollManager=new e.ScrollManager(this),this.restorationData={},this.clearCache(),this.setProgressBarDelay(500)}return r.prototype.start=function(){return e.supported&&!this.started?(addEventListener("click",this.clickCaptured,!0),addEventListener("DOMContentLoaded",this.pageLoaded,!1),this.scrollManager.start(),this.startHistory(),this.started=!0,this.enabled=!0):void 0},r.prototype.disable=function(){return this.enabled=!1},r.prototype.stop=function(){return this.started?(removeEventListener("click",this.clickCaptured,!0),removeEventListener("DOMContentLoaded",this.pageLoaded,!1),this.scrollManager.stop(),this.stopHistory(),this.started=!1):void 0},r.prototype.clearCache=function(){return this.cache=new e.SnapshotCache(10)},r.prototype.visit=function(t,r){var n,o;return null==r&&(r={}),t=e.Location.wrap(t),this.applicationAllowsVisitingLocation(t)?this.locationIsVisitable(t)?(n=null!=(o=r.action)?o:"advance",this.adapter.visitProposedToLocationWithAction(t,n)):window.location=t:void 0},r.prototype.startVisitToLocationWithAction=function(t,r,n){var o;return e.supported?(o=this.getRestorationDataForIdentifier(n),this.startVisit(t,r,{restorationData:o})):window.location=t},r.prototype.setProgressBarDelay=function(t){return this.progressBarDelay=t},r.prototype.startHistory=function(){return this.location=e.Location.wrap(window.location),this.restorationIdentifier=e.uuid(),this.history.start(),this.history.replace(this.location,this.restorationIdentifier)},r.prototype.stopHistory=function(){return this.history.stop()},r.prototype.pushHistoryWithLocationAndRestorationIdentifier=function(t,r){return this.restorationIdentifier=r,this.location=e.Location.wrap(t),this.history.push(this.location,this.restorationIdentifier)},r.prototype.replaceHistoryWithLocationAndRestorationIdentifier=function(t,r){return this.restorationIdentifier=r,this.location=e.Location.wrap(t),this.history.replace(this.location,this.restorationIdentifier)},r.prototype.historyPoppedToLocationWithRestorationIdentifier=function(t,r){var n;return this.restorationIdentifier=r,this.enabled?(n=this.getRestorationDataForIdentifier(this.restorationIdentifier),this.startVisit(t,"restore",{restorationIdentifier:this.restorationIdentifier,restorationData:n,historyChanged:!0}),this.location=e.Location.wrap(t)):this.adapter.pageInvalidated()},r.prototype.getCachedSnapshotForLocation=function(t){var e;return null!=(e=this.cache.get(t))?e.clone():void 0},r.prototype.shouldCacheSnapshot=function(){return this.view.getSnapshot().isCacheable(); +},r.prototype.cacheSnapshot=function(){var t,r;return this.shouldCacheSnapshot()?(this.notifyApplicationBeforeCachingSnapshot(),r=this.view.getSnapshot(),t=this.lastRenderedLocation,e.defer(function(e){return function(){return e.cache.put(t,r.clone())}}(this))):void 0},r.prototype.scrollToAnchor=function(t){var e;return(e=this.view.getElementForAnchor(t))?this.scrollToElement(e):this.scrollToPosition({x:0,y:0})},r.prototype.scrollToElement=function(t){return this.scrollManager.scrollToElement(t)},r.prototype.scrollToPosition=function(t){return this.scrollManager.scrollToPosition(t)},r.prototype.scrollPositionChanged=function(t){var e;return e=this.getCurrentRestorationData(),e.scrollPosition=t},r.prototype.render=function(t,e){return this.view.render(t,e)},r.prototype.viewInvalidated=function(){return this.adapter.pageInvalidated()},r.prototype.viewWillRender=function(t){return this.notifyApplicationBeforeRender(t)},r.prototype.viewRendered=function(){return this.lastRenderedLocation=this.currentVisit.location,this.notifyApplicationAfterRender()},r.prototype.pageLoaded=function(){return this.lastRenderedLocation=this.location,this.notifyApplicationAfterPageLoad()},r.prototype.clickCaptured=function(){return removeEventListener("click",this.clickBubbled,!1),addEventListener("click",this.clickBubbled,!1)},r.prototype.clickBubbled=function(t){var e,r,n;return this.enabled&&this.clickEventIsSignificant(t)&&(r=this.getVisitableLinkForNode(t.target))&&(n=this.getVisitableLocationForLink(r))&&this.applicationAllowsFollowingLinkToLocation(r,n)?(t.preventDefault(),e=this.getActionForLink(r),this.visit(n,{action:e})):void 0},r.prototype.applicationAllowsFollowingLinkToLocation=function(t,e){var r;return r=this.notifyApplicationAfterClickingLinkToLocation(t,e),!r.defaultPrevented},r.prototype.applicationAllowsVisitingLocation=function(t){var e;return e=this.notifyApplicationBeforeVisitingLocation(t),!e.defaultPrevented},r.prototype.notifyApplicationAfterClickingLinkToLocation=function(t,r){return e.dispatch("turbolinks:click",{target:t,data:{url:r.absoluteURL},cancelable:!0})},r.prototype.notifyApplicationBeforeVisitingLocation=function(t){return e.dispatch("turbolinks:before-visit",{data:{url:t.absoluteURL},cancelable:!0})},r.prototype.notifyApplicationAfterVisitingLocation=function(t){return e.dispatch("turbolinks:visit",{data:{url:t.absoluteURL}})},r.prototype.notifyApplicationBeforeCachingSnapshot=function(){return e.dispatch("turbolinks:before-cache")},r.prototype.notifyApplicationBeforeRender=function(t){return e.dispatch("turbolinks:before-render",{data:{newBody:t}})},r.prototype.notifyApplicationAfterRender=function(){return e.dispatch("turbolinks:render")},r.prototype.notifyApplicationAfterPageLoad=function(t){return null==t&&(t={}),e.dispatch("turbolinks:load",{data:{url:this.location.absoluteURL,timing:t}})},r.prototype.startVisit=function(t,e,r){var n;return null!=(n=this.currentVisit)&&n.cancel(),this.currentVisit=this.createVisit(t,e,r),this.currentVisit.start(),this.notifyApplicationAfterVisitingLocation(t)},r.prototype.createVisit=function(t,r,n){var o,i,s,a,u;return i=null!=n?n:{},a=i.restorationIdentifier,s=i.restorationData,o=i.historyChanged,u=new e.Visit(this,t,r),u.restorationIdentifier=null!=a?a:e.uuid(),u.restorationData=e.copyObject(s),u.historyChanged=o,u.referrer=this.location,u},r.prototype.visitCompleted=function(t){return this.notifyApplicationAfterPageLoad(t.getTimingMetrics())},r.prototype.clickEventIsSignificant=function(t){return!(t.defaultPrevented||t.target.isContentEditable||t.which>1||t.altKey||t.ctrlKey||t.metaKey||t.shiftKey)},r.prototype.getVisitableLinkForNode=function(t){return this.nodeIsVisitable(t)?e.closest(t,"a[href]:not([target]):not([download])"):void 0},r.prototype.getVisitableLocationForLink=function(t){var r;return r=new e.Location(t.getAttribute("href")),this.locationIsVisitable(r)?r:void 0},r.prototype.getActionForLink=function(t){var e;return null!=(e=t.getAttribute("data-turbolinks-action"))?e:"advance"},r.prototype.nodeIsVisitable=function(t){var r;return(r=e.closest(t,"[data-turbolinks]"))?"false"!==r.getAttribute("data-turbolinks"):!0},r.prototype.locationIsVisitable=function(t){return t.isPrefixedBy(this.view.getRootLocation())&&t.isHTML()},r.prototype.getCurrentRestorationData=function(){return this.getRestorationDataForIdentifier(this.restorationIdentifier)},r.prototype.getRestorationDataForIdentifier=function(t){var e;return null!=(e=this.restorationData)[t]?e[t]:e[t]={}},r}()}.call(this),function(){!function(){var t,e;if((t=e=document.currentScript)&&!e.hasAttribute("data-turbolinks-suppress-warning"))for(;t=t.parentNode;)if(t===document.body)return console.warn("You are loading Turbolinks from a {{end}} {{end}} {{define "big-article"}} {{if .ID}} -
+
+