2016-12-08 20:45:26 +01:00
|
|
|
package commands
|
|
|
|
|
2016-12-11 14:50:01 +01:00
|
|
|
import (
|
2016-12-24 11:00:29 +02:00
|
|
|
"fmt"
|
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
"github.com/gorilla/mux"
|
2018-04-24 10:28:23 +02:00
|
|
|
"github.com/usefathom/fathom/pkg/api"
|
2017-01-25 15:17:24 +01:00
|
|
|
"log"
|
2017-01-24 20:28:22 +01:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2016-12-08 20:45:26 +01:00
|
|
|
)
|
|
|
|
|
2017-01-24 20:28:22 +01:00
|
|
|
// Server starts the HTTP server, listening on the given port
|
|
|
|
func Server(port int) {
|
|
|
|
|
2016-12-11 14:50:01 +01:00
|
|
|
// register routes
|
|
|
|
r := mux.NewRouter()
|
2017-01-25 22:48:24 +01:00
|
|
|
r.Handle("/collect", api.NewCollectHandler()).Methods("GET")
|
2016-12-11 14:50:01 +01:00
|
|
|
r.Handle("/api/session", api.LoginHandler).Methods("POST")
|
|
|
|
r.Handle("/api/session", api.LogoutHandler).Methods("DELETE")
|
|
|
|
r.Handle("/api/visitors/count", api.Authorize(api.GetVisitorsCountHandler)).Methods("GET")
|
|
|
|
r.Handle("/api/visitors/count/group/{period}", api.Authorize(api.GetVisitorsPeriodCountHandler)).Methods("GET")
|
|
|
|
r.Handle("/api/visitors/count/realtime", api.Authorize(api.GetVisitorsRealtimeCountHandler)).Methods("GET")
|
|
|
|
r.Handle("/api/pageviews/count", api.Authorize(api.GetPageviewsCountHandler)).Methods("GET")
|
|
|
|
r.Handle("/api/pageviews/count/group/{period}", api.Authorize(api.GetPageviewsPeriodCountHandler)).Methods("GET")
|
|
|
|
r.Handle("/api/pageviews", api.Authorize(api.GetPageviewsHandler)).Methods("GET")
|
|
|
|
r.Handle("/api/languages", api.Authorize(api.GetLanguagesHandler)).Methods("GET")
|
|
|
|
r.Handle("/api/referrers", api.Authorize(api.GetReferrersHandler)).Methods("GET")
|
|
|
|
r.Handle("/api/screen-resolutions", api.Authorize(api.GetScreenResolutionsHandler)).Methods("GET")
|
2016-12-24 15:11:36 +02:00
|
|
|
//r.Handle("/api/countries", api.Authorize(api.GetCountriesHandler)).Methods("GET")
|
2016-12-11 14:50:01 +01:00
|
|
|
r.Handle("/api/browsers", api.Authorize(api.GetBrowsersHandler)).Methods("GET")
|
2016-12-08 20:45:26 +01:00
|
|
|
|
2018-04-20 13:12:23 +02:00
|
|
|
r.Path("/tracker.js").Handler(http.FileServer(http.Dir("./assets/dist/js/")))
|
2016-12-08 20:45:26 +01:00
|
|
|
|
2018-04-20 13:12:23 +02:00
|
|
|
r.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("./assets/dist/"))))
|
|
|
|
|
|
|
|
log.Printf("HTTP server will now start listening on http://127.0.01:%d/\n", port)
|
2017-01-24 20:28:22 +01:00
|
|
|
err := http.ListenAndServe(fmt.Sprintf(":%d", port), handlers.LoggingHandler(os.Stdout, r))
|
2017-01-25 15:17:24 +01:00
|
|
|
log.Println(err)
|
2016-12-08 20:45:26 +01:00
|
|
|
}
|