-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
154 lines (131 loc) · 4.33 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"path"
"strconv"
"strings"
"time"
models "github.com/moesif/moesifapi-go/models"
moesifmiddleware "github.com/moesif/moesifmiddleware-go"
options "github.com/moesif/moesifmiddleware-go-example/moesif_options"
)
var moesifOption map[string]interface{}
func init() {
moesifOption = options.MoesifOptions()
}
type Employee struct {
DateOfBirth *time.Time `json:"date_of_birth" form:"date_of_birth"` //Time when request was made
Id int `json:"id" form:"id"` //HTTP Status code such as 200
FirstName string `json:"first_name" form:"first_name"` //verb of the API request such as GET or POST
LastName string `json:"last_name" form:"last_name"` //verb of the API request such as GET or POST
}
func ParseID(s string) (id int, err error) {
p := strings.LastIndex(s, "/")
if p < 0 {
return 0, nil
}
first := s[:p+1]
if first != "/api/employee/" {
return 0, nil
}
id, err = strconv.Atoi(s[p+1:])
if err != nil {
return 0, nil
}
return id, nil
}
func literalFieldValue(value string) *string {
return &value
}
func main() {
http.Handle("/api/employee/", moesifmiddleware.MoesifMiddleware(http.HandlerFunc(handle), moesifOption))
http.Handle("/api/users/", moesifmiddleware.MoesifMiddleware(http.HandlerFunc(Usershandle), moesifOption))
log.Println("Listening on port 3000")
err := http.ListenAndServe(":3000", nil)
if err != nil {
log.Fatalf("Could not start server: %s\n", err.Error())
}
}
func handle(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
time := time.Now().UTC().AddDate(-30, 0, 0)
id, _ := ParseID(r.URL.Path)
body, _ := ioutil.ReadAll(r.Body)
log.Printf("Body: %s\n", body)
var employee = Employee{
DateOfBirth: &time,
Id: id,
FirstName: "firstName",
LastName: "lastName",
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(employee)
}
func Usershandle(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
// Campaign object is optional, but useful if you want to track ROI of acquisition channels
// See https://www.moesif.com/docs/api#users for campaign schema
campaign := models.CampaignModel{
UtmSource: literalFieldValue("google"),
UtmMedium: literalFieldValue("cpc"),
UtmCampaign: literalFieldValue("adwords"),
UtmTerm: literalFieldValue("api+tooling"),
UtmContent: literalFieldValue("landing"),
}
// metadata can be any custom dictionary
metadata := map[string]interface{}{
"email": "john@acmeinc.com",
"first_name": "John",
"last_name": "Doe",
"title": "Software Engineer",
"sales_info": map[string]interface{}{
"stage": "Customer",
"lifetime_value": 24000,
"account_owner": "mary@contoso.com",
},
}
userId := path.Base(r.URL.Path)
// Only UserId is required
user := models.UserModel{
UserId: userId,
CompanyId: literalFieldValue("67890"), // If set, associate user with a company object
Campaign: &campaign,
Metadata: &metadata,
}
moesifmiddleware.UpdateUser(&user, moesifOption)
}
func Companieshandle(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
// Campaign object is optional, but useful if you want to track ROI of acquisition channels
// See https://www.moesif.com/docs/api#update-a-company for campaign schema
campaign := models.CampaignModel{
UtmSource: literalFieldValue("google"),
UtmMedium: literalFieldValue("cpc"),
UtmCampaign: literalFieldValue("adwords"),
UtmTerm: literalFieldValue("api+tooling"),
UtmContent: literalFieldValue("landing"),
}
// metadata can be any custom dictionary
metadata := map[string]interface{}{
"org_name": "Acme, Inc",
"plan_name": "Free",
"deal_stage": "Lead",
"mrr": 24000,
"demographics": map[string]interface{}{
"alexa_ranking": 500000,
"employee_count": 47,
},
}
companyId := path.Base(r.URL.Path)
// Prepare company model
company := models.CompanyModel{
CompanyId: companyId, // The only required field is your company id
CompanyDomain: literalFieldValue("acmeinc.com"), // If domain is set, Moesif will enrich your profiles with publicly available info
Campaign: &campaign,
Metadata: &metadata,
}
moesifmiddleware.UpdateCompany(&company, moesifOption)
}