/
pagerequest.go
138 lines (122 loc) · 3.07 KB
/
pagerequest.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package beepboop
import (
"context"
"fmt"
"net"
"net/http"
"strings"
"time"
"github.com/mssola/user_agent"
"github.com/razzie/babble"
"github.com/razzie/reqip"
)
// PageRequest ...
type PageRequest struct {
Context *Context
Request *http.Request
RequestID string
RelPath string
RelURI string
PagePath string
Title string
IsAPI bool
renderer LayoutRenderer
logged bool
session *Session
}
func newPageRequest(page *Page, r *http.Request, ctx *Context, renderer LayoutRenderer) *PageRequest {
pr := &PageRequest{
Context: ctx,
Request: r,
RequestID: newRequestID(),
PagePath: page.Path,
Title: page.Title,
IsAPI: renderer == nil,
renderer: renderer,
}
if pr.IsAPI {
pr.RelPath = strings.TrimPrefix(r.URL.Path, "/api"+page.Path)
pr.RelURI = strings.TrimPrefix(r.URL.RequestURI(), "/api"+page.Path)
} else {
pr.RelPath = strings.TrimPrefix(r.URL.Path, page.Path)
pr.RelURI = strings.TrimPrefix(r.URL.RequestURI(), page.Path)
}
return pr
}
func newRequestID() string {
i := uint16(time.Now().UnixNano())
babbler := babble.NewBabbler()
return fmt.Sprintf("%s-%x", babbler.Babble(), i)
}
func (r *PageRequest) logRequest() {
ip := reqip.GetClientIP(r.Request)
ua := user_agent.New(r.Request.UserAgent())
browser, ver := ua.Browser()
logmsg := fmt.Sprintf("[%s]: %s %s\n • %s, %s %s %s",
r.RequestID, r.Request.Method, r.Request.RequestURI,
ip, ua.OS(), browser, ver)
var hasLocation bool
if r.Context.GeoIPClient != nil {
loc, _ := r.Context.GeoIPClient.GetLocation(context.Background(), ip)
if loc != nil {
hasLocation = true
logmsg += ", " + loc.String()
}
}
if !hasLocation {
hostnames, _ := net.LookupAddr(ip)
logmsg += ", " + strings.Join(hostnames, ", ")
}
session, _ := r.Request.Cookie("session")
if session != nil {
logmsg += ", session: " + session.Value
}
r.Context.Logger.Print(logmsg)
r.logged = true
}
func (r *PageRequest) logRequestNonblocking() {
r.logged = true
go r.logRequest()
}
// Log ...
func (r *PageRequest) Log(a ...interface{}) {
if !r.logged {
r.logRequestNonblocking()
}
prefix := fmt.Sprintf("[%s] ", r.RequestID)
r.Context.Logger.Output(2, prefix+fmt.Sprint(a...))
}
// Logf ...
func (r *PageRequest) Logf(format string, a ...interface{}) {
if !r.logged {
r.logRequestNonblocking()
}
prefix := fmt.Sprintf("[%s] ", r.RequestID)
r.Context.Logger.Output(2, prefix+fmt.Sprintf(format, a...))
}
// Respond returns the default page response View
func (r *PageRequest) Respond(data interface{}, opts ...ViewOption) *View {
v := &View{
StatusCode: http.StatusOK,
Data: data,
}
for _, opt := range opts {
opt(v)
}
v.renderer = func(w http.ResponseWriter) {
r.renderer(w, r.Request, r.Title, data, v.StatusCode)
}
return v
}
// Session returns the current session
func (r *PageRequest) Session() *Session {
if r.session == nil {
r.session = newSession(r)
}
return r.session
}
func (r *PageRequest) updateSession(view *View) {
if r.session != nil {
view.cookies = append(view.cookies, r.session.toCookies(r.Context.CookieExpiration)...)
}
}