-
Notifications
You must be signed in to change notification settings - Fork 0
/
go-lua-bindings.go
85 lines (69 loc) · 1.78 KB
/
go-lua-bindings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"log"
"net/http"
"net/url"
"time"
"github.com/nvlled/goom-temple/hn"
)
var gmtLocation *time.Location
func init() {
loc, err := time.LoadLocation("GMT")
if err != nil {
panic(err)
}
gmtLocation = loc
}
type GoLuaBindings struct {
response http.ResponseWriter
request *http.Request
}
func NewGoLuaBindings(w http.ResponseWriter, r *http.Request) *GoLuaBindings {
return &GoLuaBindings{response: w, request: r}
}
func (glb *GoLuaBindings) GetCurrentUser() *User {
return nil
//return &User{
// ID: 1,
// Username: "ronald",
//}
}
func (glb *GoLuaBindings) GetStories(feed string, pageSize, pageNum int) ([]*hn.Item, string, bool) {
items, err, hasMore := hn.FetchStories(feed, pageSize, pageNum)
if err != nil {
log.Print(err)
return nil, err.Error(), false
}
return items, "", hasMore
}
func (glb *GoLuaBindings) GetThread(id hn.ItemID) ([]*hn.Item, string) {
return logError(hn.FetchThread(id))
}
func (glb *GoLuaBindings) GetItem(id hn.ItemID) (*hn.Item, string) {
return logError(hn.FetchItem(id))
}
func (glb *GoLuaBindings) GetCommentChain(id hn.ItemID) ([]*hn.Item, string) {
return logError(hn.FetchCommentChain(id))
}
func (glb *GoLuaBindings) FormatTime(unixTime int64) string {
t := time.Unix(unixTime, 0).In(time.UTC)
return t.Format(time.RFC822)
}
func (glb *GoLuaBindings) ParseURL(rawURL string) (*url.URL, string) {
return logError(url.Parse(rawURL))
}
func logError[T any](x T, err error) (T, string) {
var defaultVal T
if err != nil {
log.Print(err)
return defaultVal, err.Error()
}
return x, ""
}
func (glb *GoLuaBindings) Redirect(url string) {
glb.response.Header().Add("Location", url)
glb.response.WriteHeader(http.StatusFound)
}
func (glb *GoLuaBindings) UnixMilli() int64 {
return time.Now().UnixMilli()
}