diff options
-rw-r--r-- | system/api/analytics/analytics.go | 97 | ||||
-rw-r--r-- | system/api/external.go | 11 | ||||
-rw-r--r-- | system/api/handlers.go | 13 |
3 files changed, 113 insertions, 8 deletions
diff --git a/system/api/analytics/analytics.go b/system/api/analytics/analytics.go new file mode 100644 index 0000000..c30ce26 --- /dev/null +++ b/system/api/analytics/analytics.go @@ -0,0 +1,97 @@ +// Package analytics provides the methods to run an analytics reporting system +// for API requests which may be useful to users for measuring access and +// possibly identifying bad actors abusing requests. +package analytics + +import ( + "log" + "net/http" + "strings" + "time" + + "github.com/boltdb/bolt" +) + +type apiRequest struct { + URL string `json:"url"` + Method string `json:"http_method"` + RemoteAddr string `json:"ip_address"` + Timestamp int64 `json:"timestamp"` + External bool `json:"external"` +} + +var ( + store *bolt.DB + recordChan chan apiRequest +) + +// Record queues an apiRequest for metrics +func Record(req *http.Request) { + external := strings.Contains(req.URL.Path, "/external/") + + r := apiRequest{ + URL: req.URL.String(), + Method: req.Method, + RemoteAddr: req.RemoteAddr, + Timestamp: time.Now().Unix() * 1000, + External: external, + } + + // put r on buffered recordChan to take advantage of batch insertion in DB + recordChan <- r + +} + +// Close exports the abillity to close our db file. Should be called with defer +// after call to Init() from the same place. +func Close() { + err := store.Close() + if err != nil { + log.Println(err) + } +} + +// Init creates a db connection, should run an initial prune of old data, and +// sets up the queue/batching channel +func Init() { + store, err := bolt.Open("analytics.db", 0666, nil) + if err != nil { + log.Fatalln(err) + } + + recordChan = make(chan apiRequest, 1024*128) + + go serve() + + err = store.Update(func(tx *bolt.Tx) error { + + return nil + }) +} + +func serve() { + // make timer to notify select to batch request insert from recordChan + // interval: 1 minute + apiRequestTimer := time.NewTicker(time.Minute * 1) + + // make timer to notify select to remove old analytics + // interval: 2 weeks + // TODO: enable analytics backup service to cloud + pruneDBTimer := time.NewTicker(time.Hour * 24 * 14) + + for { + select { + case <-apiRequestTimer.C: + var reqs []apiRequest + batchSize := len(recordChan) + + for i := 0; i < batchSize; i++ { + reqs = append(reqs, <-recordChan) + } + + case <-pruneDBTimer.C: + + default: + } + } +} diff --git a/system/api/external.go b/system/api/external.go index e6e75f0..0da7eac 100644 --- a/system/api/external.go +++ b/system/api/external.go @@ -10,6 +10,7 @@ import ( // Externalable accepts or rejects external POST requests to /external/posts?type=Review type Externalable interface { + // Accepts determines whether a type will allow external submissions Accepts() bool } @@ -21,7 +22,7 @@ func externalPostsHandler(res http.ResponseWriter, req *http.Request) { err := req.ParseMultipartForm(1024 * 1024 * 4) // maxMemory 4MB if err != nil { - log.Println("[External]", err) + log.Println("[External] error:", err) res.WriteHeader(http.StatusInternalServerError) return } @@ -34,7 +35,7 @@ func externalPostsHandler(res http.ResponseWriter, req *http.Request) { p, found := content.Types[t] if !found { - log.Println("[External] Attempt to submit content", t, "by", req.RemoteAddr) + log.Println("[External] attempt to submit unknown type:", t, "from:", req.RemoteAddr) res.WriteHeader(http.StatusNotFound) return } @@ -43,15 +44,15 @@ func externalPostsHandler(res http.ResponseWriter, req *http.Request) { ext, ok := post.(Externalable) if !ok { - log.Println("[External]", err) - res.WriteHeader(http.StatusInternalServerError) + log.Println("[External] rejected non-externalable type:", t, "from:", req.RemoteAddr) + res.WriteHeader(http.StatusBadRequest) return } if ext.Accepts() { _, err := db.SetContent(t+"_external"+":-1", req.Form) if err != nil { - log.Println("[External]", err) + log.Println("[External] error:", err) res.WriteHeader(http.StatusInternalServerError) return } diff --git a/system/api/handlers.go b/system/api/handlers.go index a56a667..8356683 100644 --- a/system/api/handlers.go +++ b/system/api/handlers.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/bosssauce/ponzu/content" + "github.com/bosssauce/ponzu/system/api/analytics" "github.com/bosssauce/ponzu/system/db" ) @@ -210,9 +211,6 @@ func SendJSON(res http.ResponseWriter, j map[string]interface{}) { sendData(res, data, 200) } -// ResponseFunc ... -type ResponseFunc func(http.ResponseWriter, *http.Request) - // CORS wraps a HandleFunc to response to OPTIONS requests properly func CORS(next http.HandlerFunc) http.HandlerFunc { return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { @@ -224,3 +222,12 @@ func CORS(next http.HandlerFunc) http.HandlerFunc { next.ServeHTTP(res, req) }) } + +// Record wraps a HandleFunc to record API requests for analytical purposes +func Record(next http.HandlerFunc) http.HandlerFunc { + return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { + go analytics.Record(req) + + next.ServeHTTP(res, req) + }) +} |