Skip to content

Commit

Permalink
pkg/visit.go: Server.visitList() list event from specifi delay
Browse files Browse the repository at this point in the history
  • Loading branch information
HuguesGuilleus committed Jun 29, 2020
1 parent 763c588 commit 143c1e6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
19 changes: 13 additions & 6 deletions pkg/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (s *Server) logAdd(u *User, op string, value ...string) {

// List the element for a specific period.
func (s *Server) logList(w http.ResponseWriter, r *http.Request) {
prefix, err := logGetPrefix(w, r)
prefix, err := getDuration(w, r, "log:")
if err {
return
}
Expand All @@ -51,7 +51,7 @@ func (s *Server) logList(w http.ResponseWriter, r *http.Request) {

// Send the number of element for a specifica period.
func (s *Server) logCount(w http.ResponseWriter, r *http.Request) {
prefix, err := logGetPrefix(w, r)
prefix, err := getDuration(w, r, "log:")
if err {
return
}
Expand All @@ -68,8 +68,7 @@ func (s *Server) logCount(w http.ResponseWriter, r *http.Request) {
}

// Get the prefix for log list or index operation
func logGetPrefix(w http.ResponseWriter, r *http.Request) (string, bool) {
prefix := "log:"
func getDuration(w http.ResponseWriter, r *http.Request, prefix string) (string, bool) {
q := r.URL.Query()

// Generic function to get a specific params
Expand Down Expand Up @@ -101,14 +100,22 @@ func logGetPrefix(w http.ResponseWriter, r *http.Request) (string, bool) {
if m := getP("mouth", "m"); m < 0 {
return "", true
} else if m > 0 {
prefix += strconv.FormatInt(m, 10) + "-"
prefix += towDigit(strconv.FormatInt(m, 10)) + "-"
// Day
if d := getP("day", "d"); d < 0 {
return "", true
} else if d > 0 {
prefix += strconv.FormatInt(d, 10) + "-"
prefix += towDigit(strconv.FormatInt(d, 10))
}
}

return prefix, false
}

// Transform: "2" --> "02"
func towDigit(s string) string {
if len(s) != 2 {
return "0" + s
}
return s
}
2 changes: 1 addition & 1 deletion pkg/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func Create(opt Option) *Server {
}

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Println("[REQ]", r.URL.Path)
log.Println("[REQ]", r.RequestURI)
w.Header().Add("Server", "Arveto auth server")
s.mux.ServeHTTP(w, r)
}
Expand Down
11 changes: 10 additions & 1 deletion pkg/visit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package auth
import (
"./public"
"encoding/json"
"log"
"net/http"
"time"
)
Expand Down Expand Up @@ -99,10 +100,18 @@ func (s *Server) visitTicket(w http.ResponseWriter, r *http.Request) {

// List the invitation
func (s *Server) visitList(w http.ResponseWriter, r *http.Request) {
prefix, err := getDuration(w, r, "visit:")
if err {
return
}

log.Printf("prefix: %#+v\n", prefix)

list := make([]Visit, 0)
s.db.ForS("visit:", 0, 0, nil, func(_ string, v Visit) {
s.db.ForS(prefix, 0, 0, nil, func(_ string, v Visit) {
list = append(list, v)
})

j, _ := json.Marshal(list)
w.Header().Set("Content-Type", "application/json")
w.Write(j)
Expand Down

0 comments on commit 143c1e6

Please sign in to comment.