Skip to content

Commit ace4f8e

Browse files
Merge branch 'fix/tag-updates'
2 parents 37bae82 + 46284cd commit ace4f8e

File tree

7 files changed

+58
-92
lines changed

7 files changed

+58
-92
lines changed

apiModels.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

authController.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,7 @@ func tokenAuthMiddleware(jwtSecret string, next http.Handler) http.Handler {
9696
next.ServeHTTP(w, r)
9797
})
9898
}
99+
100+
type clientToken struct {
101+
Token string `json:"token"`
102+
}

main.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ func main() {
3838
if err != nil {
3939
log.Fatal(err)
4040
}
41+
err = db.AutoMigrate(&gormHis{}, &gormRec{})
42+
if err != nil {
43+
log.Fatal(err)
44+
}
4145

4246
// OTEL
4347
metricExporter, err := prometheus.New()
@@ -57,14 +61,20 @@ func main() {
5761
otel.SetMeterProvider(meterProvider) // Sets global
5862
go serveMetrics()
5963

64+
// Stores
65+
historyStore := newGormHistoryStore(db)
66+
recStore := newGormRecStore(db)
67+
currentStore := newInMemoryCurrentStore()
68+
6069
serverConfig := ServerConfig{
6170
username: os.Getenv("USERNAME"),
6271
password: os.Getenv("PASSWORD"),
6372
jwtSecret: os.Getenv("JWT_SECRET"),
6473
tokenDurationSeconds: 60 * 60, // 1 hour
6574

66-
db: db,
67-
dbAutoMigrate: true,
75+
historyStore: historyStore,
76+
recStore: recStore,
77+
currentStore: currentStore,
6878
}
6979
server, err := NewServer(serverConfig)
7080
if err != nil {

recController.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,6 @@ func (recController recController) postRecs(w http.ResponseWriter, r *http.Reque
6161
w.Write(recJSON)
6262
}
6363

64-
// GET /recs/tag/:tag
65-
func (recController recController) getRecsByTag(w http.ResponseWriter, r *http.Request) {
66-
recs, err := recController.store.readRecs("siteMeter")
67-
if err != nil {
68-
log.Printf("Storage Error: %s", err)
69-
w.WriteHeader(http.StatusInternalServerError)
70-
return
71-
}
72-
73-
httpJson, err := json.Marshal(recs)
74-
if err != nil {
75-
log.Printf("Cannot encode response JSON")
76-
w.WriteHeader(http.StatusBadRequest)
77-
return
78-
}
79-
80-
w.WriteHeader(http.StatusOK)
81-
w.Write(httpJson)
82-
}
83-
8464
// GET /recs/:id
8565
func (recController recController) getRec(w http.ResponseWriter, r *http.Request) {
8666
idString := r.PathValue("id")

recStore.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ type recStore interface {
1515
}
1616

1717
type rec struct {
18-
ID uuid.UUID `json:"id"`
19-
Tags datatypes.JSON `json:"tags"`
20-
Dis *string `json:"dis"`
21-
Unit *string `json:"unit"`
18+
ID uuid.UUID `json:"id"`
19+
Tags datatypes.JSONMap `json:"tags"`
20+
Dis *string `json:"dis"`
21+
Unit *string `json:"unit"`
2222
}
2323

2424
// gormHistoryStore stores point historical values in a GORM database.
@@ -85,7 +85,9 @@ func (s gormRecStore) updateRec(
8585
if rec.Unit != nil {
8686
gormRec.Unit = rec.Unit
8787
}
88-
88+
if rec.Tags != nil {
89+
gormRec.Tags = rec.Tags
90+
}
8991
return s.db.Save(&gormRec).Error
9092
}
9193

@@ -94,8 +96,8 @@ func (s gormRecStore) deleteRec(id uuid.UUID) error {
9496
}
9597

9698
type gormRec struct {
97-
ID uuid.UUID `gorm:"column:id;type:uuid;primaryKey:rec_pkey"`
98-
Tags datatypes.JSON `gorm:"type:json"`
99+
ID uuid.UUID `gorm:"column:id;type:uuid;primaryKey:rec_pkey"`
100+
Tags datatypes.JSONMap `gorm:"type:json"`
99101
Dis *string
100102
Unit *string
101103
}

server.go

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"net/http"
55

66
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
7-
"gorm.io/gorm"
87
)
98

109
type ServerConfig struct {
@@ -14,20 +13,13 @@ type ServerConfig struct {
1413
jwtSecret string
1514
tokenDurationSeconds int
1615

17-
// Database
18-
db *gorm.DB
19-
dbAutoMigrate bool
16+
// Stores
17+
historyStore historyStore
18+
recStore recStore
19+
currentStore currentStore
2020
}
2121

2222
func NewServer(serverConfig ServerConfig) (http.Handler, error) {
23-
24-
if serverConfig.dbAutoMigrate {
25-
err := serverConfig.db.AutoMigrate(&gormHis{}, &gormRec{})
26-
if err != nil {
27-
return nil, err
28-
}
29-
}
30-
3123
// Register paths
3224
server := http.NewServeMux()
3325
tokenAuth := http.NewServeMux()
@@ -38,43 +30,19 @@ func NewServer(serverConfig ServerConfig) (http.Handler, error) {
3830
username: serverConfig.username,
3931
password: serverConfig.password,
4032
}
41-
hisController := hisController{store: newGormHistoryStore(serverConfig.db)}
42-
recController := recController{store: newGormRecStore(serverConfig.db)}
43-
currentController := currentController{store: newInMemoryCurrentStore()}
33+
hisController := hisController{store: serverConfig.historyStore}
34+
recController := recController{store: serverConfig.recStore}
35+
currentController := currentController{store: serverConfig.currentStore}
4436

4537
// handleFunc is a replacement for mux.HandleFunc that adds route data to the metrics.
4638
handleFunc := func(mux *http.ServeMux, pattern string, handlerFunc func(http.ResponseWriter, *http.Request)) {
4739
handler := otelhttp.WithRouteTag(pattern, http.HandlerFunc(handlerFunc))
4840
mux.Handle(pattern, handler)
4941
}
5042

51-
// All are deprecated. Instead use /api/*
52-
handleFunc(server, "GET /auth/token", authController.getAuthToken)
53-
handleFunc(tokenAuth, "GET /his/{pointId}", hisController.getHis) // Deprecated
54-
handleFunc(tokenAuth, "POST /his/{pointId}", hisController.postHis) // Deprecated
55-
handleFunc(tokenAuth, "DELETE /his/{pointId}", hisController.deleteHis) // Deprecated
56-
handleFunc(tokenAuth, "GET /recs", recController.getRecs)
57-
handleFunc(tokenAuth, "POST /recs", recController.postRecs)
58-
handleFunc(tokenAuth, "GET /recs/tag/siteMeter", recController.getRecsByTag) // Deprecated. Use /recs?tag=""
59-
handleFunc(tokenAuth, "GET /recs/{id}", recController.getRec)
60-
handleFunc(tokenAuth, "PUT /recs/{id}", recController.putRec)
61-
handleFunc(tokenAuth, "DELETE /recs/{id}", recController.deleteRec)
62-
handleFunc(tokenAuth, "GET /recs/{pointId}/history", hisController.getHis)
63-
handleFunc(tokenAuth, "POST /recs/{pointId}/history", hisController.postHis)
64-
handleFunc(tokenAuth, "DELETE /recs/{pointId}/history", hisController.deleteHis)
65-
handleFunc(tokenAuth, "GET /recs/{pointId}/current", currentController.getCurrent)
66-
handleFunc(tokenAuth, "POST /recs/{pointId}/current", currentController.postCurrent)
67-
server.Handle("/his/", tokenAuthMiddleware(serverConfig.jwtSecret, tokenAuth))
68-
server.Handle("/recs", tokenAuthMiddleware(serverConfig.jwtSecret, tokenAuth))
69-
server.Handle("/recs/", tokenAuthMiddleware(serverConfig.jwtSecret, tokenAuth))
70-
7143
handleFunc(server, "GET /api/auth/token", authController.getAuthToken)
72-
handleFunc(tokenAuth, "GET /api/his/{pointId}", hisController.getHis) // Deprecated
73-
handleFunc(tokenAuth, "POST /api/his/{pointId}", hisController.postHis) // Deprecated
74-
handleFunc(tokenAuth, "DELETE /api/his/{pointId}", hisController.deleteHis) // Deprecated
7544
handleFunc(tokenAuth, "GET /api/recs", recController.getRecs)
7645
handleFunc(tokenAuth, "POST /api/recs", recController.postRecs)
77-
handleFunc(tokenAuth, "GET /api/recs/tag/siteMeter", recController.getRecsByTag) // Deprecated. Use /recs?tag=""
7846
handleFunc(tokenAuth, "GET /api/recs/{id}", recController.getRec)
7947
handleFunc(tokenAuth, "PUT /api/recs/{id}", recController.putRec)
8048
handleFunc(tokenAuth, "DELETE /api/recs/{id}", recController.deleteRec)

server_test.go

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,22 @@ func TestServerTestSuite(t *testing.T) {
3030
func (suite *ServerTestSuite) SetupTest() {
3131
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
3232
assert.Nil(suite.T(), err)
33+
err = db.AutoMigrate(&gormHis{}, &gormRec{})
34+
assert.Nil(suite.T(), err)
35+
36+
historyStore := newGormHistoryStore(db)
37+
recStore := newGormRecStore(db)
38+
currentStore := newInMemoryCurrentStore()
3339

3440
server, err := NewServer(ServerConfig{
3541
username: "test",
3642
password: "password",
3743
jwtSecret: "aaa",
3844
tokenDurationSeconds: 60,
3945

40-
db: db,
41-
dbAutoMigrate: true,
46+
historyStore: historyStore,
47+
recStore: recStore,
48+
currentStore: currentStore,
4249
})
4350
assert.Nil(suite.T(), err)
4451

@@ -186,13 +193,13 @@ func (suite *ServerTestSuite) TestGetRecs() {
186193
{
187194
ID: id1,
188195
Dis: s("rec1"),
189-
Tags: datatypes.JSON([]byte(`{"tag":"value1"}`)),
196+
Tags: datatypes.JSONMap(map[string]interface{}{"tag": "value1"}),
190197
Unit: s("kW"),
191198
},
192199
{
193200
ID: id2,
194201
Dis: s("rec2"),
195-
Tags: datatypes.JSON([]byte(`{"tag":"value2"}`)),
202+
Tags: datatypes.JSONMap(map[string]interface{}{"tag": "value2"}),
196203
Unit: s("lb"),
197204
},
198205
}
@@ -214,13 +221,13 @@ func (suite *ServerTestSuite) TestGetRecs() {
214221
{
215222
ID: id1,
216223
Dis: &rec1,
217-
Tags: datatypes.JSON([]byte(`{"tag":"value1"}`)),
224+
Tags: datatypes.JSONMap(map[string]interface{}{"tag": "value1"}),
218225
Unit: &kW,
219226
},
220227
{
221228
ID: id2,
222229
Dis: &rec2,
223-
Tags: datatypes.JSON([]byte(`{"tag":"value2"}`)),
230+
Tags: datatypes.JSONMap(map[string]interface{}{"tag": "value2"}),
224231
Unit: &lb,
225232
},
226233
},
@@ -234,7 +241,7 @@ func (suite *ServerTestSuite) TestPostRecs() {
234241
rec := rec{
235242
ID: id1,
236243
Dis: &rec1,
237-
Tags: datatypes.JSON([]byte(`{"tag":"value1"}`)),
244+
Tags: datatypes.JSONMap(map[string]interface{}{"tag": "value1"}),
238245
Unit: &kW,
239246
}
240247
authToken := suite.getAuthToken()
@@ -250,7 +257,7 @@ func (suite *ServerTestSuite) TestPostRecs() {
250257
{
251258
ID: id1,
252259
Dis: s("rec1"),
253-
Tags: datatypes.JSON([]byte(`{"tag":"value1"}`)),
260+
Tags: datatypes.JSONMap(map[string]interface{}{"tag": "value1"}),
254261
Unit: s("kW"),
255262
},
256263
},
@@ -264,13 +271,13 @@ func (suite *ServerTestSuite) TestGetRecsByTag() {
264271
{
265272
ID: id1,
266273
Dis: s("rec1"),
267-
Tags: datatypes.JSON([]byte(`{"tag1":"value1"}`)),
274+
Tags: datatypes.JSONMap(map[string]interface{}{"tag1": "value1"}),
268275
Unit: s("kW"),
269276
},
270277
{
271278
ID: id2,
272279
Dis: s("rec2"),
273-
Tags: datatypes.JSON([]byte(`{"tag2":"value2"}`)),
280+
Tags: datatypes.JSONMap(map[string]interface{}{"tag2": "value2"}),
274281
Unit: s("lb"),
275282
},
276283
}
@@ -290,7 +297,7 @@ func (suite *ServerTestSuite) TestGetRecsByTag() {
290297
{
291298
ID: id1,
292299
Dis: &rec1,
293-
Tags: datatypes.JSON([]byte(`{"tag1":"value1"}`)),
300+
Tags: datatypes.JSONMap(map[string]interface{}{"tag1": "value1"}),
294301
Unit: &kW,
295302
},
296303
},
@@ -304,13 +311,13 @@ func (suite *ServerTestSuite) TestGetRec() {
304311
{
305312
ID: id1,
306313
Dis: s("rec1"),
307-
Tags: datatypes.JSON([]byte(`{"tag":"value1"}`)),
314+
Tags: datatypes.JSONMap(map[string]interface{}{"tag": "value1"}),
308315
Unit: s("kW"),
309316
},
310317
{
311318
ID: id2,
312319
Dis: s("rec2"),
313-
Tags: datatypes.JSON([]byte(`{"tag":"value2"}`)),
320+
Tags: datatypes.JSONMap(map[string]interface{}{"tag": "value2"}),
314321
Unit: s("lb"),
315322
},
316323
}
@@ -329,7 +336,7 @@ func (suite *ServerTestSuite) TestGetRec() {
329336
rec{
330337
ID: id2,
331338
Dis: &rec2Dis,
332-
Tags: datatypes.JSON([]byte(`{"tag":"value2"}`)),
339+
Tags: datatypes.JSONMap(map[string]interface{}{"tag": "value2"}),
333340
Unit: &lb,
334341
},
335342
)
@@ -341,7 +348,7 @@ func (suite *ServerTestSuite) TestPutRec() {
341348
{
342349
ID: id,
343350
Dis: s("rec"),
344-
Tags: datatypes.JSON([]byte(`{"tag":"value"}`)),
351+
Tags: datatypes.JSONMap(map[string]interface{}{"tag": "value"}),
345352
Unit: s("kW"),
346353
},
347354
}
@@ -352,7 +359,7 @@ func (suite *ServerTestSuite) TestPutRec() {
352359
rec := rec{
353360
ID: id,
354361
Dis: &dis,
355-
Tags: datatypes.JSON([]byte(`{"tag":"value1"}`)),
362+
Tags: datatypes.JSONMap(map[string]interface{}{"tag": "value1"}),
356363
Unit: &lb,
357364
}
358365

@@ -368,7 +375,7 @@ func (suite *ServerTestSuite) TestPutRec() {
368375
gormRec{
369376
ID: id,
370377
Dis: s("rec updated"),
371-
Tags: datatypes.JSON([]byte(`{"tag":"value"}`)),
378+
Tags: datatypes.JSONMap(map[string]interface{}{"tag": "value1"}),
372379
Unit: s("lb"),
373380
},
374381
)
@@ -380,7 +387,7 @@ func (suite *ServerTestSuite) TestDeleteRec() {
380387
{
381388
ID: id,
382389
Dis: s("rec"),
383-
Tags: datatypes.JSON([]byte(`{"tag":"value"}`)),
390+
Tags: datatypes.JSONMap(map[string]interface{}{"tag": "value"}),
384391
Unit: s("kW"),
385392
},
386393
}
@@ -400,7 +407,7 @@ func (suite *ServerTestSuite) TestCurrent() {
400407
{
401408
ID: id,
402409
Dis: s("rec"),
403-
Tags: datatypes.JSON([]byte(`{"tag":"value"}`)),
410+
Tags: datatypes.JSONMap(map[string]interface{}{"tag": "value"}),
404411
Unit: s("kW"),
405412
},
406413
}

0 commit comments

Comments
 (0)