-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
149 lines (133 loc) · 4.63 KB
/
api.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
package main
import (
"github.com/ant0ine/go-json-rest/rest"
"github.com/jinzhu/gorm"
_ "github.com/lib/pq"
"github.com/yvasiyarov/go-metrics"
"github.com/yvasiyarov/gorelic"
"log"
"net/http"
"os"
"time"
)
func main() {
i := Impl{}
i.InitDB()
i.InitSchema()
api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
api.Use(&NewRelicMiddleware{
License: os.Getenv("NEWRELIC_LICENCE_KEY"),
Name: "Puffin Event API",
Verbose: true,
})
//api.Use(&rest.AccessLogJsonMiddleware{})
router, err := rest.MakeRouter(
rest.Post("/events", i.PostEvent),
rest.Post("/installations", i.PostInstallation),
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
log.Fatal(http.ListenAndServe("0.0.0.0:"+os.Getenv("PORT"), api.MakeHandler()))
}
type NewRelicMiddleware struct {
License string
Name string
Verbose bool
agent *gorelic.Agent
}
func (mw *NewRelicMiddleware) MiddlewareFunc(handler rest.HandlerFunc) rest.HandlerFunc {
mw.agent = gorelic.NewAgent()
mw.agent.NewrelicLicense = mw.License
mw.agent.HTTPTimer = metrics.NewTimer()
mw.agent.Verbose = mw.Verbose
mw.agent.NewrelicName = mw.Name
mw.agent.CollectHTTPStat = true
mw.agent.Run()
return func(writer rest.ResponseWriter, request *rest.Request) {
handler(writer, request)
// the timer middleware keeps track of the time
startTime := request.Env["START_TIME"].(*time.Time)
mw.agent.HTTPTimer.UpdateSince(*startTime)
}
}
type Event struct {
Id int64 `json:"id"`
DeviceId string `sql:"size:40" json:"deviceId"`
MeasuredAt time.Time `json:"measuredAt"`
Measurement string `sql:"size:1024" json:"measurement"`
Value float64 `json:"value"`
Fields string `sql:"size:1024" json:"fields"`
Tags string `sql:"size:1024" json:"tags"`
Note string `sql:"size:1024" json:"note"`
Tenant string `sql:"size:30" json:"tenant"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt time.Time `json:"-"`
NotificationIntervalMinutes int64 `json:"notificationIntervalMinutes"`
NotificationShownAt *time.Time `json:"notificationShownAt"`
NotificationDismissedAt *time.Time `json:"notificationDismissedAt"`
NotificationAcceptedAt *time.Time `json:"notificationAcceptedAt"`
StartAt *time.Time `json:"startAt"`
EndAt *time.Time `json:"EndAt"`
}
type Installation struct {
Id int64 `json:"id"`
DeviceType string `sql:"size:40" json:"deviceType"`
DeviceManufacturer string `sql:"size:40" json:"deviceManufacturer"`
DeviceModel string `sql:"size:40" json:"deviceModel"`
DeviceProduct string `sql:"size:40" json:"deviceProduct"`
DeviceSdk string `sql:"size:40" json:"deviceSdk"`
DeviceSystemLocale string `sql:"size:40" json:"deviceSystemLocale"`
DeviceAdvertisingId string `sql:"size:40" json:"deviceAdverstisingId"`
DeviceMobileCarrier string `sql:"size:40" json:"deviceMobileCarrier"`
AppVersion string `sql:"size:40" json:"appVersion"`
AppVersionCode string `sql:"size:40" json:"appVersionCode"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt time.Time `json:"-"`
}
type Impl struct {
DB gorm.DB
}
func (i *Impl) InitDB() {
var err error
connection := os.Getenv("DATABASE_URL")
i.DB, err = gorm.Open("postgres", connection)
if err != nil {
log.Fatalf("Got error when connect database, the error is '%v'", err)
}
i.DB.LogMode(true)
}
func (i *Impl) InitSchema() {
i.DB.AutoMigrate(&Event{})
i.DB.AutoMigrate(&Installation{})
}
func (i *Impl) PostEvent(w rest.ResponseWriter, r *rest.Request) {
event := Event{}
if err := r.DecodeJsonPayload(&event); err != nil {
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := i.DB.Create(&event).Error; err != nil {
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
w.WriteJson(&event)
}
func (i *Impl) PostInstallation(w rest.ResponseWriter, r *rest.Request) {
installation := Installation{}
if err := r.DecodeJsonPayload(&installation); err != nil {
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := i.DB.Save(&installation).Error; err != nil {
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
w.WriteJson(&installation)
}