searchInfo.go 733 B

123456789101112131415161718192021222324252627282930313233343536
  1. package main
  2. import (
  3. "net"
  4. "net/http"
  5. "strconv"
  6. "time"
  7. )
  8. const (
  9. timeFormat = "2006-01-02 15:04"
  10. )
  11. // SearchInfo represents concise information about search query
  12. type SearchInfo struct {
  13. Date time.Time
  14. Host, Phrase, Language, LanguageCode string
  15. ResultsServed int
  16. }
  17. func (s *SearchInfo) ToArray() []string {
  18. return []string{s.Date.Format(timeFormat), s.Host, s.Phrase, s.Language, strconv.Itoa(s.ResultsServed)}
  19. }
  20. func (s *SearchInfo) GetInfoFromRequest(r *http.Request) {
  21. s.Date = time.Now()
  22. s.Host, _, _ = net.SplitHostPort(r.RemoteAddr)
  23. s.Phrase = r.URL.Query().Get("phrase")
  24. if s.Phrase == "" {
  25. s.Phrase = "TMS"
  26. }
  27. s.LanguageCode = r.URL.Query().Get("lang")
  28. s.Language = app.Languages[s.LanguageCode]
  29. }