-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·112 lines (88 loc) · 2.85 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"time"
"github.com/pirsch-analytics/pirsch/v4/db"
"github.com/pirsch-analytics/pirsch/v4/tracker"
"github.com/pirsch-analytics/pirsch/v4/tracker/session"
)
// For more details, take a look at the backend demo and documentation.
func main() {
time.Sleep(10 * time.Second)
// Set the key for SipHash.
tracker.SetFingerprintKeys(42, 123)
dbConfig := &db.ClientConfig{
Hostname: os.Getenv("CLICKHOUSE_HOST"),
Port: 9000,
Database: os.Getenv("CLICKHOUSE_DB"),
Username: os.Getenv("CLICKHOUSE_USER"),
Password: os.Getenv("CLICKHOUSE_PASSWORD"),
SSLSkipVerify: true,
Debug: false,
}
if err := db.Migrate(dbConfig); err != nil {
panic(err)
}
store, err := db.NewClient(dbConfig)
if err != nil {
panic(err)
}
pirschTracker := tracker.NewTracker(store, "salt", &tracker.Config{
SessionCache: session.NewMemCache(store, 100),
})
// Create an endpoint to handle client tracking requests.
// HitOptionsFromRequest is a utility function to process the required parameters.
// You might want to additional checks, like for the client ID.
http.Handle("/count", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// We don't need to call Hit in a new goroutine, as this is the only call in the handler
// (running in its own goroutine already).
pirschTracker.Hit(r, tracker.HitOptionsFromRequest(r))
log.Println("Counted one hit")
}))
// Create an endpoint to handle client event requests.
http.Handle("/event", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req struct {
Name string `json:"event_name"`
Duration uint32 `json:"event_duration"`
Meta map[string]string `json:"event_meta"`
}
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&req); err != nil {
log.Printf("Error decoding event request: %s", err)
return
}
data := tracker.EventOptions{
Name: req.Name,
Duration: req.Duration,
Meta: req.Meta,
}
pirschTracker.Event(r, data, tracker.HitOptionsFromRequest(r))
log.Println("Received event")
}))
// Add a handler to serve index.html and pirsch.js.
http.Handle("/", http.FileServer(http.Dir("./")))
log.Println("Started server on http://localhost:8080")
log.Fatal(http.ListenAndServeTLS(":443", "/etc/ssl/certs/selfsigned.crt", "/etc/ssl/private/selfsigned.key", nil))
// log.Fatal(http.ListenAndServe(":8080", nil))
}
func copyPirschJs() {
content, err := os.ReadFile("../../js/pirsch.js")
if err != nil {
panic(err)
}
if err := os.WriteFile("pirsch.js", content, 0755); err != nil {
panic(err)
}
}
func copyPirschEventsJs() {
content, err := os.ReadFile("../../js/pirsch-events.js")
if err != nil {
panic(err)
}
if err := os.WriteFile("pirsch-events.js", content, 0755); err != nil {
panic(err)
}
}