Skip to content

Commit

Permalink
only statistics on blog posts starting with /year/month/day/...
Browse files Browse the repository at this point in the history
  • Loading branch information
quantonganh committed Jul 13, 2024
1 parent 75f1219 commit 803cb76
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net"
"net/http"
"path"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -87,15 +88,21 @@ func NewServer(logger zerolog.Logger, config *blog.Config, posts []*blog.Post) (

if config.Env != "local" {
go func() {
pattern := `^/\d{4}/\d{2}/\d{2}/[a-z-]+(\.md)?$`
regex := regexp.MustCompile(pattern)
ua := r.Header.Get("User-Agent")
if !strings.Contains(strings.ToLower(ua), "bot") {
if regex.MatchString(r.URL.Path) && !strings.Contains(strings.ToLower(ua), "bot") {
ip, err := httperror.GetIP(r)
if err != nil {
s.logger.Error().Err(err).Msg("failed to get IP address")
return
}
userID := generateUserID(ip, ua)

urlPath := r.URL.Path
if !strings.HasSuffix(urlPath, ".md") {
urlPath += ".md"
}
referer := r.Header.Get("Referer")
if referer == "" {
referer = "Unknown"
Expand All @@ -104,7 +111,7 @@ func NewServer(logger zerolog.Logger, config *blog.Config, posts []*blog.Post) (
data := map[string]string{
"ip": ip,
"user_agent": ua,
"url": r.URL.Path,
"url": urlPath,
"referer": referer,
"time": now,
}
Expand Down

0 comments on commit 803cb76

Please sign in to comment.