Keep It Simple Stupid Stats (KISSS)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

43 lines
1.2 KiB

package main
import (
"github.com/gorilla/handlers"
"net/http"
)
func initTrackingRouter() {
corsHandler := handlers.CORS(handlers.AllowedOrigins([]string{"*"}))
viewRouter := app.router.Path("/view").Subrouter()
viewRouter.Use(corsHandler)
viewRouter.Path("").HandlerFunc(TrackingHandler)
scriptRouter := app.router.Path("/kis3.js").Subrouter()
scriptRouter.Use(corsHandler)
scriptRouter.HandleFunc("", TrackingScriptHandler)
}
func TrackingHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate, max-age=0")
url := r.URL.Query().Get("url")
ref := r.URL.Query().Get("ref")
ua := r.Header.Get("User-Agent")
if !(r.Header.Get("DNT") == "1" && appConfig.Dnt) {
go trackView(url, ref, ua) // run with goroutine for awesome speed!
}
w.WriteHeader(201)
}
func TrackingScriptHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/javascript")
w.Header().Set("Cache-Control", "public, max-age=432000") // 5 days
filename := "kis3.js"
file, err := app.staticBox.Open(filename)
defer func() { _ = file.Close() }()
if err != nil {
return
}
stat, err := file.Stat()
if err != nil {
return
}
http.ServeContent(w, r, filename, stat.ModTime(), file)
}