2018-05-06 11:53:19 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2018-08-08 09:43:06 +02:00
|
|
|
"net/http"
|
|
|
|
|
2018-05-08 14:57:08 +02:00
|
|
|
"github.com/gobuffalo/packr"
|
2018-05-06 11:53:19 +02:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
2018-05-15 13:30:37 +02:00
|
|
|
func (api *API) Routes() *mux.Router {
|
2018-05-06 11:53:19 +02:00
|
|
|
// register routes
|
|
|
|
r := mux.NewRouter()
|
2018-08-08 09:43:06 +02:00
|
|
|
r.Handle("/collect", NewCollector(api.database)).Methods(http.MethodGet)
|
2018-09-12 09:36:59 +02:00
|
|
|
|
2018-10-05 13:09:58 +02:00
|
|
|
r.Handle("/api/session", HandlerFunc(api.GetSession)).Methods(http.MethodGet)
|
|
|
|
r.Handle("/api/session", HandlerFunc(api.CreateSession)).Methods(http.MethodPost)
|
|
|
|
r.Handle("/api/session", HandlerFunc(api.DeleteSession)).Methods(http.MethodDelete)
|
|
|
|
|
2018-10-04 13:40:52 +02:00
|
|
|
r.Handle("/api/sites", api.Authorize(HandlerFunc(api.GetSitesHandler))).Methods(http.MethodGet)
|
|
|
|
r.Handle("/api/sites", api.Authorize(HandlerFunc(api.SaveSiteHandler))).Methods(http.MethodPost)
|
|
|
|
r.Handle("/api/sites/{id:[0-9]+}", api.Authorize(HandlerFunc(api.SaveSiteHandler))).Methods(http.MethodPost)
|
|
|
|
r.Handle("/api/sites/{id:[0-9]+}", api.Authorize(HandlerFunc(api.DeleteSiteHandler))).Methods(http.MethodDelete)
|
2018-10-03 11:07:04 +02:00
|
|
|
|
2018-10-05 13:09:58 +02:00
|
|
|
r.Handle("/api/sites/{id:[0-9]+}/stats/site", api.Authorize(HandlerFunc(api.GetSiteStatsHandler))).Methods(http.MethodGet)
|
2018-11-14 11:47:47 +01:00
|
|
|
r.Handle("/api/sites/{id:[0-9]+}/stats/site/agg", api.Authorize(HandlerFunc(api.GetAggregatedSiteStatsHandler))).Methods(http.MethodGet)
|
2018-10-05 13:09:58 +02:00
|
|
|
r.Handle("/api/sites/{id:[0-9]+}/stats/site/realtime", api.Authorize(HandlerFunc(api.GetSiteStatsRealtimeHandler))).Methods(http.MethodGet)
|
2018-05-06 11:53:19 +02:00
|
|
|
|
2018-11-14 11:47:47 +01:00
|
|
|
r.Handle("/api/sites/{id:[0-9]+}/stats/pages/agg", api.Authorize(HandlerFunc(api.GetAggregatedPageStatsHandler))).Methods(http.MethodGet)
|
|
|
|
r.Handle("/api/sites/{id:[0-9]+}/stats/pages/agg/pageviews", api.Authorize(HandlerFunc(api.GetAggregatedPageStatsPageviewsHandler))).Methods(http.MethodGet)
|
2018-05-11 15:49:37 +02:00
|
|
|
|
2018-11-14 11:47:47 +01:00
|
|
|
r.Handle("/api/sites/{id:[0-9]+}/stats/referrers/agg", api.Authorize(HandlerFunc(api.GetAggregatedReferrerStatsHandler))).Methods(http.MethodGet)
|
|
|
|
r.Handle("/api/sites/{id:[0-9]+}/stats/referrers/agg/pageviews", api.Authorize(HandlerFunc(api.GetAggregatedReferrerStatsPageviewsHandler))).Methods(http.MethodGet)
|
2018-05-07 17:01:20 +02:00
|
|
|
|
2018-09-17 17:20:48 +02:00
|
|
|
r.Handle("/health", HandlerFunc(api.Health)).Methods(http.MethodGet)
|
|
|
|
|
2018-05-14 12:08:18 +02:00
|
|
|
// static assets & 404 handler
|
2018-05-29 14:52:34 +02:00
|
|
|
box := packr.NewBox("./../../assets/build")
|
2018-06-13 11:15:18 +02:00
|
|
|
r.Path("/tracker.js").Handler(serveTrackerFile(&box))
|
2018-05-14 12:08:18 +02:00
|
|
|
r.Path("/").Handler(serveFileHandler(&box, "index.html"))
|
2018-05-14 11:17:41 +02:00
|
|
|
r.PathPrefix("/assets").Handler(http.StripPrefix("/assets", http.FileServer(box)))
|
2018-05-14 12:08:18 +02:00
|
|
|
r.NotFoundHandler = NotFoundHandler(&box)
|
2018-05-14 11:17:41 +02:00
|
|
|
|
2018-05-14 12:08:18 +02:00
|
|
|
return r
|
2018-05-14 11:17:41 +02:00
|
|
|
}
|
2018-06-13 11:15:18 +02:00
|
|
|
|
|
|
|
func serveTrackerFile(box *packr.Box) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Tk", "N")
|
|
|
|
next := serveFile(box, "js/tracker.js")
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|