orangesite/hn/handlers.go

101 lines
2.4 KiB
Go
Raw Normal View History

2020-03-24 20:32:35 +00:00
package hn
import (
"net/http"
2020-03-25 00:49:19 +00:00
"time"
2020-03-24 20:32:35 +00:00
u "git.eeqj.de/sneak/goutil"
2020-03-25 00:49:19 +00:00
"github.com/flosch/pongo2"
2020-03-25 03:55:03 +00:00
"github.com/jinzhu/gorm"
2020-03-24 20:32:35 +00:00
"github.com/labstack/echo"
)
2020-03-25 03:55:03 +00:00
type RequestHandlerSet struct {
2020-03-25 16:11:31 +00:00
db *gorm.DB
version string
2020-03-25 03:55:03 +00:00
}
2020-03-25 16:11:31 +00:00
func NewRequestHandlerSet(version string, db *gorm.DB) *RequestHandlerSet {
2020-03-25 03:55:03 +00:00
rhs := new(RequestHandlerSet)
rhs.db = db
2020-03-25 16:11:31 +00:00
rhs.version = version
2020-03-25 03:55:03 +00:00
return rhs
}
func (r *RequestHandlerSet) indexHandler(c echo.Context) error {
last24h := time.Now().Add(time.Second * 86400 * -1)
2020-03-25 03:55:03 +00:00
var fpi []HNFrontPage
2020-03-28 19:48:04 +00:00
r.db.Where("disappeared is not ? and disappeared > ?", time.Time{}, last24h).Order("disappeared desc").Find(&fpi)
2020-03-25 03:55:03 +00:00
type fprow struct {
Duration string
DurationSecs uint
URL string
Title string
HighestRank uint
HNID uint
Score uint
TimeGone string
TimeGoneSecs uint
2020-03-25 03:55:03 +00:00
}
var fprows []fprow
for _, item := range fpi {
fprows = append(fprows, fprow{
Duration: u.TimeDiffHuman(item.Disappeared, item.Appeared),
DurationSecs: u.TimeDiffAbsSeconds(item.Disappeared, item.Appeared),
URL: item.URL,
HNID: item.HNID,
Score: item.Score,
Title: item.Title,
HighestRank: item.HighestRank,
TimeGone: u.TimeDiffHuman(time.Now(), item.Disappeared),
TimeGoneSecs: u.TimeDiffAbsSeconds(time.Now(), item.Disappeared),
2020-03-25 03:55:03 +00:00
})
}
type rowtwo struct {
Duration string
DurationSecs uint
URL string
Title string
Score uint
HighestRank uint
HNID uint
Rank uint
2020-03-25 03:55:03 +00:00
}
var currentfp []rowtwo
var cur []HNFrontPage
2020-03-25 16:11:31 +00:00
r.db.Where("disappeared is ?", time.Time{}).Order("rank asc").Find(&cur)
2020-03-25 03:55:03 +00:00
for _, item := range cur {
currentfp = append(currentfp, rowtwo{
Duration: u.TimeDiffHuman(time.Now(), item.Appeared),
DurationSecs: u.TimeDiffAbsSeconds(time.Now(), item.Appeared),
URL: item.URL,
HNID: item.HNID,
Score: item.Score,
Title: item.Title,
HighestRank: item.HighestRank,
Rank: item.Rank,
2020-03-25 03:55:03 +00:00
})
}
2020-03-25 00:49:19 +00:00
tc := pongo2.Context{
2020-03-25 03:55:03 +00:00
"time": time.Now().UTC().Format(time.RFC3339Nano),
"exits": fprows,
"current": currentfp,
2020-03-25 16:11:31 +00:00
"gitrev": r.version,
2020-03-25 00:49:19 +00:00
}
return c.Render(http.StatusOK, "index.html", tc)
}
2020-03-25 03:55:03 +00:00
func (r *RequestHandlerSet) aboutHandler(c echo.Context) error {
2020-03-25 00:49:19 +00:00
tc := pongo2.Context{
2020-03-25 16:11:31 +00:00
"time": time.Now().UTC().Format(time.RFC3339Nano),
"gitrev": r.version,
2020-03-25 00:49:19 +00:00
}
return c.Render(http.StatusOK, "about.html", tc)
2020-03-24 20:32:35 +00:00
}