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
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 {
2020-03-28 17:54:00 +00:00
last24h := time . Now ( ) . Add ( time . Second * 86400 * - 1 )
2020-03-25 03:55:03 +00:00
var fpi [ ] HNFrontPage
2020-03-28 17:54:00 +00:00
r . db . Where ( "disappeared is not ? and disappeared > ?" , time . Time { } , last24h ) . Select ( "*, ( strftime('%s',disappeared) - strftime('%s',appeared)) as hangtime" ) . Order ( "hangtime asc" ) . Find ( & fpi )
2020-03-25 03:55:03 +00:00
type fprow struct {
2020-03-25 14:50:10 +00:00
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 {
2020-03-25 14:50:10 +00:00
Duration : timeDiffHuman ( item . Disappeared , item . Appeared ) ,
DurationSecs : timeDiffAbsSeconds ( item . Disappeared , item . Appeared ) ,
URL : item . URL ,
HNID : item . HNID ,
Score : item . Score ,
Title : item . Title ,
HighestRank : item . HighestRank ,
TimeGone : timeDiffHuman ( time . Now ( ) , item . Disappeared ) ,
TimeGoneSecs : timeDiffAbsSeconds ( time . Now ( ) , item . Disappeared ) ,
2020-03-25 03:55:03 +00:00
} )
}
type rowtwo struct {
2020-03-25 14:50:10 +00:00
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 {
2020-03-25 14:50:10 +00:00
Duration : timeDiffHuman ( time . Now ( ) , item . Appeared ) ,
DurationSecs : 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
}