Skip to content

Instantly share code, notes, and snippets.

@lansana
Created February 6, 2019 17:34
Show Gist options
  • Save lansana/6ae54efa7b7f2c27c5f56e6da882e7f5 to your computer and use it in GitHub Desktop.
Save lansana/6ae54efa7b7f2c27c5f56e6da882e7f5 to your computer and use it in GitHub Desktop.
package middleware
import (
"net/http"
"strconv"
"time"
"core/http/jsonapi"
"core/http/router"
"domain"
)
func DateRange(next http.Handler) http.Handler {
h := func(w http.ResponseWriter, r *http.Request) {
from := router.QueryString(r, "from")
if from == "" {
// ... return error response
return
}
fromInt, err := strconv.ParseInt(from, 10, 64)
if err != nil {
// ... return error response
return
}
to := router.QueryString(r, "to")
if from == "" {
// ... return error response
return
}
toInt, err := strconv.ParseInt(to, 10, 64)
if err != nil {
// ... return error response
return
}
fromTime := time.Unix(fromInt, 0)
toTime := time.Unix(toInt, 0)
domain.DateRange{
From: time.Date(fromTime.Year(), fromTime.Month(), fromTime.Day(), 0, 0, 0, 0, time.UTC),
To: time.Date(toTime.Year(), toTime.Month(), toTime.Day(), 23, 59, 59, 0, time.UTC),
}
next.ServeHTTP(w, r.WithContext(ctx))
}
return http.HandlerFunc(h)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment