diff --git a/Dockerfile b/Dockerfile index 3ee616e..56c913a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM golang:1.14 as builder -WORKDIR /go/src/git.eeqj.de/sneak/hntransparencylog +WORKDIR /go/src/git.eeqj.de/sneak/orangesite COPY . . #RUN make lint && make build @@ -16,9 +16,9 @@ FROM alpine RUN mkdir -p /app/bin -COPY --from=builder /go/src/git.eeqj.de/sneak/hntransparencylog/server /app/bin/server +COPY --from=builder /go/src/git.eeqj.de/sneak/orangesite/server /app/bin/server # FIXME figure out how to embed these stupid templates -COPY --from=builder /go/src/git.eeqj.de/sneak/hntransparencylog/view /app/view +COPY --from=builder /go/src/git.eeqj.de/sneak/orangesite/view /app/view # put the source in there too for safekeeping COPY --from=builder /go/go-src.tgz /usr/local/src/go-src.tgz diff --git a/Makefile b/Makefile index 10fbef2..ad85305 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ BUILDHOST := $(shell hostname -s) BUILDARCH := $(shell uname -m) FN := server -IMAGENAME := sneak/hntransparencylog +IMAGENAME := sneak/orangesite UNAME_S := $(shell uname -s) diff --git a/cmd/server/main.go b/cmd/server/main.go index c35bb46..f9e7707 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -4,7 +4,7 @@ package main import ( "os" - "git.eeqj.de/sneak/hntransparencylog/hn" + "git.eeqj.de/sneak/orangesite/hn" ) var Version string diff --git a/hn/fetcher.go b/hn/fetcher.go index 3a09a3b..5d141ab 100644 --- a/hn/fetcher.go +++ b/hn/fetcher.go @@ -12,8 +12,6 @@ import ( "github.com/rs/zerolog" ) -const SQLITE_NULL_DATETIME = "0001-01-01 00:00:00+00:00" - func NewFetcher(db *gorm.DB) *Fetcher { f := new(Fetcher) f.db = db @@ -99,14 +97,18 @@ func (f *Fetcher) StoreFrontPage() error { // disabled for now //f.db.Create(&s) + //FIXME check to see if the same HNID was already on the frontpage + //or not so we don't spam the db + // check to see if the item was on the frontpage already or not var c int - f.db.Model(&HNFrontPage{}).Where("hn_id = ? and disappeared is ?", id, SQLITE_NULL_DATETIME).Count(&c) + f.db.Model(&HNFrontPage{}).Where("hn_id = ?", id).Count(&c) if c == 0 { // first appearance on frontpage r := HNFrontPage{ HNID: uint(id), Appeared: t, + Disappeared: time.Time{}, HighestRank: uint(i + 1), Rank: uint(i + 1), Title: item.Title, @@ -122,11 +124,10 @@ func (f *Fetcher) StoreFrontPage() error { Str("url", item.URL). Msg("HN new story on frontpage") } else { - // it's still here, compare its ranking + // it's still here, (or back) var old HNFrontPage - f.db.Model(&HNFrontPage{}).Where("hn_id = ? and disappeared is ?", id, SQLITE_NULL_DATETIME).First(&old) - // FIXME update highestrank if new is lower - needSave := false + f.db.Model(&HNFrontPage{}).Where("hn_id = ?", id).First(&old) + if old.Rank != uint(i+1) { f.log.Info(). Uint("hnid", uint(id)). @@ -138,7 +139,6 @@ func (f *Fetcher) StoreFrontPage() error { Msg("HN story rank changed, recording new rank") old.Rank = uint(i + 1) old.Score = uint(item.Score) - needSave = true } if old.HighestRank > uint(i+1) { @@ -148,24 +148,22 @@ func (f *Fetcher) StoreFrontPage() error { Uint("newrecord", uint(i+1)). Msg("recording new record high rank for story") old.HighestRank = uint(i + 1) - needSave = true } if old.Score != uint(item.Score) { old.Score = uint(item.Score) - needSave = true } - if needSave { - f.db.Save(&old) - } + // in any case it's here now + old.Disappeared = time.Time{} + f.db.Save(&old) } } // FIXME iterate over frontpage items still active in DB and note any // that are no longer on the scrape - fpitems, err := f.db.Model(&HNFrontPage{}).Where("disappeared is ?", SQLITE_NULL_DATETIME).Rows() + fpitems, err := f.db.Model(&HNFrontPage{}).Where("disappeared is ?", time.Time{}).Rows() if err != nil { f.log.Error(). Err(err) @@ -196,8 +194,8 @@ func (f *Fetcher) StoreFrontPage() error { } } - fpitems.Close() // close tx before we do the update - f.db.Model(&HNFrontPage{}).Where("disappeared is ? and hn_id in (?)", SQLITE_NULL_DATETIME, toupdate).Update("Disappeared", t) + fpitems.Close() // close result before we do the update + f.db.Model(&HNFrontPage{}).Where("disappeared is ? and hn_id in (?)", time.Time{}, toupdate).Update("Disappeared", t) return nil } diff --git a/hn/handlers.go b/hn/handlers.go index bb5d511..3b519dc 100644 --- a/hn/handlers.go +++ b/hn/handlers.go @@ -10,18 +10,20 @@ import ( ) type RequestHandlerSet struct { - db *gorm.DB + db *gorm.DB + version string } -func NewRequestHandlerSet(db *gorm.DB) *RequestHandlerSet { +func NewRequestHandlerSet(version string, db *gorm.DB) *RequestHandlerSet { rhs := new(RequestHandlerSet) rhs.db = db + rhs.version = version return rhs } func (r *RequestHandlerSet) indexHandler(c echo.Context) error { var fpi []HNFrontPage - r.db.Where("disappeared is not ?", SQLITE_NULL_DATETIME).Order("disappeared desc").Find(&fpi) + r.db.Where("disappeared is not ?", time.Time{}).Order("disappeared desc").Find(&fpi) type fprow struct { Duration string @@ -63,7 +65,7 @@ func (r *RequestHandlerSet) indexHandler(c echo.Context) error { var currentfp []rowtwo var cur []HNFrontPage - r.db.Where("disappeared is ?", SQLITE_NULL_DATETIME).Order("rank asc").Find(&cur) + r.db.Where("disappeared is ?", time.Time{}).Order("rank asc").Find(&cur) for _, item := range cur { currentfp = append(currentfp, rowtwo{ @@ -82,13 +84,15 @@ func (r *RequestHandlerSet) indexHandler(c echo.Context) error { "time": time.Now().UTC().Format(time.RFC3339Nano), "exits": fprows, "current": currentfp, + "gitrev": r.version, } return c.Render(http.StatusOK, "index.html", tc) } func (r *RequestHandlerSet) aboutHandler(c echo.Context) error { tc := pongo2.Context{ - "time": time.Now().UTC().Format(time.RFC3339Nano), + "time": time.Now().UTC().Format(time.RFC3339Nano), + "gitrev": r.version, } return c.Render(http.StatusOK, "about.html", tc) } diff --git a/hn/server.go b/hn/server.go index aca6f46..2a9149c 100644 --- a/hn/server.go +++ b/hn/server.go @@ -122,7 +122,7 @@ func (a *App) runForever() int { } a.e.Renderer = r - rhs := NewRequestHandlerSet(a.db) + rhs := NewRequestHandlerSet(a.version, a.db) // Routes a.e.GET("/", rhs.indexHandler) diff --git a/view/about.html b/view/about.html index e641ea8..c025fa5 100644 --- a/view/about.html +++ b/view/about.html @@ -5,7 +5,7 @@

