-
Notifications
You must be signed in to change notification settings - Fork 2
/
api_app.go
120 lines (95 loc) · 3.94 KB
/
api_app.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
// Copyright © 2014-2023 Thomas Rabaix <thomas.rabaix@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package api
import (
"container/list"
"github.com/gorilla/websocket"
"github.com/lib/pq"
"github.com/rande/goapp"
"github.com/rande/gonode/core/config"
"github.com/rande/gonode/core/security"
"github.com/rande/gonode/modules/base"
log "github.com/sirupsen/logrus"
"github.com/zenazn/goji/graceful"
"github.com/zenazn/goji/web"
)
func Configure(l *goapp.Lifecycle, conf *config.Config) {
l.Prepare(func(app *goapp.App) error {
app.Set("gonode.api", func(app *goapp.App) interface{} {
return &Api{
Manager: app.Get("gonode.manager").(*base.PgNodeManager),
Version: "1.0.0",
Logger: app.Get("logger").(*log.Logger),
Authorizer: app.Get("security.authorizer").(security.AuthorizationChecker),
}
})
app.Set("gonode.websocket.clients", func(app *goapp.App) interface{} {
return list.New()
})
sub := app.Get("gonode.postgres.subscriber").(*base.Subscriber)
sub.ListenMessage(conf.Databases["master"].Prefix+"_manager_action", func(notification *pq.Notification) (int, error) {
logger := app.Get("logger").(*log.Logger)
logger.WithFields(log.Fields{
"module": "api.websocket",
"payload": notification.Extra,
}).Debug("Sending message")
webSocketList := app.Get("gonode.websocket.clients").(*list.List)
for e := webSocketList.Front(); e != nil; e = e.Next() {
if err := e.Value.(*websocket.Conn).WriteMessage(websocket.TextMessage, []byte(notification.Extra)); err != nil {
logger.Warn("Error writing to websocket")
}
}
logger.WithFields(log.Fields{
"module": "api.websocket",
}).Debug("WebSocket: End Sending message")
return base.PubSubListenContinue, nil
})
graceful.PreHook(func() {
logger := app.Get("logger").(*log.Logger)
webSocketList := app.Get("gonode.websocket.clients").(*list.List)
logger.WithFields(log.Fields{
"module": "api.websocket",
}).Info("Closing websocket connections")
for e := webSocketList.Front(); e != nil; e = e.Next() {
e.Value.(*websocket.Conn).Close()
}
})
mux := app.Get("goji.mux").(*web.Mux)
mux.Get(conf.Api.Prefix+"/:version/nodes/stream", Api_GET_Stream(app))
mux.Get(conf.Api.Prefix+"/:version/nodes/:uuid", Api_GET_Node(app))
mux.Get(conf.Api.Prefix+"/:version/nodes/:uuid/revisions", Api_GET_Node_Revisions(app))
mux.Get(conf.Api.Prefix+"/:version/nodes/:uuid/revisions/:rev", Api_GET_Node_Revision(app))
mux.Post(conf.Api.Prefix+"/:version/nodes", Api_POST_Nodes(app))
mux.Put(conf.Api.Prefix+"/:version/nodes/:uuid", Api_PUT_Nodes(app))
mux.Put(conf.Api.Prefix+"/:version/nodes/move/:uuid/:parentUuid", Api_PUT_Nodes_Move(app))
mux.Delete(conf.Api.Prefix+"/:version/nodes/:uuid", Api_DELETE_Nodes(app))
mux.Get(conf.Api.Prefix+"/:version/nodes", Api_GET_Nodes(app))
mux.Get(conf.Api.Prefix+"/:version/hello", Api_GET_Hello(app))
mux.Put(conf.Api.Prefix+"/:version/notify/:name", Api_PUT_Notify(app))
mux.Get(conf.Api.Prefix+"/:version/handlers/node", Api_GET_Handlers_Node(app))
mux.Get(conf.Api.Prefix+"/:version/handlers/view", Api_GET_Handlers_View(app))
mux.Get(conf.Api.Prefix+"/:version/services", Api_GET_Services(app))
return nil
})
l.Run(func(app *goapp.App, state *goapp.GoroutineState) error {
logger := app.Get("logger").(*log.Logger)
logger.WithFields(log.Fields{
"module": "api.websocket",
}).Debug("Starting PostgreSQL subcriber")
app.Get("gonode.postgres.subscriber").(*base.Subscriber).Register()
return nil
})
l.Exit(func(app *goapp.App) error {
logger := app.Get("logger").(*log.Logger)
logger.WithFields(log.Fields{
"module": "api.websocket",
}).Debug("Closing PostgreSQL subcriber")
app.Get("gonode.postgres.subscriber").(*base.Subscriber).Stop()
logger.WithFields(log.Fields{
"module": "api.websocket",
}).Debug("End closing PostgreSQL subcriber")
return nil
})
}