-
Notifications
You must be signed in to change notification settings - Fork 1
/
searchInfo.go
executable file
·45 lines (36 loc) · 911 Bytes
/
searchInfo.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
package main
import (
"net"
"net/http"
"strconv"
"time"
)
const (
timeFormat = "2006-01-02 15:04"
)
// SearchInfo represents concise information about search query
type SearchInfo struct {
Date time.Time
Host, Phrase, Language, LanguageCode string
Reverse bool
ResultsServed, SearchLimit int
}
func (s *SearchInfo) ToArray() []string {
return []string{s.Date.Format(timeFormat), s.Host, s.Phrase, s.Language, strconv.Itoa(s.ResultsServed)}
}
func (s *SearchInfo) ParseRequest(r *http.Request) {
s.Date = time.Now()
s.Host, _, _ = net.SplitHostPort(r.RemoteAddr)
s.Phrase = r.URL.Query().Get("phrase")
if r.URL.Query().Get("reverse") == "true" {
s.Reverse = true
}
limit, err := strconv.Atoi(r.URL.Query().Get("limit"))
if err != nil {
s.SearchLimit = 64
} else {
s.SearchLimit = limit
}
s.LanguageCode = r.URL.Query().Get("lang")
s.Language = app.Languages[s.LanguageCode]
}