About This Site

-

IMPORTANT

+

⚠️ Important!

This is a third-party site, not affiliated in any way with HN, provided for informational purposes only. Do not contact or @@ -24,12 +24,9 @@

How?

-

Go and the +

Go and the graciously provided HN API.

-

Who?

- -

@sneak.

{% endblock %} diff --git a/view/base.html b/view/base.html index 153f6b3..f299191 100644 --- a/view/base.html +++ b/view/base.html @@ -13,14 +13,7 @@ diff --git a/view/bodyfooter.html b/view/bodyfooter.html new file mode 100644 index 0000000..93c99e7 --- /dev/null +++ b/view/bodyfooter.html @@ -0,0 +1,10 @@ + diff --git a/view/index.html b/view/index.html index 83f3211..0c0fe19 100644 --- a/view/index.html +++ b/view/index.html @@ -61,8 +61,5 @@ - - -{{ time }} {% endblock %} diff --git a/view/page.html b/view/page.html index 5a33e9f..07251ec 100644 --- a/view/page.html +++ b/view/page.html @@ -12,4 +12,5 @@ {% endblock %} + {% include "bodyfooter.html" %} {% endblock %} diff --git a/view/style.css b/view/style.css new file mode 100644 index 0000000..6cac2b6 --- /dev/null +++ b/view/style.css @@ -0,0 +1,18 @@ +body { + background: #f6f6ef; +} + +#pagebody { + margin-top: 4em; +} + +footer { + padding-left: 1em; + padding-top: 2em; + padding-bottom: 0.5em; +} + +h1,h2,h3,h4 { + padding-top: 1em; + padding-bottom: 0.25em; +}