forked from usefathom/fathom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
44 lines (35 loc) · 802 Bytes
/
api.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
package api
import (
"log"
"time"
"strconv"
"net/http"
)
const defaultPeriod = 7
const defaultLimit = 10
// log fatal errors
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}
func getRequestedLimit(r *http.Request) int {
limit, err := strconv.Atoi(r.URL.Query().Get("limit"))
if err != nil || limit == 0 {
limit = 10
}
return limit
}
func getRequestedPeriods(r *http.Request) (int64, int64) {
var before, after int64
var err error
before, err = strconv.ParseInt(r.URL.Query().Get("before"), 10, 64)
if err != nil || before == 0 {
before = time.Now().Unix()
}
after, err = strconv.ParseInt(r.URL.Query().Get("after"), 10, 64)
if err != nil || before == 0 {
after = time.Now().AddDate(0, 0, -7).Unix()
}
return before, after
}