From d784a26953d0ac17f6e7ae5217593f9cc4b09fe9 Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Sun, 25 Jan 2015 22:45:34 +0100 Subject: [PATCH 01/25] Fixed static content path --- service/stream/router/default.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/stream/router/default.go b/service/stream/router/default.go index acdcac1..6777cde 100644 --- a/service/stream/router/default.go +++ b/service/stream/router/default.go @@ -6,7 +6,7 @@ import ( ) func init() { - feedify.SetStaticPath("/static", "stream/static") + feedify.SetStaticPath("/static", "service/stream/static") feedify.Router("/lp/join", &channel.LongPollingController{}, "get:Join") feedify.Router("/lp/post", &channel.LongPollingController{}) From af80703d18ae335d2dea5df5111007c896ca0496 Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Mon, 26 Jan 2015 20:00:13 +0100 Subject: [PATCH 02/25] UnixNano() time accuracy --- service/stream/controller/room/feed.go | 4 ++-- service/stream/model/event.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/service/stream/controller/room/feed.go b/service/stream/controller/room/feed.go index 7c3bc16..57de598 100644 --- a/service/stream/controller/room/feed.go +++ b/service/stream/controller/room/feed.go @@ -17,7 +17,7 @@ type Subscription struct { } func NewEvent(ep model.EventType, user, msg string) model.Event { - return model.Event{ep, user, int(time.Now().Unix()), msg} + return model.Event{ep, user, time.Now().UnixNano(), msg} } func Join(user string, ws *websocket.Conn) { @@ -53,7 +53,7 @@ func FeedManager() { Publish <- NewEvent(model.EVENT_JOIN, sub.Name, "") case client := <-System_rpc: - data, _ := json.Marshal(&model.Event{model.EVENT_MESSAGE, "system", int(time.Now().Unix()), "ok"}) + data, _ := json.Marshal(&model.Event{model.EVENT_MESSAGE, "system", time.Now().UnixNano(), "ok"}) client.WriteMessage(websocket.TextMessage, data) case event := <-Publish: diff --git a/service/stream/model/event.go b/service/stream/model/event.go index 9abac77..eb5fd62 100644 --- a/service/stream/model/event.go +++ b/service/stream/model/event.go @@ -15,11 +15,11 @@ const ( type Event struct { Type EventType User string - Timestamp int + Timestamp int64 Content string } -const archiveSize = 1 +const archiveSize = 100 var archive = list.New() @@ -34,7 +34,7 @@ func GetEvents(lastReceived int) []Event { events := make([]Event, 0, archive.Len()) for event := archive.Front(); event != nil; event = event.Next() { e := event.Value.(Event) - if e.Timestamp > int(lastReceived) { + if e.Timestamp > int64(lastReceived) { events = append(events, e) } } From 448a299efccf007372327b9995a7d5f3eef91b46 Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Mon, 26 Jan 2015 20:03:03 +0100 Subject: [PATCH 03/25] Renamed rpc to P2P --- service/stream/controller/channel/websocket.go | 2 +- service/stream/controller/room/feed.go | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/service/stream/controller/channel/websocket.go b/service/stream/controller/channel/websocket.go index 11282ce..a54645b 100644 --- a/service/stream/controller/channel/websocket.go +++ b/service/stream/controller/channel/websocket.go @@ -39,6 +39,6 @@ func (this *WebSocketController) Join() { } room.Publish <- room.NewEvent(model.EVENT_MESSAGE, uname, string(p)) - room.System_rpc <- ws + room.P2P <- ws } } diff --git a/service/stream/controller/room/feed.go b/service/stream/controller/room/feed.go index 57de598..9df78e3 100644 --- a/service/stream/controller/room/feed.go +++ b/service/stream/controller/room/feed.go @@ -37,8 +37,7 @@ var ( Subscribe = make(chan Subscriber, 10) Unsubscribe = make(chan string, 10) Publish = make(chan model.Event, 10) - - System_rpc = make(chan *websocket.Conn, 10) + P2P = make(chan *websocket.Conn, 10) WaitingList = list.New() Subscribers = list.New() @@ -52,7 +51,7 @@ func FeedManager() { Subscribers.PushBack(sub) Publish <- NewEvent(model.EVENT_JOIN, sub.Name, "") - case client := <-System_rpc: + case client := <-P2P: data, _ := json.Marshal(&model.Event{model.EVENT_MESSAGE, "system", time.Now().UnixNano(), "ok"}) client.WriteMessage(websocket.TextMessage, data) From e14104f425f11d1d35bccea3f18d5068a885af7d Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Mon, 26 Jan 2015 21:12:40 +0100 Subject: [PATCH 04/25] Cleaned up streaming platform --- service/stream/controller/channel/long_pooling.go | 5 +++++ service/stream/controller/channel/websocket.go | 4 ++-- service/stream/controller/room/feed.go | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/service/stream/controller/channel/long_pooling.go b/service/stream/controller/channel/long_pooling.go index efcf595..ec7c6a0 100644 --- a/service/stream/controller/channel/long_pooling.go +++ b/service/stream/controller/channel/long_pooling.go @@ -27,6 +27,11 @@ func (this *LongPollingController) Post() { return } + // Feature: + // or specific request for this client; + // should be executed and returned directly to user + // lastReceived time should not be changed in that case + room.Publish <- room.NewEvent(model.EVENT_MESSAGE, uname, content) } diff --git a/service/stream/controller/channel/websocket.go b/service/stream/controller/channel/websocket.go index a54645b..302aa90 100644 --- a/service/stream/controller/channel/websocket.go +++ b/service/stream/controller/channel/websocket.go @@ -33,12 +33,12 @@ func (this *WebSocketController) Join() { defer room.Leave(uname) for { - _, p, err := ws.ReadMessage() + _, p, err := ws.ReadMessage()a if err != nil { return } - room.Publish <- room.NewEvent(model.EVENT_MESSAGE, uname, string(p)) + room.Publish <- room.NewEvent(model.EVENT_MESSAGE, uname, string(p)) room.P2P <- ws } } diff --git a/service/stream/controller/room/feed.go b/service/stream/controller/room/feed.go index 9df78e3..e47ba86 100644 --- a/service/stream/controller/room/feed.go +++ b/service/stream/controller/room/feed.go @@ -37,7 +37,7 @@ var ( Subscribe = make(chan Subscriber, 10) Unsubscribe = make(chan string, 10) Publish = make(chan model.Event, 10) - P2P = make(chan *websocket.Conn, 10) + P2P = make(chan *websocket.Conn, 10) WaitingList = list.New() Subscribers = list.New() From fa547fc6058acd9baf257524a777a733fa4cc77d Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Mon, 26 Jan 2015 21:13:00 +0100 Subject: [PATCH 05/25] Cleaned up JS client --- service/stream/static/elasticfeed.js | 84 +++++++++++++--------------- service/stream/static/lib/channel.js | 35 ++++++++++++ service/stream/static/lp.html | 2 +- service/stream/static/ws.html | 2 +- 4 files changed, 75 insertions(+), 48 deletions(-) diff --git a/service/stream/static/elasticfeed.js b/service/stream/static/elasticfeed.js index c910566..531db87 100644 --- a/service/stream/static/elasticfeed.js +++ b/service/stream/static/elasticfeed.js @@ -21,63 +21,55 @@ includeJs('lib/event.js'); var elasticfeed = { /** @type {Object} */ - channel: null, + channelList: {}, - getFeed: function(id) { - return Feed; - }, + /** @type {Object} */ + feedList: {}, + + /** + * Returns Feed object + * @param options + * @returns {*} + */ + getFeed: function(options) { + if (options.id == undefined) { + return null; + } - getChannel: function() { - if (this.channel == null) { - this.channel = new Channel() + if (this.feedList[id] == undefined) { + this.feedList[id] = new Feed(options) } - return this.channel; + return this.feedList[options.id]; }, - newFeed: function(options) { - - }, + /** + * Returns Channel defined per API url + * @param options + * @param credential + * @returns {*} + */ + getChannel: function(options, credential) { + if (options.url == undefined) { + return null; + } - newChannel: function(options) { + if (this.channelList[options.url] == undefined) { + this.channelList[options.url] = new Channel(options, credential) + } + return this.channelList[options.url]; }, - load: function(url, callback) { - var xhr; - if (typeof XMLHttpRequest !== 'undefined') { - xhr = new XMLHttpRequest(); - } else { - var versions = ["MSXML2.XmlHttp.5.0", "MSXML2.XmlHttp.4.0", "MSXML2.XmlHttp.3.0", "MSXML2.XmlHttp.2.0", "Microsoft.XmlHttp"]; - for (var i = 0, len = versions.length; i < len; i++) { - try { - xhr = new ActiveXObject(versions[i]); - break; - } - catch (e) { - } - } - } - xhr.onreadystatechange = ensureReadiness; - function ensureReadiness() { - if (xhr.readyState < 4) { - return; - } - if (xhr.status !== 200) { - return; - } - if (xhr.readyState === 4) { - callback(xhr); - } - } + findFeed: function(id) { + return this.getFeed({id: id}); + }, - xhr.open('GET', url, true); - xhr.send(''); + findChannel: function(url) { + return this.getChannel({url: url}); } - }; // elasticfeed - - // =========================================================================== + }; if ("function" === typeof define) { define(function(require) { @@ -86,15 +78,15 @@ includeJs('lib/event.js'); } else { window.elasticfeed = elasticfeed; } -}(window)); +}(window)); // Helpers -// ======= Element.prototype.remove = function() { this.parentElement.removeChild(this); }; + NodeList.prototype.remove = HTMLCollection.prototype.remove = function() { for (var i = 0, len = this.length; i < len; i++) { if (this[i] && this[i].parentElement) { diff --git a/service/stream/static/lib/channel.js b/service/stream/static/lib/channel.js index ce68ecd..a994a07 100644 --- a/service/stream/static/lib/channel.js +++ b/service/stream/static/lib/channel.js @@ -26,6 +26,9 @@ var Channel = (function() { /** @type {String} */ this.id = _uniqueId(); + /** @type {String} */ + this.url = null + /** @type {Object} */ this.options = _extend(defaultOptions, options); @@ -132,6 +135,38 @@ var Channel = (function() { }; } + Channel.prototype.load = function(url, callback) { + var xhr; + if (typeof XMLHttpRequest !== 'undefined') { + xhr = new XMLHttpRequest(); + } else { + var versions = ["MSXML2.XmlHttp.5.0", "MSXML2.XmlHttp.4.0", "MSXML2.XmlHttp.3.0", "MSXML2.XmlHttp.2.0", "Microsoft.XmlHttp"]; + for (var i = 0, len = versions.length; i < len; i++) { + try { + xhr = new ActiveXObject(versions[i]); + break; + } + catch (e) { + } + } + } + xhr.onreadystatechange = ensureReadiness; + function ensureReadiness() { + if (xhr.readyState < 4) { + return; + } + if (xhr.status !== 200) { + return; + } + if (xhr.readyState === 4) { + callback(xhr); + } + } + + xhr.open('GET', url, true); + xhr.send(''); + } + // HTTP Channel.prototype.getJSON = function(url, callback) { diff --git a/service/stream/static/lp.html b/service/stream/static/lp.html index cc6b1ac..868efa1 100644 --- a/service/stream/static/lp.html +++ b/service/stream/static/lp.html @@ -4,7 +4,7 @@ diff --git a/service/stream/static/ws.html b/service/stream/static/ws.html index aa1c075..09131b2 100644 --- a/service/stream/static/ws.html +++ b/service/stream/static/ws.html @@ -4,7 +4,7 @@ From 3cafe74a92af932ab460c028fd744fb9783e277a Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Mon, 26 Jan 2015 21:26:59 +0100 Subject: [PATCH 06/25] Formatting; Naming --- .../stream/controller/channel/long_pooling.go | 14 ++++----- .../stream/controller/channel/websocket.go | 12 +++---- service/stream/controller/room/feed.go | 31 +++++++++---------- service/stream/static/lib/channel.js | 10 ++++-- 4 files changed, 35 insertions(+), 32 deletions(-) diff --git a/service/stream/controller/channel/long_pooling.go b/service/stream/controller/channel/long_pooling.go index ec7c6a0..3232fce 100644 --- a/service/stream/controller/channel/long_pooling.go +++ b/service/stream/controller/channel/long_pooling.go @@ -12,18 +12,18 @@ type LongPollingController struct { } func (this *LongPollingController) Join() { - uname := this.GetString("uname") - if len(uname) == 0 { + chid := this.GetString("chid") + if len(chid) == 0 { return } - room.Join(uname, nil) + room.Join(chid, nil) } func (this *LongPollingController) Post() { - uname := this.GetString("uname") - content := this.GetString("content") - if len(uname) == 0 || len(content) == 0 { + chid := this.GetString("chid") + data := this.GetString("data") + if len(chid) == 0 || len(data) == 0 { return } @@ -32,7 +32,7 @@ func (this *LongPollingController) Post() { // should be executed and returned directly to user // lastReceived time should not be changed in that case - room.Publish <- room.NewEvent(model.EVENT_MESSAGE, uname, content) + room.Publish <- room.NewEvent(model.EVENT_MESSAGE, chid, data) } func (this *LongPollingController) Fetch() { diff --git a/service/stream/controller/channel/websocket.go b/service/stream/controller/channel/websocket.go index 302aa90..c5971ee 100644 --- a/service/stream/controller/channel/websocket.go +++ b/service/stream/controller/channel/websocket.go @@ -15,8 +15,8 @@ type WebSocketController struct { } func (this *WebSocketController) Join() { - uname := this.GetString("uname") - if len(uname) == 0 { + chid := this.GetString("chid") + if len(chid) == 0 { return } @@ -29,16 +29,16 @@ func (this *WebSocketController) Join() { return } - room.Join(uname, ws) - defer room.Leave(uname) + room.Join(chid, ws) + defer room.Leave(chid) for { - _, p, err := ws.ReadMessage()a + _, p, err := ws.ReadMessage() if err != nil { return } - room.Publish <- room.NewEvent(model.EVENT_MESSAGE, uname, string(p)) + room.Publish <- room.NewEvent(model.EVENT_MESSAGE, chid, string(p)) room.P2P <- ws } } diff --git a/service/stream/controller/room/feed.go b/service/stream/controller/room/feed.go index e47ba86..a993324 100644 --- a/service/stream/controller/room/feed.go +++ b/service/stream/controller/room/feed.go @@ -11,11 +11,26 @@ import ( "github.com/feedlabs/elasticfeed/service/stream/model" ) +var ( + Subscribe = make(chan Subscriber, 10) + Unsubscribe = make(chan string, 10) + Publish = make(chan model.Event, 10) + P2P = make(chan *websocket.Conn, 10) + + WaitingList = list.New() + Subscribers = list.New() +) + type Subscription struct { Archive []model.Event New <-chan model.Event } +type Subscriber struct { + Name string + Conn *websocket.Conn +} + func NewEvent(ep model.EventType, user, msg string) model.Event { return model.Event{ep, user, time.Now().UnixNano(), msg} } @@ -28,21 +43,6 @@ func Leave(user string) { Unsubscribe <- user } -type Subscriber struct { - Name string - Conn *websocket.Conn -} - -var ( - Subscribe = make(chan Subscriber, 10) - Unsubscribe = make(chan string, 10) - Publish = make(chan model.Event, 10) - P2P = make(chan *websocket.Conn, 10) - - WaitingList = list.New() - Subscribers = list.New() -) - func FeedManager() { for { select { @@ -83,7 +83,6 @@ func FeedManager() { } } - func broadcastWebSocket(event model.Event) { data, err := json.Marshal(event) if err != nil { diff --git a/service/stream/static/lib/channel.js b/service/stream/static/lib/channel.js index a994a07..04a5e5f 100644 --- a/service/stream/static/lib/channel.js +++ b/service/stream/static/lib/channel.js @@ -36,6 +36,10 @@ var Channel = (function() { this.id = this.options.id; } + if (this.options.url != null) { + this.url = this.options.url; + } + /** @type {Object} */ this.credential = _extend(defaultCredential, credential); @@ -75,7 +79,7 @@ var Channel = (function() { } Channel.prototype.getWebSocketConnection = function() { - this._socket = new WebSocket('ws://localhost:10100/ws/join?uname=' + this.id); + this._socket = new WebSocket('ws://localhost:10100/ws/join?chid=' + this.id); self = this this._socket.onmessage = function(event) { @@ -95,7 +99,7 @@ var Channel = (function() { var lastReceived = 0; var isWait = false; - this.getJSON('http://localhost:10100/lp/join?uname=' + this.id, function() { + this.getJSON('http://localhost:10100/lp/join?chid=' + this.id, function() { }) self = this; @@ -129,7 +133,7 @@ var Channel = (function() { return { send: function(data) { - self.post("/lp/post", {uname: self.id, content: JSON.stringify(data)}, function(status) { + self.post("/lp/post", {chid: self.id, data: JSON.stringify(data)}, function(status) { }); } }; From f0f5ababa2c50062567912f8ab9d314ca6557d05 Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Mon, 26 Jan 2015 21:39:35 +0100 Subject: [PATCH 07/25] Introduced ChannelEvent and SystemEvent --- service/stream/static/elasticfeed.js | 3 +- service/stream/static/lib/channel.js | 8 ++-- service/stream/static/lib/event/channel.js | 48 +++++++++++++++++++ .../static/lib/{event.js => event/system.js} | 10 ++-- 4 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 service/stream/static/lib/event/channel.js rename service/stream/static/lib/{event.js => event/system.js} (78%) diff --git a/service/stream/static/elasticfeed.js b/service/stream/static/elasticfeed.js index 531db87..d825c33 100644 --- a/service/stream/static/elasticfeed.js +++ b/service/stream/static/elasticfeed.js @@ -14,7 +14,8 @@ function includeJs(jsFilePath) { includeJs('lib/feed.js'); includeJs('lib/entry.js'); includeJs('lib/channel.js'); -includeJs('lib/event.js'); +includeJs('lib/event/channel.js'); +includeJs('lib/event/system.js'); (function(window) { diff --git a/service/stream/static/lib/channel.js b/service/stream/static/lib/channel.js index 04a5e5f..38babdc 100644 --- a/service/stream/static/lib/channel.js +++ b/service/stream/static/lib/channel.js @@ -48,7 +48,7 @@ var Channel = (function() { } /** - * @param {StreamEvent} event + * @param {ChannelEvent} event * @param {Function} callback */ Channel.prototype.registerHandler = function(options, callback) { @@ -62,7 +62,7 @@ var Channel = (function() { } /** - * @param {StreamEvent} event + * @param {ChannelEvent} event */ Channel.prototype.onData = function(event) { for (var i in this._handlers) { @@ -83,7 +83,7 @@ var Channel = (function() { self = this this._socket.onmessage = function(event) { - event = new StreamEvent(JSON.parse(event.data)) + event = new ChannelEvent(JSON.parse(event.data)) self.onData(event) }; @@ -119,7 +119,7 @@ var Channel = (function() { } self.each(data, function(i, event) { - event = new StreamEvent(event) + event = new ChannelEvent(event) self.onData(event) lastReceived = event.GetTimestamp(); diff --git a/service/stream/static/lib/event/channel.js b/service/stream/static/lib/event/channel.js new file mode 100644 index 0000000..dfbf253 --- /dev/null +++ b/service/stream/static/lib/event/channel.js @@ -0,0 +1,48 @@ +var ChannelEvent = (function() { + + function ChannelEvent(event) { + + /** @type {String} */ + this.id = null; + + /** @type {Integer} */ + this.ts = event.Timestamp; + + /** @type {Integer} */ + this.actionGroup = null + + /** @type {Integer} */ + this.actionType = null + + /** @type {String} */ + this.User = event.User + + /** @type {String} */ + this.Type = event.Type + + /** @type {String} */ + this.ContentType = 'string' + + /** @type {String} */ + try { + this.Content = JSON.parse(event.Content) + this.ContentType = 'json' + } catch (e) { + this.Content = event.Content + } + } + + ChannelEvent.prototype.GetTimestamp = function() { + return this.ts; + } + + ChannelEvent.prototype.PrintContent = function() { + if (this.ContentType == 'string') { + return this.Content + } + return JSON.stringify(this.Content) + } + + return ChannelEvent; + +})(); diff --git a/service/stream/static/lib/event.js b/service/stream/static/lib/event/system.js similarity index 78% rename from service/stream/static/lib/event.js rename to service/stream/static/lib/event/system.js index 34bba3a..2afedb6 100644 --- a/service/stream/static/lib/event.js +++ b/service/stream/static/lib/event/system.js @@ -1,6 +1,6 @@ -var StreamEvent = (function() { +var SystemEvent = (function() { - function StreamEvent(event) { + function SystemEvent(event) { /** @type {String} */ this.id = null; @@ -32,17 +32,17 @@ var StreamEvent = (function() { } } - StreamEvent.prototype.GetTimestamp = function() { + SystemEvent.prototype.GetTimestamp = function() { return this.ts; } - StreamEvent.prototype.PrintContent = function() { + SystemEvent.prototype.PrintContent = function() { if (this.ContentType == 'string') { return this.Content } return JSON.stringify(this.Content) } - return StreamEvent; + return SystemEvent; })(); From 5b8ddf35be49a38324fc34eae99e9121b4d8b15b Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Tue, 27 Jan 2015 15:04:55 +0100 Subject: [PATCH 08/25] Introduced ChannelEvent, SystemEvent --- resource/entry.go | 2 +- resource/feed.go | 2 +- service/stream/controller/room/feed.go | 23 ++++++++++++++++++++++- service/stream/static/elasticfeed.js | 4 ---- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/resource/entry.go b/resource/entry.go index 37d9ce4..12da1d8 100644 --- a/resource/entry.go +++ b/resource/entry.go @@ -143,7 +143,7 @@ func AddEntry(feedEntry Entry, FeedId string, ApplicationId string, OrgId string // notify data, _ := json.Marshal(entry) - room.Publish <- room.NewEvent(model.EVENT_MESSAGE, "system", string(data)) + room.Publish <- room.NewSystemEvent(model.EVENT_MESSAGE, "system", string(data)) return feedEntry.Id, nil } diff --git a/resource/feed.go b/resource/feed.go index eb3ce9b..99e14ab 100644 --- a/resource/feed.go +++ b/resource/feed.go @@ -95,7 +95,7 @@ func AddFeed(feed Feed, applicationId string, orgId string) (id string, err erro // notify data, _ := json.Marshal(feed) - room.Publish <- room.NewEvent(model.EVENT_MESSAGE, "system", string(data)) + room.Publish <- room.NewSystemEvent(model.EVENT_MESSAGE, "system", string(data)) return feed.Id, nil } diff --git a/service/stream/controller/room/feed.go b/service/stream/controller/room/feed.go index a993324..ff2ec93 100644 --- a/service/stream/controller/room/feed.go +++ b/service/stream/controller/room/feed.go @@ -11,6 +11,19 @@ import ( "github.com/feedlabs/elasticfeed/service/stream/model" ) +const ( + FEED_ADD = iota + FEED_DELETE + FEED_UPDATE + FEED_RESET + FEED_RELOAD + + ENTRY_ADD + ENTRY_DELETE + ENTRY_UPDATE + ENTRY_RELOAD +) + var ( Subscribe = make(chan Subscriber, 10) Unsubscribe = make(chan string, 10) @@ -35,6 +48,14 @@ func NewEvent(ep model.EventType, user, msg string) model.Event { return model.Event{ep, user, time.Now().UnixNano(), msg} } +func NewChannelEvent(ep model.EventType, user, msg string) model.Event { + return NewEvent(ep, user, msg) +} + +func NewSystemEvent(ep model.EventType, user, msg string) model.Event { + return NewChannelEvent(ep, user, msg) +} + func Join(user string, ws *websocket.Conn) { Subscribe <- Subscriber{Name: user, Conn: ws} } @@ -49,7 +70,7 @@ func FeedManager() { case sub := <-Subscribe: Subscribers.PushBack(sub) - Publish <- NewEvent(model.EVENT_JOIN, sub.Name, "") + Publish <- NewEvent(model.EVENT_JOIN, sub.Name, "") case client := <-P2P: data, _ := json.Marshal(&model.Event{model.EVENT_MESSAGE, "system", time.Now().UnixNano(), "ok"}) diff --git a/service/stream/static/elasticfeed.js b/service/stream/static/elasticfeed.js index d825c33..8f6baa5 100644 --- a/service/stream/static/elasticfeed.js +++ b/service/stream/static/elasticfeed.js @@ -1,7 +1,3 @@ -/* - * Author: Feed Labs - */ - function includeJs(jsFilePath) { var js = document.createElement("script"); From 6b0233f035189dfac897e6dc4d66b181f57feac5 Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Tue, 27 Jan 2015 15:08:25 +0100 Subject: [PATCH 09/25] Introduced NewFeedEvent and NewEntryEvent on backend site --- resource/entry.go | 2 +- resource/feed.go | 2 +- service/stream/controller/room/feed.go | 8 ++++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/resource/entry.go b/resource/entry.go index 12da1d8..9a77700 100644 --- a/resource/entry.go +++ b/resource/entry.go @@ -143,7 +143,7 @@ func AddEntry(feedEntry Entry, FeedId string, ApplicationId string, OrgId string // notify data, _ := json.Marshal(entry) - room.Publish <- room.NewSystemEvent(model.EVENT_MESSAGE, "system", string(data)) + room.Publish <- room.NewEntryEvent(model.EVENT_MESSAGE, "system", string(data)) return feedEntry.Id, nil } diff --git a/resource/feed.go b/resource/feed.go index 99e14ab..c8a735a 100644 --- a/resource/feed.go +++ b/resource/feed.go @@ -95,7 +95,7 @@ func AddFeed(feed Feed, applicationId string, orgId string) (id string, err erro // notify data, _ := json.Marshal(feed) - room.Publish <- room.NewSystemEvent(model.EVENT_MESSAGE, "system", string(data)) + room.Publish <- room.NewFeedEvent(model.EVENT_MESSAGE, "system", string(data)) return feed.Id, nil } diff --git a/service/stream/controller/room/feed.go b/service/stream/controller/room/feed.go index ff2ec93..bea9b0a 100644 --- a/service/stream/controller/room/feed.go +++ b/service/stream/controller/room/feed.go @@ -56,6 +56,14 @@ func NewSystemEvent(ep model.EventType, user, msg string) model.Event { return NewChannelEvent(ep, user, msg) } +func NewFeedEvent(ep model.EventType, user, msg string) model.Event { + return NewSystemEvent(ep, user, msg) +} + +func NewEntryEvent(ep model.EventType, user, msg string) model.Event { + return NewSystemEvent(ep, user, msg) +} + func Join(user string, ws *websocket.Conn) { Subscribe <- Subscriber{Name: user, Conn: ws} } From 553e36708c60dfd336dfede2005660588119b923 Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Tue, 27 Jan 2015 15:10:41 +0100 Subject: [PATCH 10/25] Introduced FeedEvent and EntryEvent on frontend site --- service/stream/static/lib/event/entry.js | 48 ++++++++++++++++++++++++ service/stream/static/lib/event/feed.js | 48 ++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 service/stream/static/lib/event/entry.js create mode 100644 service/stream/static/lib/event/feed.js diff --git a/service/stream/static/lib/event/entry.js b/service/stream/static/lib/event/entry.js new file mode 100644 index 0000000..fef5b97 --- /dev/null +++ b/service/stream/static/lib/event/entry.js @@ -0,0 +1,48 @@ +var EntryEvent = (function() { + + function EntryEvent(event) { + + /** @type {String} */ + this.id = null; + + /** @type {Integer} */ + this.ts = event.Timestamp; + + /** @type {Integer} */ + this.actionGroup = null + + /** @type {Integer} */ + this.actionType = null + + /** @type {String} */ + this.User = event.User + + /** @type {String} */ + this.Type = event.Type + + /** @type {String} */ + this.ContentType = 'string' + + /** @type {String} */ + try { + this.Content = JSON.parse(event.Content) + this.ContentType = 'json' + } catch (e) { + this.Content = event.Content + } + } + + EntryEvent.prototype.GetTimestamp = function() { + return this.ts; + } + + EntryEvent.prototype.PrintContent = function() { + if (this.ContentType == 'string') { + return this.Content + } + return JSON.stringify(this.Content) + } + + return EntryEvent; + +})(); diff --git a/service/stream/static/lib/event/feed.js b/service/stream/static/lib/event/feed.js new file mode 100644 index 0000000..3b0432e --- /dev/null +++ b/service/stream/static/lib/event/feed.js @@ -0,0 +1,48 @@ +var FeedEvent = (function() { + + function FeedEvent(event) { + + /** @type {String} */ + this.id = null; + + /** @type {Integer} */ + this.ts = event.Timestamp; + + /** @type {Integer} */ + this.actionGroup = null + + /** @type {Integer} */ + this.actionType = null + + /** @type {String} */ + this.User = event.User + + /** @type {String} */ + this.Type = event.Type + + /** @type {String} */ + this.ContentType = 'string' + + /** @type {String} */ + try { + this.Content = JSON.parse(event.Content) + this.ContentType = 'json' + } catch (e) { + this.Content = event.Content + } + } + + FeedEvent.prototype.GetTimestamp = function() { + return this.ts; + } + + FeedEvent.prototype.PrintContent = function() { + if (this.ContentType == 'string') { + return this.Content + } + return JSON.stringify(this.Content) + } + + return FeedEvent; + +})(); From a39e83fba304910f2f6f6ef53fd16423fae55fba Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Tue, 27 Jan 2015 17:17:23 +0100 Subject: [PATCH 11/25] Reorganized protocol messages stack --- service/stream/controller/channel/long_pooling.go | 2 +- service/stream/controller/channel/websocket.go | 2 +- service/stream/controller/room/feed.go | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/service/stream/controller/channel/long_pooling.go b/service/stream/controller/channel/long_pooling.go index 3232fce..def3587 100644 --- a/service/stream/controller/channel/long_pooling.go +++ b/service/stream/controller/channel/long_pooling.go @@ -32,7 +32,7 @@ func (this *LongPollingController) Post() { // should be executed and returned directly to user // lastReceived time should not be changed in that case - room.Publish <- room.NewEvent(model.EVENT_MESSAGE, chid, data) + room.Publish <- room.NewSystemEvent(model.EVENT_MESSAGE, chid, data) } func (this *LongPollingController) Fetch() { diff --git a/service/stream/controller/channel/websocket.go b/service/stream/controller/channel/websocket.go index c5971ee..2c6aa50 100644 --- a/service/stream/controller/channel/websocket.go +++ b/service/stream/controller/channel/websocket.go @@ -38,7 +38,7 @@ func (this *WebSocketController) Join() { return } - room.Publish <- room.NewEvent(model.EVENT_MESSAGE, chid, string(p)) + room.Publish <- room.NewSystemEvent(model.EVENT_MESSAGE, chid, string(p)) room.P2P <- ws } } diff --git a/service/stream/controller/room/feed.go b/service/stream/controller/room/feed.go index bea9b0a..587171d 100644 --- a/service/stream/controller/room/feed.go +++ b/service/stream/controller/room/feed.go @@ -78,10 +78,10 @@ func FeedManager() { case sub := <-Subscribe: Subscribers.PushBack(sub) - Publish <- NewEvent(model.EVENT_JOIN, sub.Name, "") + Publish <- NewChannelEvent(model.EVENT_JOIN, sub.Name, "") case client := <-P2P: - data, _ := json.Marshal(&model.Event{model.EVENT_MESSAGE, "system", time.Now().UnixNano(), "ok"}) + data, _ := json.Marshal(NewSystemEvent(model.EVENT_MESSAGE, "system", "ok")) client.WriteMessage(websocket.TextMessage, data) case event := <-Publish: @@ -104,7 +104,7 @@ func FeedManager() { ws.Close() feedify.Error("WebSocket closed:", unsub) } - Publish <- NewEvent(model.EVENT_LEAVE, unsub, "") + Publish <- NewChannelEvent(model.EVENT_LEAVE, unsub, "") break } } From 37be34084f068a077be7c4e8c8958184d79b8254 Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Tue, 27 Jan 2015 21:42:24 +0100 Subject: [PATCH 12/25] Added events to channel --- service/stream/static/lib/channel.js | 80 +++++++++++++++++++++------- service/stream/static/lp.html | 17 +++++- service/stream/static/ws.html | 17 +++++- 3 files changed, 94 insertions(+), 20 deletions(-) diff --git a/service/stream/static/lib/channel.js b/service/stream/static/lib/channel.js index 38babdc..4b9c57e 100644 --- a/service/stream/static/lib/channel.js +++ b/service/stream/static/lib/channel.js @@ -1,8 +1,8 @@ var Channel = (function() { - const ACTION_JOIN = 0 - const ACTION_LEAVE = 1 - const ACTION_MESSAGE = 2 + const JOIN = 0 + const LEAVE = 1 + const MESSAGE = 2 const AUTHENTICATED = 3 const AUTHENTICATION_REQUIRED = 4 @@ -44,37 +44,81 @@ var Channel = (function() { this.credential = _extend(defaultCredential, credential); /** @type {Object} */ - this._handlers = []; + this._handlers = {}; } + // Handlers + /** * @param {ChannelEvent} event * @param {Function} callback */ - Channel.prototype.registerHandler = function(options, callback) { - options = { - id: null, - action_group: "feed|entry", - action_type: "add|del|update|*" + Channel.prototype.on = function(name, callback) { + switch (name) { + case 'join': + type = JOIN + break; + case 'leave': + type = LEAVE + break; + case 'message': + type = MESSAGE + break; + default: + return false; + break; } - - this._handlers.push({options: options, cb: callback}); + if (this._handlers[type] == undefined) { + this._handlers[type] = [] + } + this._handlers[type].push(callback); + return true; } + // Events + /** * @param {ChannelEvent} event */ Channel.prototype.onData = function(event) { - for (var i in this._handlers) { - this._handlers[i].cb.call(this, data); + switch (event.Type) { + case JOIN: + this.onJoin(event.User, event.ts) + break; + case LEAVE: + this.onLeave(event.User, event.ts) + break; + case MESSAGE: + this.onMessage(event.User, event.ts, event.Content) + break; } + } - this.EventToString(event); + Channel.prototype.onJoin = function(chid, timestamp) { + for (var i in this._handlers[JOIN]) { + this._handlers[JOIN][i].call(this, chid, timestamp); + } } - Channel.prototype.Authenticate = function(credential) { + Channel.prototype.onLeave = function(chid, timestamp) { + for (var i in this._handlers[LEAVE]) { + this._handlers[LEAVE][i].call(this, chid, timestamp); + } } + Channel.prototype.onMessage = function(chid, timestamp, data) { + for (var i in this._handlers[MESSAGE]) { + this._handlers[MESSAGE][i].call(this, chid, timestamp, data); + } + } + + // Auth + + Channel.prototype.Authenticate = function(data) { + } + + // Connection + Channel.prototype.getConnection = function() { } @@ -221,13 +265,13 @@ var Channel = (function() { Channel.prototype.EventToString = function(event) { switch (event.Type) { - case ACTION_JOIN: + case JOIN: console.log(event.User + " joined the chat room"); break; - case ACTION_LEAVE: + case LEAVE: console.log(event.User + " left the chat room"); break; - case ACTION_MESSAGE: + case MESSAGE: console.log(event.User + ", " + event.PrintContent()); break; } diff --git a/service/stream/static/lp.html b/service/stream/static/lp.html index 868efa1..491c9fa 100644 --- a/service/stream/static/lp.html +++ b/service/stream/static/lp.html @@ -4,7 +4,22 @@ diff --git a/service/stream/static/ws.html b/service/stream/static/ws.html index 09131b2..b518c72 100644 --- a/service/stream/static/ws.html +++ b/service/stream/static/ws.html @@ -4,7 +4,22 @@ From eeacf5eec248c3c9d644c8da5c2b4bd48746a4ad Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Tue, 27 Jan 2015 22:28:17 +0100 Subject: [PATCH 13/25] Moved authentication layer to feed --- service/stream/static/lib/channel.js | 10 ---------- service/stream/static/lib/feed.js | 5 +++++ 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/service/stream/static/lib/channel.js b/service/stream/static/lib/channel.js index 4b9c57e..e88897e 100644 --- a/service/stream/static/lib/channel.js +++ b/service/stream/static/lib/channel.js @@ -4,11 +4,6 @@ var Channel = (function() { const LEAVE = 1 const MESSAGE = 2 - const AUTHENTICATED = 3 - const AUTHENTICATION_REQUIRED = 4 - const AUTHENTICATION_FAILED = 5 - const LOGGED_OUT = 6 - var defaultOptions = { id: null, transport: 'ws', @@ -112,11 +107,6 @@ var Channel = (function() { } } - // Auth - - Channel.prototype.Authenticate = function(data) { - } - // Connection Channel.prototype.getConnection = function() { diff --git a/service/stream/static/lib/feed.js b/service/stream/static/lib/feed.js index 0d081b2..8480f2d 100644 --- a/service/stream/static/lib/feed.js +++ b/service/stream/static/lib/feed.js @@ -9,6 +9,11 @@ var Feed = (function() { const ACTION_HIDE = 5 const ACTION_SHOW = 6 + const AUTHENTICATED = 100 + const AUTHENTICATION_REQUIRED = 101 + const AUTHENTICATION_FAILED = 102 + const LOGGED_OUT = 103 + /** @type {Feed} */ var localCache = {} From 1d15d18c34dd8adda7334dc8c63287e0187654b1 Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Tue, 27 Jan 2015 22:29:45 +0100 Subject: [PATCH 14/25] Moved credential to feed --- service/stream/static/lib/channel.js | 11 +---------- service/stream/static/lib/feed.js | 6 ++++++ 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/service/stream/static/lib/channel.js b/service/stream/static/lib/channel.js index e88897e..b4677fb 100644 --- a/service/stream/static/lib/channel.js +++ b/service/stream/static/lib/channel.js @@ -10,13 +10,7 @@ var Channel = (function() { connectOnInit: true } - var defaultCredential = { - username: null, - token: null, - method: 'basic' - } - - function Channel(options, credential) { + function Channel(options) { /** @type {String} */ this.id = _uniqueId(); @@ -35,9 +29,6 @@ var Channel = (function() { this.url = this.options.url; } - /** @type {Object} */ - this.credential = _extend(defaultCredential, credential); - /** @type {Object} */ this._handlers = {}; } diff --git a/service/stream/static/lib/feed.js b/service/stream/static/lib/feed.js index 8480f2d..c1a4b3e 100644 --- a/service/stream/static/lib/feed.js +++ b/service/stream/static/lib/feed.js @@ -25,6 +25,12 @@ var Feed = (function() { defaultElementCount: 0 }; + var globalCredential = { + username: null, + token: null, + method: 'basic' + } + function Feed(options, stylerFunction) { /** @type {String} */ From 6646110070b1175acebbd715acf3cc17f45b347b Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Tue, 27 Jan 2015 22:30:21 +0100 Subject: [PATCH 15/25] Formatting --- service/stream/static/lib/feed.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/service/stream/static/lib/feed.js b/service/stream/static/lib/feed.js index c1a4b3e..8c87278 100644 --- a/service/stream/static/lib/feed.js +++ b/service/stream/static/lib/feed.js @@ -25,11 +25,12 @@ var Feed = (function() { defaultElementCount: 0 }; + /** @type {Object} */ var globalCredential = { username: null, token: null, method: 'basic' - } + }; function Feed(options, stylerFunction) { From f65e51b56fd0345e726e53265837d24206caa99b Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Wed, 28 Jan 2015 00:49:39 +0100 Subject: [PATCH 16/25] Feed instance skeleton --- service/stream/static/elasticfeed.js | 34 ++++++++++++++++++++++++++++ service/stream/static/lib/feed.js | 34 +++++++++++++++++++--------- service/stream/static/lp.html | 17 +++++++++----- service/stream/static/ws.html | 12 +++++----- 4 files changed, 74 insertions(+), 23 deletions(-) diff --git a/service/stream/static/elasticfeed.js b/service/stream/static/elasticfeed.js index 8f6baa5..f4ca535 100644 --- a/service/stream/static/elasticfeed.js +++ b/service/stream/static/elasticfeed.js @@ -23,6 +23,23 @@ includeJs('lib/event/system.js'); /** @type {Object} */ feedList: {}, + initFeed: function(options) { + options = _extend({ + channel: { + url: 'ws://localhost:80/ws', + transport: 'ws' + }, + styler: function(data) { + } + }, options); + + console.log(options) + + channel = this.getChannel(options.channel); + + return new Feed(options, channel); + }, + /** * Returns Feed object * @param options @@ -68,6 +85,23 @@ includeJs('lib/event/system.js'); }; + // Helpers + + var _extend = function(a, b) { + var c = {}, prop; + for (prop in a) { + if (a.hasOwnProperty(prop)) { + c[prop] = a[prop]; + } + } + for (prop in b) { + if (b.hasOwnProperty(prop)) { + c[prop] = b[prop]; + } + } + return c; + } + if ("function" === typeof define) { define(function(require) { return elasticfeed; diff --git a/service/stream/static/lib/feed.js b/service/stream/static/lib/feed.js index 8c87278..945dd1e 100644 --- a/service/stream/static/lib/feed.js +++ b/service/stream/static/lib/feed.js @@ -32,20 +32,33 @@ var Feed = (function() { method: 'basic' }; - function Feed(options, stylerFunction) { + function Feed(options, channel) { /** @type {String} */ this.id = null; - /** @type {String} */ - this.channelId = null; + /** @type {Channel} */ + this.channel = channel; /** @type {Array} */ this.entryList = []; + /** @type {Object} */ + if (this.channel.options.transport == 'ws') { + this.socket = this.channel.getWebSocketConnection(); + } else if (this.channel.options.transport == 'lp') { + this.socket = this.channel.getLongPoolingConnection(); + } + this.options = _extend(globalOptions, options); - this._stylerFunction = stylerFunction || this._stylerFunction; + this._stylerFunction = options.styler || this._stylerFunction; this.outputContainer = document.getElementById(this.options.outputContainerId); + + this.bindChannel(this.channel); + } + + Feed.prototype.on = function(type, callback) { + } // Events callbacks @@ -88,13 +101,12 @@ var Feed = (function() { // Handlers - Feed.prototype.registerHandlers = function() { - // bind to channel data - } - - // Channel management - - Feed.prototype.getChannel = function() { + Feed.prototype.bindChannel = function(channel) { + channel.on('message', function(chid, ts, data) { + // should detect type of message + // if feed addressed then check id + // trigger action if needed + }); } // Stylers diff --git a/service/stream/static/lp.html b/service/stream/static/lp.html index 491c9fa..14a059e 100644 --- a/service/stream/static/lp.html +++ b/service/stream/static/lp.html @@ -4,22 +4,27 @@ diff --git a/service/stream/static/ws.html b/service/stream/static/ws.html index b518c72..8d414ae 100644 --- a/service/stream/static/ws.html +++ b/service/stream/static/ws.html @@ -4,22 +4,22 @@ From 41798d707d825af0ddeb8c8fa0ab6154a6526e0c Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Wed, 28 Jan 2015 13:27:14 +0100 Subject: [PATCH 17/25] Added service/system with some basic metrics --- service/service.go | 1 + service/system/controller/default.go | 30 ++++++++++++++++++++++++++++ service/system/router/default.go | 10 ++++++++++ service/system/system.go | 8 ++++++++ 4 files changed, 49 insertions(+) create mode 100644 service/system/controller/default.go create mode 100644 service/system/router/default.go create mode 100644 service/system/system.go diff --git a/service/service.go b/service/service.go index 64fa20c..0bee939 100644 --- a/service/service.go +++ b/service/service.go @@ -3,6 +3,7 @@ package service import ( _ "github.com/feedlabs/elasticfeed/service/api" _ "github.com/feedlabs/elasticfeed/service/stream" + _ "github.com/feedlabs/elasticfeed/service/system" ) func init() {} diff --git a/service/system/controller/default.go b/service/system/controller/default.go new file mode 100644 index 0000000..fe15b29 --- /dev/null +++ b/service/system/controller/default.go @@ -0,0 +1,30 @@ +package controller + +import ( + "os" + "runtime" + "strconv" + "github.com/feedlabs/feedify" +) + +type SystemController struct { + feedify.Controller +} + +func (this *SystemController) Get() { + var memstats runtime.MemStats; + + runtime.ReadMemStats(&memstats) + hostname, _ := os.Hostname() + + this.Data["json"] = map[string]string{ + "pid": strconv.Itoa(os.Getpid()), + "hostname": hostname, + "mem_alloc": strconv.Itoa(int(memstats.Alloc)), + "mem_alloc_heap": strconv.Itoa(int(memstats.HeapAlloc)), + "goroutines": strconv.Itoa(runtime.NumGoroutine()), + "cpus": strconv.Itoa(runtime.NumCPU()), + } + + this.Controller.ServeJson() +} diff --git a/service/system/router/default.go b/service/system/router/default.go new file mode 100644 index 0000000..fd5e4fa --- /dev/null +++ b/service/system/router/default.go @@ -0,0 +1,10 @@ +package router + +import ( + "github.com/feedlabs/feedify" + "github.com/feedlabs/elasticfeed/service/system/controller" +) + +func init() { + feedify.Router("/status", &controller.SystemController{}, "get:Get") +} diff --git a/service/system/system.go b/service/system/system.go new file mode 100644 index 0000000..f4352a0 --- /dev/null +++ b/service/system/system.go @@ -0,0 +1,8 @@ +package system + +import ( + _ "github.com/feedlabs/elasticfeed/service/system/controller" + _ "github.com/feedlabs/elasticfeed/service/system/router" +) + +func init() {} From 4ff7cd373b15a9873b88e33838fe14cb02fac989 Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Wed, 28 Jan 2015 13:29:59 +0100 Subject: [PATCH 18/25] Extended system status metrics --- service/system/controller/default.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/service/system/controller/default.go b/service/system/controller/default.go index fe15b29..9c0881e 100644 --- a/service/system/controller/default.go +++ b/service/system/controller/default.go @@ -18,12 +18,14 @@ func (this *SystemController) Get() { hostname, _ := os.Hostname() this.Data["json"] = map[string]string{ - "pid": strconv.Itoa(os.Getpid()), "hostname": hostname, + "pid": strconv.Itoa(os.Getpid()), + "cpus": strconv.Itoa(runtime.NumCPU()), + "goroutines": strconv.Itoa(runtime.NumGoroutine()), "mem_alloc": strconv.Itoa(int(memstats.Alloc)), "mem_alloc_heap": strconv.Itoa(int(memstats.HeapAlloc)), - "goroutines": strconv.Itoa(runtime.NumGoroutine()), - "cpus": strconv.Itoa(runtime.NumCPU()), + "mem_alloc_total": strconv.Itoa(int(memstats.TotalAlloc)), + "mem_sys": strconv.Itoa(int(memstats.Sys)), } this.Controller.ServeJson() From a4ffc9d5d08358c50be51540ae300394834badfd Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Wed, 28 Jan 2015 13:31:31 +0100 Subject: [PATCH 19/25] Renamed system controller to status controller --- service/system/controller/{default.go => status.go} | 4 ++-- service/system/router/{default.go => status.go} | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename service/system/controller/{default.go => status.go} (90%) rename service/system/router/{default.go => status.go} (68%) diff --git a/service/system/controller/default.go b/service/system/controller/status.go similarity index 90% rename from service/system/controller/default.go rename to service/system/controller/status.go index 9c0881e..fd1212a 100644 --- a/service/system/controller/default.go +++ b/service/system/controller/status.go @@ -7,11 +7,11 @@ import ( "github.com/feedlabs/feedify" ) -type SystemController struct { +type StatusController struct { feedify.Controller } -func (this *SystemController) Get() { +func (this *StatusController) Get() { var memstats runtime.MemStats; runtime.ReadMemStats(&memstats) diff --git a/service/system/router/default.go b/service/system/router/status.go similarity index 68% rename from service/system/router/default.go rename to service/system/router/status.go index fd5e4fa..7e04641 100644 --- a/service/system/router/default.go +++ b/service/system/router/status.go @@ -6,5 +6,5 @@ import ( ) func init() { - feedify.Router("/status", &controller.SystemController{}, "get:Get") + feedify.Router("/status", &controller.StatusController{}, "get:Get") } From a9c8dfdea77f5aaa3215f34a70e96d64f6233ca0 Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Wed, 28 Jan 2015 14:14:14 +0100 Subject: [PATCH 20/25] Restructured routers; add /service/ prefix to /api and /stream; added /system to /status --- service/api/v1/controller/default.go | 2 +- service/api/v1/router/admin.go | 4 ++-- service/api/v1/router/application.go | 4 ++-- service/api/v1/router/entry.go | 8 ++++---- service/api/v1/router/feed.go | 4 ++-- service/api/v1/router/org.go | 4 ++-- service/api/v1/router/token.go | 8 ++++---- service/stream/router/default.go | 8 ++++---- service/stream/static/lib/channel.js | 6 +++--- service/stream/static/ws.html | 4 +++- service/system/router/status.go | 2 +- 11 files changed, 28 insertions(+), 26 deletions(-) diff --git a/service/api/v1/controller/default.go b/service/api/v1/controller/default.go index 3c3e296..fdbdd7c 100644 --- a/service/api/v1/controller/default.go +++ b/service/api/v1/controller/default.go @@ -55,7 +55,7 @@ func SetAuthenticationFilter() { var AuthUser = func(ctx *context.Context) { ctx.Input.Data["admin"] = helper.Auth(ctx) } - beego.InsertFilter("/v1/*", beego.BeforeRouter, AuthUser) + beego.InsertFilter("/service/api/v1/*", beego.BeforeRouter, AuthUser) } func NoRoutes() { diff --git a/service/api/v1/router/admin.go b/service/api/v1/router/admin.go index 7d8ccc8..fefcce6 100644 --- a/service/api/v1/router/admin.go +++ b/service/api/v1/router/admin.go @@ -6,6 +6,6 @@ import ( ) func init() { - feedify.Router("/v1/admin", &controller.AdminController{}, "get:GetList;post:Post") - feedify.Router("/v1/admin/:adminId:string", &controller.AdminController{}, "get:Get;delete:Delete;put:Put") + feedify.Router("/service/api/v1/admin", &controller.AdminController{}, "get:GetList;post:Post") + feedify.Router("/service/api/v1/admin/:adminId:string", &controller.AdminController{}, "get:Get;delete:Delete;put:Put") } diff --git a/service/api/v1/router/application.go b/service/api/v1/router/application.go index f16be7c..202d31f 100644 --- a/service/api/v1/router/application.go +++ b/service/api/v1/router/application.go @@ -6,6 +6,6 @@ import ( ) func init() { - feedify.Router("/v1/application", &controller.ApplicationController{}, "get:GetList;post:Post") - feedify.Router("/v1/application/:applicationId:string", &controller.ApplicationController{}, "get:Get;delete:Delete;put:Put") + feedify.Router("/service/api/v1/application", &controller.ApplicationController{}, "get:GetList;post:Post") + feedify.Router("/service/api/v1/application/:applicationId:string", &controller.ApplicationController{}, "get:Get;delete:Delete;put:Put") } diff --git a/service/api/v1/router/entry.go b/service/api/v1/router/entry.go index 2ed16f6..c5e945d 100644 --- a/service/api/v1/router/entry.go +++ b/service/api/v1/router/entry.go @@ -6,9 +6,9 @@ import ( ) func init() { - feedify.Router("/v1/application/:applicationId:string/entry", &controller.EntryController{}, "post:Post") - feedify.Router("/v1/application/:applicationId:string/entry/:feedEntryId:int", &controller.EntryController{}, "get:Get;delete:Delete;put:Put") + feedify.Router("/service/api/v1/application/:applicationId:string/entry", &controller.EntryController{}, "post:Post") + feedify.Router("/service/api/v1/application/:applicationId:string/entry/:feedEntryId:int", &controller.EntryController{}, "get:Get;delete:Delete;put:Put") - feedify.Router("/v1/application/:applicationId:string/feed/:feedId:int/entry", &controller.EntryController{}, "get:GetListByFeed;post:PostToFeed") - feedify.Router("/v1/application/:applicationId:string/feed/:feedId:int/entry/:feedEntryId:int", &controller.EntryController{}, "get:Get;delete:Remove") + feedify.Router("/service/api/v1/application/:applicationId:string/feed/:feedId:int/entry", &controller.EntryController{}, "get:GetListByFeed;post:PostToFeed") + feedify.Router("/service/api/v1/application/:applicationId:string/feed/:feedId:int/entry/:feedEntryId:int", &controller.EntryController{}, "get:Get;delete:Remove") } diff --git a/service/api/v1/router/feed.go b/service/api/v1/router/feed.go index 9e171ed..e08dfd3 100644 --- a/service/api/v1/router/feed.go +++ b/service/api/v1/router/feed.go @@ -6,6 +6,6 @@ import ( ) func init() { - feedify.Router("/v1/application/:applicationId:string/feed", &controller.FeedController{}, "get:GetList;post:Post") - feedify.Router("/v1/application/:applicationId:string/feed/:feedId:int", &controller.FeedController{}, "get:Get;delete:Delete;put:Put") + feedify.Router("/service/api/v1/application/:applicationId:string/feed", &controller.FeedController{}, "get:GetList;post:Post") + feedify.Router("/service/api/v1/application/:applicationId:string/feed/:feedId:int", &controller.FeedController{}, "get:Get;delete:Delete;put:Put") } diff --git a/service/api/v1/router/org.go b/service/api/v1/router/org.go index 7b0064a..9d64812 100644 --- a/service/api/v1/router/org.go +++ b/service/api/v1/router/org.go @@ -6,6 +6,6 @@ import ( ) func init() { - feedify.Router("/v1/org", &controller.OrgController{}, "get:GetList;post:Post") - feedify.Router("/v1/org/:orgId:string", &controller.OrgController{}, "get:Get;delete:Delete;put:Put") + feedify.Router("/service/api/v1/org", &controller.OrgController{}, "get:GetList;post:Post") + feedify.Router("/service/api/v1/org/:orgId:string", &controller.OrgController{}, "get:Get;delete:Delete;put:Put") } diff --git a/service/api/v1/router/token.go b/service/api/v1/router/token.go index ff3a90c..0917ad7 100644 --- a/service/api/v1/router/token.go +++ b/service/api/v1/router/token.go @@ -6,9 +6,9 @@ import ( ) func init() { - feedify.Router("/v1/org/:orgId:string/token", &controller.TokenController{}, "get:GetOrgList;post:PostOrg") - feedify.Router("/v1/org/:orgId:string/token/:tokenId:string", &controller.TokenController{}, "get:GetOrg;delete:DeleteOrg") + feedify.Router("/service/api/v1/org/:orgId:string/token", &controller.TokenController{}, "get:GetOrgList;post:PostOrg") + feedify.Router("/service/api/v1/org/:orgId:string/token/:tokenId:string", &controller.TokenController{}, "get:GetOrg;delete:DeleteOrg") - feedify.Router("/v1/admin/:adminId:string/token", &controller.TokenController{}, "get:GetAdminList;post:PostAdmin") - feedify.Router("/v1/admin/:adminId:string/token/:tokenId:string", &controller.TokenController{}, "get:GetAdmin;delete:DeleteAdmin") + feedify.Router("/service/api/v1/admin/:adminId:string/token", &controller.TokenController{}, "get:GetAdminList;post:PostAdmin") + feedify.Router("/service/api/v1/admin/:adminId:string/token/:tokenId:string", &controller.TokenController{}, "get:GetAdmin;delete:DeleteAdmin") } diff --git a/service/stream/router/default.go b/service/stream/router/default.go index 6777cde..0a24c3e 100644 --- a/service/stream/router/default.go +++ b/service/stream/router/default.go @@ -8,9 +8,9 @@ import ( func init() { feedify.SetStaticPath("/static", "service/stream/static") - feedify.Router("/lp/join", &channel.LongPollingController{}, "get:Join") - feedify.Router("/lp/post", &channel.LongPollingController{}) - feedify.Router("/lp/fetch", &channel.LongPollingController{}, "get:Fetch") + feedify.Router("/service/stream/lp/join", &channel.LongPollingController{}, "get:Join") + feedify.Router("/service/stream/lp/post", &channel.LongPollingController{}) + feedify.Router("/service/stream/lp/fetch", &channel.LongPollingController{}, "get:Fetch") - feedify.Router("/ws/join", &channel.WebSocketController{}, "get:Join") + feedify.Router("/service/stream/ws/join", &channel.WebSocketController{}, "get:Join") } diff --git a/service/stream/static/lib/channel.js b/service/stream/static/lib/channel.js index b4677fb..d7da436 100644 --- a/service/stream/static/lib/channel.js +++ b/service/stream/static/lib/channel.js @@ -104,7 +104,7 @@ var Channel = (function() { } Channel.prototype.getWebSocketConnection = function() { - this._socket = new WebSocket('ws://localhost:10100/ws/join?chid=' + this.id); + this._socket = new WebSocket('ws://localhost:10100/service/stream/ws/join?chid=' + this.id); self = this this._socket.onmessage = function(event) { @@ -124,7 +124,7 @@ var Channel = (function() { var lastReceived = 0; var isWait = false; - this.getJSON('http://localhost:10100/lp/join?chid=' + this.id, function() { + this.getJSON('http://localhost:10100/service/stream/lp/join?chid=' + this.id, function() { }) self = this; @@ -133,7 +133,7 @@ var Channel = (function() { return; } isWait = true; - self.getJSON("http://localhost:10100/lp/fetch?lastReceived=" + lastReceived, function(data, code) { + self.getJSON("http://localhost:10100/service/stream/lp/fetch?lastReceived=" + lastReceived, function(data, code) { if (code == 4) { isWait = false diff --git a/service/stream/static/ws.html b/service/stream/static/ws.html index 8d414ae..b658150 100644 --- a/service/stream/static/ws.html +++ b/service/stream/static/ws.html @@ -5,7 +5,9 @@ window.onload = function() { - feed = elasticfeed.initFeed({transport: 'ws'}); + feed = elasticfeed.initFeed({ + transport: 'ws' + }); feed.channel.on('join', function(chid, ts) { console.log(chid + " joined the chat room"); diff --git a/service/system/router/status.go b/service/system/router/status.go index 7e04641..79a01a3 100644 --- a/service/system/router/status.go +++ b/service/system/router/status.go @@ -6,5 +6,5 @@ import ( ) func init() { - feedify.Router("/status", &controller.StatusController{}, "get:Get") + feedify.Router("/system/status", &controller.StatusController{}, "get:Get") } From 83a33d0b2dfe516e94d586a86267bf3b162e8f4a Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Wed, 28 Jan 2015 22:38:54 +0100 Subject: [PATCH 21/25] Added session manager --- service/stream/controller/channel/long_pooling.go | 5 +++++ service/stream/controller/channel/websocket.go | 7 ++++++- service/stream/controller/room/feed.go | 11 +++++++++-- service/stream/model/event.go | 8 ++++---- service/stream/static/elasticfeed.js | 2 -- service/stream/static/lib/channel.js | 2 +- 6 files changed, 25 insertions(+), 10 deletions(-) diff --git a/service/stream/controller/channel/long_pooling.go b/service/stream/controller/channel/long_pooling.go index def3587..67e5380 100644 --- a/service/stream/controller/channel/long_pooling.go +++ b/service/stream/controller/channel/long_pooling.go @@ -17,6 +17,11 @@ func (this *LongPollingController) Join() { return } + w := this.GetCtx().ResponseWriter + r := this.GetCtx().Input.Request + sess := room.GlobalSessions.SessionStart(w, r) + defer sess.SessionRelease(w) + room.Join(chid, nil) } diff --git a/service/stream/controller/channel/websocket.go b/service/stream/controller/channel/websocket.go index 2c6aa50..ea8619d 100644 --- a/service/stream/controller/channel/websocket.go +++ b/service/stream/controller/channel/websocket.go @@ -20,7 +20,12 @@ func (this *WebSocketController) Join() { return } - ws, err := websocket.Upgrade(this.Ctx.ResponseWriter, this.Ctx.Request, nil, 1024, 1024) + w := this.GetCtx().ResponseWriter + r := this.GetCtx().Input.Request + sess := room.GlobalSessions.SessionStart(w, r) + defer sess.SessionRelease(w) + + ws, err := websocket.Upgrade(w, r, nil, 1024, 1024) if _, ok := err.(websocket.HandshakeError); ok { http.Error(this.Ctx.ResponseWriter, "Not a websocket handshake", 400) return diff --git a/service/stream/controller/room/feed.go b/service/stream/controller/room/feed.go index 587171d..dc1fbdd 100644 --- a/service/stream/controller/room/feed.go +++ b/service/stream/controller/room/feed.go @@ -8,6 +8,8 @@ import ( "github.com/feedlabs/feedify" "github.com/gorilla/websocket" + "github.com/astaxie/beego/session" + "github.com/feedlabs/elasticfeed/service/stream/model" ) @@ -32,6 +34,8 @@ var ( WaitingList = list.New() Subscribers = list.New() + + GlobalSessions *session.Manager ) type Subscription struct { @@ -40,8 +44,8 @@ type Subscription struct { } type Subscriber struct { - Name string - Conn *websocket.Conn + Name string + Conn *websocket.Conn } func NewEvent(ep model.EventType, user, msg string) model.Event { @@ -130,5 +134,8 @@ func broadcastWebSocket(event model.Event) { } func init() { + GlobalSessions, _ = session.NewManager("memory", `{"cookieName":"elasticfeedsessid","gclifetime":3600}`) + go FeedManager() + go GlobalSessions.GC() } diff --git a/service/stream/model/event.go b/service/stream/model/event.go index eb5fd62..209236a 100644 --- a/service/stream/model/event.go +++ b/service/stream/model/event.go @@ -13,10 +13,10 @@ const ( ) type Event struct { - Type EventType - User string - Timestamp int64 - Content string + Type EventType + User string + Timestamp int64 + Content string } const archiveSize = 100 diff --git a/service/stream/static/elasticfeed.js b/service/stream/static/elasticfeed.js index f4ca535..d592b96 100644 --- a/service/stream/static/elasticfeed.js +++ b/service/stream/static/elasticfeed.js @@ -33,8 +33,6 @@ includeJs('lib/event/system.js'); } }, options); - console.log(options) - channel = this.getChannel(options.channel); return new Feed(options, channel); diff --git a/service/stream/static/lib/channel.js b/service/stream/static/lib/channel.js index d7da436..372e90f 100644 --- a/service/stream/static/lib/channel.js +++ b/service/stream/static/lib/channel.js @@ -158,7 +158,7 @@ var Channel = (function() { return { send: function(data) { - self.post("/lp/post", {chid: self.id, data: JSON.stringify(data)}, function(status) { + self.post("/service/stream/lp/post", {chid: self.id, data: JSON.stringify(data)}, function(status) { }); } }; From c6f3dd72855022e26bdb341a144f05d7e24657fd Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Wed, 28 Jan 2015 23:13:50 +0100 Subject: [PATCH 22/25] Renamed api service with db service --- service/api/api.go | 7 ------- service/api/v1/api.go | 8 -------- service/api/v1/router/admin.go | 11 ----------- service/api/v1/router/application.go | 11 ----------- service/api/v1/router/default.go | 11 ----------- service/api/v1/router/entry.go | 14 -------------- service/api/v1/router/feed.go | 11 ----------- service/api/v1/router/org.go | 11 ----------- service/api/v1/router/token.go | 14 -------------- service/db/api.go | 7 +++++++ service/db/v1/api.go | 8 ++++++++ service/{api => db}/v1/controller/admin.go | 2 +- service/{api => db}/v1/controller/application.go | 2 +- service/{api => db}/v1/controller/default.go | 6 +++--- service/{api => db}/v1/controller/entry.go | 2 +- service/{api => db}/v1/controller/feed.go | 2 +- service/{api => db}/v1/controller/org.go | 2 +- service/{api => db}/v1/controller/token.go | 2 +- service/db/v1/router/admin.go | 11 +++++++++++ service/db/v1/router/application.go | 11 +++++++++++ service/db/v1/router/default.go | 12 ++++++++++++ service/db/v1/router/entry.go | 14 ++++++++++++++ service/db/v1/router/feed.go | 11 +++++++++++ service/db/v1/router/org.go | 11 +++++++++++ service/db/v1/router/token.go | 14 ++++++++++++++ service/{api => db}/v1/template/admin/request.go | 0 service/{api => db}/v1/template/admin/response.go | 0 .../{api => db}/v1/template/application/request.go | 0 .../v1/template/application/response.go | 0 service/{api => db}/v1/template/const.go | 0 service/{api => db}/v1/template/default.go | 0 service/{api => db}/v1/template/entry/request.go | 0 service/{api => db}/v1/template/entry/response.go | 0 service/{api => db}/v1/template/feed/request.go | 0 service/{api => db}/v1/template/feed/response.go | 0 service/{api => db}/v1/template/org/request.go | 2 +- service/{api => db}/v1/template/org/response.go | 2 +- service/{api => db}/v1/template/request.go | 0 service/{api => db}/v1/template/response.go | 0 service/{api => db}/v1/template/token/request.go | 0 service/{api => db}/v1/template/token/response.go | 0 service/service.go | 2 +- service/stream/router/default.go | 8 ++++---- 43 files changed, 115 insertions(+), 114 deletions(-) delete mode 100644 service/api/api.go delete mode 100644 service/api/v1/api.go delete mode 100644 service/api/v1/router/admin.go delete mode 100644 service/api/v1/router/application.go delete mode 100644 service/api/v1/router/default.go delete mode 100644 service/api/v1/router/entry.go delete mode 100644 service/api/v1/router/feed.go delete mode 100644 service/api/v1/router/org.go delete mode 100644 service/api/v1/router/token.go create mode 100644 service/db/api.go create mode 100644 service/db/v1/api.go rename service/{api => db}/v1/controller/admin.go (97%) rename service/{api => db}/v1/controller/application.go (98%) rename service/{api => db}/v1/controller/default.go (91%) rename service/{api => db}/v1/controller/entry.go (98%) rename service/{api => db}/v1/controller/feed.go (98%) rename service/{api => db}/v1/controller/org.go (97%) rename service/{api => db}/v1/controller/token.go (98%) create mode 100644 service/db/v1/router/admin.go create mode 100644 service/db/v1/router/application.go create mode 100644 service/db/v1/router/default.go create mode 100644 service/db/v1/router/entry.go create mode 100644 service/db/v1/router/feed.go create mode 100644 service/db/v1/router/org.go create mode 100644 service/db/v1/router/token.go rename service/{api => db}/v1/template/admin/request.go (100%) rename service/{api => db}/v1/template/admin/response.go (100%) rename service/{api => db}/v1/template/application/request.go (100%) rename service/{api => db}/v1/template/application/response.go (100%) rename service/{api => db}/v1/template/const.go (100%) rename service/{api => db}/v1/template/default.go (100%) rename service/{api => db}/v1/template/entry/request.go (100%) rename service/{api => db}/v1/template/entry/response.go (100%) rename service/{api => db}/v1/template/feed/request.go (100%) rename service/{api => db}/v1/template/feed/response.go (100%) rename service/{api => db}/v1/template/org/request.go (96%) rename service/{api => db}/v1/template/org/response.go (98%) rename service/{api => db}/v1/template/request.go (100%) rename service/{api => db}/v1/template/response.go (100%) rename service/{api => db}/v1/template/token/request.go (100%) rename service/{api => db}/v1/template/token/response.go (100%) diff --git a/service/api/api.go b/service/api/api.go deleted file mode 100644 index f9ce540..0000000 --- a/service/api/api.go +++ /dev/null @@ -1,7 +0,0 @@ -package api - -import ( - _ "github.com/feedlabs/elasticfeed/service/api/v1" -) - -func init() {} diff --git a/service/api/v1/api.go b/service/api/v1/api.go deleted file mode 100644 index 4bf8636..0000000 --- a/service/api/v1/api.go +++ /dev/null @@ -1,8 +0,0 @@ -package v1 - -import ( - _ "github.com/feedlabs/elasticfeed/service/api/v1/router" - _ "github.com/feedlabs/elasticfeed/service/api/v1/controller" -) - -func init() {} diff --git a/service/api/v1/router/admin.go b/service/api/v1/router/admin.go deleted file mode 100644 index fefcce6..0000000 --- a/service/api/v1/router/admin.go +++ /dev/null @@ -1,11 +0,0 @@ -package router - -import ( - "github.com/feedlabs/feedify" - "github.com/feedlabs/elasticfeed/service/api/v1/controller" -) - -func init() { - feedify.Router("/service/api/v1/admin", &controller.AdminController{}, "get:GetList;post:Post") - feedify.Router("/service/api/v1/admin/:adminId:string", &controller.AdminController{}, "get:Get;delete:Delete;put:Put") -} diff --git a/service/api/v1/router/application.go b/service/api/v1/router/application.go deleted file mode 100644 index 202d31f..0000000 --- a/service/api/v1/router/application.go +++ /dev/null @@ -1,11 +0,0 @@ -package router - -import ( - "github.com/feedlabs/feedify" - "github.com/feedlabs/elasticfeed/service/api/v1/controller" -) - -func init() { - feedify.Router("/service/api/v1/application", &controller.ApplicationController{}, "get:GetList;post:Post") - feedify.Router("/service/api/v1/application/:applicationId:string", &controller.ApplicationController{}, "get:Get;delete:Delete;put:Put") -} diff --git a/service/api/v1/router/default.go b/service/api/v1/router/default.go deleted file mode 100644 index 95ad3aa..0000000 --- a/service/api/v1/router/default.go +++ /dev/null @@ -1,11 +0,0 @@ -package router - -import ( - "github.com/feedlabs/feedify" - "github.com/feedlabs/elasticfeed/service/api/v1/controller" -) - -func init() { - feedify.Router("/", &controller.DefaultController{}, "get:Get") - feedify.Router("/v1", &controller.DefaultController{}, "get:Get") -} diff --git a/service/api/v1/router/entry.go b/service/api/v1/router/entry.go deleted file mode 100644 index c5e945d..0000000 --- a/service/api/v1/router/entry.go +++ /dev/null @@ -1,14 +0,0 @@ -package router - -import ( - "github.com/feedlabs/feedify" - "github.com/feedlabs/elasticfeed/service/api/v1/controller" -) - -func init() { - feedify.Router("/service/api/v1/application/:applicationId:string/entry", &controller.EntryController{}, "post:Post") - feedify.Router("/service/api/v1/application/:applicationId:string/entry/:feedEntryId:int", &controller.EntryController{}, "get:Get;delete:Delete;put:Put") - - feedify.Router("/service/api/v1/application/:applicationId:string/feed/:feedId:int/entry", &controller.EntryController{}, "get:GetListByFeed;post:PostToFeed") - feedify.Router("/service/api/v1/application/:applicationId:string/feed/:feedId:int/entry/:feedEntryId:int", &controller.EntryController{}, "get:Get;delete:Remove") -} diff --git a/service/api/v1/router/feed.go b/service/api/v1/router/feed.go deleted file mode 100644 index e08dfd3..0000000 --- a/service/api/v1/router/feed.go +++ /dev/null @@ -1,11 +0,0 @@ -package router - -import ( - "github.com/feedlabs/feedify" - "github.com/feedlabs/elasticfeed/service/api/v1/controller" -) - -func init() { - feedify.Router("/service/api/v1/application/:applicationId:string/feed", &controller.FeedController{}, "get:GetList;post:Post") - feedify.Router("/service/api/v1/application/:applicationId:string/feed/:feedId:int", &controller.FeedController{}, "get:Get;delete:Delete;put:Put") -} diff --git a/service/api/v1/router/org.go b/service/api/v1/router/org.go deleted file mode 100644 index 9d64812..0000000 --- a/service/api/v1/router/org.go +++ /dev/null @@ -1,11 +0,0 @@ -package router - -import ( - "github.com/feedlabs/feedify" - "github.com/feedlabs/elasticfeed/service/api/v1/controller" -) - -func init() { - feedify.Router("/service/api/v1/org", &controller.OrgController{}, "get:GetList;post:Post") - feedify.Router("/service/api/v1/org/:orgId:string", &controller.OrgController{}, "get:Get;delete:Delete;put:Put") -} diff --git a/service/api/v1/router/token.go b/service/api/v1/router/token.go deleted file mode 100644 index 0917ad7..0000000 --- a/service/api/v1/router/token.go +++ /dev/null @@ -1,14 +0,0 @@ -package router - -import ( - "github.com/feedlabs/feedify" - "github.com/feedlabs/elasticfeed/service/api/v1/controller" -) - -func init() { - feedify.Router("/service/api/v1/org/:orgId:string/token", &controller.TokenController{}, "get:GetOrgList;post:PostOrg") - feedify.Router("/service/api/v1/org/:orgId:string/token/:tokenId:string", &controller.TokenController{}, "get:GetOrg;delete:DeleteOrg") - - feedify.Router("/service/api/v1/admin/:adminId:string/token", &controller.TokenController{}, "get:GetAdminList;post:PostAdmin") - feedify.Router("/service/api/v1/admin/:adminId:string/token/:tokenId:string", &controller.TokenController{}, "get:GetAdmin;delete:DeleteAdmin") -} diff --git a/service/db/api.go b/service/db/api.go new file mode 100644 index 0000000..c51e66e --- /dev/null +++ b/service/db/api.go @@ -0,0 +1,7 @@ +package api + +import ( + _ "github.com/feedlabs/elasticfeed/service/db/v1" +) + +func init() {} diff --git a/service/db/v1/api.go b/service/db/v1/api.go new file mode 100644 index 0000000..522b251 --- /dev/null +++ b/service/db/v1/api.go @@ -0,0 +1,8 @@ +package v1 + +import ( + _ "github.com/feedlabs/elasticfeed/service/db/v1/router" + _ "github.com/feedlabs/elasticfeed/service/db/v1/controller" +) + +func init() {} diff --git a/service/api/v1/controller/admin.go b/service/db/v1/controller/admin.go similarity index 97% rename from service/api/v1/controller/admin.go rename to service/db/v1/controller/admin.go index 2c8774b..71807de 100644 --- a/service/api/v1/controller/admin.go +++ b/service/db/v1/controller/admin.go @@ -4,7 +4,7 @@ import ( "encoding/json" "github.com/feedlabs/elasticfeed/resource" - "github.com/feedlabs/elasticfeed/service/api/v1/template/admin" + "github.com/feedlabs/elasticfeed/service/db/v1/template/admin" ) type AdminController struct { diff --git a/service/api/v1/controller/application.go b/service/db/v1/controller/application.go similarity index 98% rename from service/api/v1/controller/application.go rename to service/db/v1/controller/application.go index 61fe698..63c1bd0 100644 --- a/service/api/v1/controller/application.go +++ b/service/db/v1/controller/application.go @@ -4,7 +4,7 @@ import ( "encoding/json" "github.com/feedlabs/elasticfeed/resource" - "github.com/feedlabs/elasticfeed/service/api/v1/template/application" + "github.com/feedlabs/elasticfeed/service/db/v1/template/application" ) type ApplicationController struct { diff --git a/service/api/v1/controller/default.go b/service/db/v1/controller/default.go similarity index 91% rename from service/api/v1/controller/default.go rename to service/db/v1/controller/default.go index fdbdd7c..c0af43b 100644 --- a/service/api/v1/controller/default.go +++ b/service/db/v1/controller/default.go @@ -7,7 +7,7 @@ import ( "github.com/feedlabs/feedify" "github.com/feedlabs/elasticfeed/resource" "github.com/feedlabs/elasticfeed/helper" - "github.com/feedlabs/elasticfeed/service/api/v1/template" + "github.com/feedlabs/elasticfeed/service/db/v1/template" ) type DefaultController struct { @@ -55,7 +55,7 @@ func SetAuthenticationFilter() { var AuthUser = func(ctx *context.Context) { ctx.Input.Data["admin"] = helper.Auth(ctx) } - beego.InsertFilter("/service/api/v1/*", beego.BeforeRouter, AuthUser) + beego.InsertFilter("/db/v1/*", beego.BeforeRouter, AuthUser) } func NoRoutes() { @@ -70,5 +70,5 @@ func NoRoutes() { func init() { SetAuthenticationFilter() SetGlobalResponseHeader() -// NoRoutes() + // NoRoutes() } diff --git a/service/api/v1/controller/entry.go b/service/db/v1/controller/entry.go similarity index 98% rename from service/api/v1/controller/entry.go rename to service/db/v1/controller/entry.go index f0258c7..8bc6a24 100644 --- a/service/api/v1/controller/entry.go +++ b/service/db/v1/controller/entry.go @@ -4,7 +4,7 @@ import ( "encoding/json" "github.com/feedlabs/elasticfeed/resource" - "github.com/feedlabs/elasticfeed/service/api/v1/template/entry" + "github.com/feedlabs/elasticfeed/service/db/v1/template/entry" ) type EntryController struct { diff --git a/service/api/v1/controller/feed.go b/service/db/v1/controller/feed.go similarity index 98% rename from service/api/v1/controller/feed.go rename to service/db/v1/controller/feed.go index c207266..e9a4b29 100644 --- a/service/api/v1/controller/feed.go +++ b/service/db/v1/controller/feed.go @@ -4,7 +4,7 @@ import ( "encoding/json" "github.com/feedlabs/elasticfeed/resource" - "github.com/feedlabs/elasticfeed/service/api/v1/template/feed" + "github.com/feedlabs/elasticfeed/service/db/v1/template/feed" ) type FeedController struct { diff --git a/service/api/v1/controller/org.go b/service/db/v1/controller/org.go similarity index 97% rename from service/api/v1/controller/org.go rename to service/db/v1/controller/org.go index f62d96a..c26211a 100644 --- a/service/api/v1/controller/org.go +++ b/service/db/v1/controller/org.go @@ -4,7 +4,7 @@ import ( "encoding/json" "github.com/feedlabs/elasticfeed/resource" - template "github.com/feedlabs/elasticfeed/service/api/v1/template/org" + template "github.com/feedlabs/elasticfeed/service/db/v1/template/org" ) type OrgController struct { diff --git a/service/api/v1/controller/token.go b/service/db/v1/controller/token.go similarity index 98% rename from service/api/v1/controller/token.go rename to service/db/v1/controller/token.go index cdf5427..4b7cad6 100644 --- a/service/api/v1/controller/token.go +++ b/service/db/v1/controller/token.go @@ -4,7 +4,7 @@ import ( "encoding/json" "github.com/feedlabs/elasticfeed/resource" - "github.com/feedlabs/elasticfeed/service/api/v1/template/token" + "github.com/feedlabs/elasticfeed/service/db/v1/template/token" ) type TokenController struct { diff --git a/service/db/v1/router/admin.go b/service/db/v1/router/admin.go new file mode 100644 index 0000000..e4f8ec2 --- /dev/null +++ b/service/db/v1/router/admin.go @@ -0,0 +1,11 @@ +package router + +import ( + "github.com/feedlabs/feedify" + "github.com/feedlabs/elasticfeed/service/db/v1/controller" +) + +func init() { + feedify.Router("/db/v1/admin", &controller.AdminController{}, "get:GetList;post:Post") + feedify.Router("/db/v1/admin/:adminId:string", &controller.AdminController{}, "get:Get;delete:Delete;put:Put") +} diff --git a/service/db/v1/router/application.go b/service/db/v1/router/application.go new file mode 100644 index 0000000..c00cd58 --- /dev/null +++ b/service/db/v1/router/application.go @@ -0,0 +1,11 @@ +package router + +import ( + "github.com/feedlabs/feedify" + "github.com/feedlabs/elasticfeed/service/db/v1/controller" +) + +func init() { + feedify.Router("/db/v1/application", &controller.ApplicationController{}, "get:GetList;post:Post") + feedify.Router("/db/v1/application/:applicationId:string", &controller.ApplicationController{}, "get:Get;delete:Delete;put:Put") +} diff --git a/service/db/v1/router/default.go b/service/db/v1/router/default.go new file mode 100644 index 0000000..a966f56 --- /dev/null +++ b/service/db/v1/router/default.go @@ -0,0 +1,12 @@ +package router + +import ( + "github.com/feedlabs/feedify" + "github.com/feedlabs/elasticfeed/service/db/v1/controller" +) + +func init() { + feedify.Router("/", &controller.DefaultController{}, "get:Get") + feedify.Router("/db/", &controller.DefaultController{}, "get:Get") + feedify.Router("/db/v1", &controller.DefaultController{}, "get:Get") +} diff --git a/service/db/v1/router/entry.go b/service/db/v1/router/entry.go new file mode 100644 index 0000000..e1ac42f --- /dev/null +++ b/service/db/v1/router/entry.go @@ -0,0 +1,14 @@ +package router + +import ( + "github.com/feedlabs/feedify" + "github.com/feedlabs/elasticfeed/service/db/v1/controller" +) + +func init() { + feedify.Router("/db/v1/application/:applicationId:string/entry", &controller.EntryController{}, "post:Post") + feedify.Router("/db/v1/application/:applicationId:string/entry/:feedEntryId:int", &controller.EntryController{}, "get:Get;delete:Delete;put:Put") + + feedify.Router("/db/v1/application/:applicationId:string/feed/:feedId:int/entry", &controller.EntryController{}, "get:GetListByFeed;post:PostToFeed") + feedify.Router("/db/v1/application/:applicationId:string/feed/:feedId:int/entry/:feedEntryId:int", &controller.EntryController{}, "get:Get;delete:Remove") +} diff --git a/service/db/v1/router/feed.go b/service/db/v1/router/feed.go new file mode 100644 index 0000000..7d63270 --- /dev/null +++ b/service/db/v1/router/feed.go @@ -0,0 +1,11 @@ +package router + +import ( + "github.com/feedlabs/feedify" + "github.com/feedlabs/elasticfeed/service/db/v1/controller" +) + +func init() { + feedify.Router("/db/v1/application/:applicationId:string/feed", &controller.FeedController{}, "get:GetList;post:Post") + feedify.Router("/db/v1/application/:applicationId:string/feed/:feedId:int", &controller.FeedController{}, "get:Get;delete:Delete;put:Put") +} diff --git a/service/db/v1/router/org.go b/service/db/v1/router/org.go new file mode 100644 index 0000000..5c197d2 --- /dev/null +++ b/service/db/v1/router/org.go @@ -0,0 +1,11 @@ +package router + +import ( + "github.com/feedlabs/feedify" + "github.com/feedlabs/elasticfeed/service/db/v1/controller" +) + +func init() { + feedify.Router("/db/v1/org", &controller.OrgController{}, "get:GetList;post:Post") + feedify.Router("/db/v1/org/:orgId:string", &controller.OrgController{}, "get:Get;delete:Delete;put:Put") +} diff --git a/service/db/v1/router/token.go b/service/db/v1/router/token.go new file mode 100644 index 0000000..216db37 --- /dev/null +++ b/service/db/v1/router/token.go @@ -0,0 +1,14 @@ +package router + +import ( + "github.com/feedlabs/feedify" + "github.com/feedlabs/elasticfeed/service/db/v1/controller" +) + +func init() { + feedify.Router("/db/v1/org/:orgId:string/token", &controller.TokenController{}, "get:GetOrgList;post:PostOrg") + feedify.Router("/db/v1/org/:orgId:string/token/:tokenId:string", &controller.TokenController{}, "get:GetOrg;delete:DeleteOrg") + + feedify.Router("/db/v1/admin/:adminId:string/token", &controller.TokenController{}, "get:GetAdminList;post:PostAdmin") + feedify.Router("/db/v1/admin/:adminId:string/token/:tokenId:string", &controller.TokenController{}, "get:GetAdmin;delete:DeleteAdmin") +} diff --git a/service/api/v1/template/admin/request.go b/service/db/v1/template/admin/request.go similarity index 100% rename from service/api/v1/template/admin/request.go rename to service/db/v1/template/admin/request.go diff --git a/service/api/v1/template/admin/response.go b/service/db/v1/template/admin/response.go similarity index 100% rename from service/api/v1/template/admin/response.go rename to service/db/v1/template/admin/response.go diff --git a/service/api/v1/template/application/request.go b/service/db/v1/template/application/request.go similarity index 100% rename from service/api/v1/template/application/request.go rename to service/db/v1/template/application/request.go diff --git a/service/api/v1/template/application/response.go b/service/db/v1/template/application/response.go similarity index 100% rename from service/api/v1/template/application/response.go rename to service/db/v1/template/application/response.go diff --git a/service/api/v1/template/const.go b/service/db/v1/template/const.go similarity index 100% rename from service/api/v1/template/const.go rename to service/db/v1/template/const.go diff --git a/service/api/v1/template/default.go b/service/db/v1/template/default.go similarity index 100% rename from service/api/v1/template/default.go rename to service/db/v1/template/default.go diff --git a/service/api/v1/template/entry/request.go b/service/db/v1/template/entry/request.go similarity index 100% rename from service/api/v1/template/entry/request.go rename to service/db/v1/template/entry/request.go diff --git a/service/api/v1/template/entry/response.go b/service/db/v1/template/entry/response.go similarity index 100% rename from service/api/v1/template/entry/response.go rename to service/db/v1/template/entry/response.go diff --git a/service/api/v1/template/feed/request.go b/service/db/v1/template/feed/request.go similarity index 100% rename from service/api/v1/template/feed/request.go rename to service/db/v1/template/feed/request.go diff --git a/service/api/v1/template/feed/response.go b/service/db/v1/template/feed/response.go similarity index 100% rename from service/api/v1/template/feed/response.go rename to service/db/v1/template/feed/response.go diff --git a/service/api/v1/template/org/request.go b/service/db/v1/template/org/request.go similarity index 96% rename from service/api/v1/template/org/request.go rename to service/db/v1/template/org/request.go index 0fd6a0e..0efc62b 100644 --- a/service/api/v1/template/org/request.go +++ b/service/db/v1/template/org/request.go @@ -3,7 +3,7 @@ package org import ( "errors" "github.com/feedlabs/feedify/context" - "github.com/feedlabs/elasticfeed/service/api/v1/template" + "github.com/feedlabs/elasticfeed/service/db/v1/template" ) func CheckRequiredParams() { diff --git a/service/api/v1/template/org/response.go b/service/db/v1/template/org/response.go similarity index 98% rename from service/api/v1/template/org/response.go rename to service/db/v1/template/org/response.go index 07969d8..3c951bf 100644 --- a/service/api/v1/template/org/response.go +++ b/service/db/v1/template/org/response.go @@ -3,7 +3,7 @@ package org import ( "strconv" "github.com/feedlabs/elasticfeed/resource" - "github.com/feedlabs/elasticfeed/service/api/v1/template" + "github.com/feedlabs/elasticfeed/service/db/v1/template" "errors" "sort" ) diff --git a/service/api/v1/template/request.go b/service/db/v1/template/request.go similarity index 100% rename from service/api/v1/template/request.go rename to service/db/v1/template/request.go diff --git a/service/api/v1/template/response.go b/service/db/v1/template/response.go similarity index 100% rename from service/api/v1/template/response.go rename to service/db/v1/template/response.go diff --git a/service/api/v1/template/token/request.go b/service/db/v1/template/token/request.go similarity index 100% rename from service/api/v1/template/token/request.go rename to service/db/v1/template/token/request.go diff --git a/service/api/v1/template/token/response.go b/service/db/v1/template/token/response.go similarity index 100% rename from service/api/v1/template/token/response.go rename to service/db/v1/template/token/response.go diff --git a/service/service.go b/service/service.go index 0bee939..3e3d97e 100644 --- a/service/service.go +++ b/service/service.go @@ -1,7 +1,7 @@ package service import ( - _ "github.com/feedlabs/elasticfeed/service/api" + _ "github.com/feedlabs/elasticfeed/service/db" _ "github.com/feedlabs/elasticfeed/service/stream" _ "github.com/feedlabs/elasticfeed/service/system" ) diff --git a/service/stream/router/default.go b/service/stream/router/default.go index 0a24c3e..acf4c10 100644 --- a/service/stream/router/default.go +++ b/service/stream/router/default.go @@ -8,9 +8,9 @@ import ( func init() { feedify.SetStaticPath("/static", "service/stream/static") - feedify.Router("/service/stream/lp/join", &channel.LongPollingController{}, "get:Join") - feedify.Router("/service/stream/lp/post", &channel.LongPollingController{}) - feedify.Router("/service/stream/lp/fetch", &channel.LongPollingController{}, "get:Fetch") + feedify.Router("/stream/lp/join", &channel.LongPollingController{}, "get:Join") + feedify.Router("/stream/lp/post", &channel.LongPollingController{}) + feedify.Router("/stream/lp/fetch", &channel.LongPollingController{}, "get:Fetch") - feedify.Router("/service/stream/ws/join", &channel.WebSocketController{}, "get:Join") + feedify.Router("/stream/ws/join", &channel.WebSocketController{}, "get:Join") } From a9c92becc128469753efb6e8175f5cf54eba76bc Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Wed, 28 Jan 2015 23:19:45 +0100 Subject: [PATCH 23/25] Updated routers --- service/db/v1/controller/default.go | 2 +- service/db/v1/router/admin.go | 4 ++-- service/db/v1/router/application.go | 4 ++-- service/db/v1/router/default.go | 3 +-- service/db/v1/router/entry.go | 8 ++++---- service/db/v1/router/feed.go | 4 ++-- service/db/v1/router/org.go | 4 ++-- service/db/v1/router/token.go | 8 ++++---- 8 files changed, 18 insertions(+), 19 deletions(-) diff --git a/service/db/v1/controller/default.go b/service/db/v1/controller/default.go index c0af43b..63a464a 100644 --- a/service/db/v1/controller/default.go +++ b/service/db/v1/controller/default.go @@ -55,7 +55,7 @@ func SetAuthenticationFilter() { var AuthUser = func(ctx *context.Context) { ctx.Input.Data["admin"] = helper.Auth(ctx) } - beego.InsertFilter("/db/v1/*", beego.BeforeRouter, AuthUser) + beego.InsertFilter("/v1/*", beego.BeforeRouter, AuthUser) } func NoRoutes() { diff --git a/service/db/v1/router/admin.go b/service/db/v1/router/admin.go index e4f8ec2..47bfbd8 100644 --- a/service/db/v1/router/admin.go +++ b/service/db/v1/router/admin.go @@ -6,6 +6,6 @@ import ( ) func init() { - feedify.Router("/db/v1/admin", &controller.AdminController{}, "get:GetList;post:Post") - feedify.Router("/db/v1/admin/:adminId:string", &controller.AdminController{}, "get:Get;delete:Delete;put:Put") + feedify.Router("/v1/admin", &controller.AdminController{}, "get:GetList;post:Post") + feedify.Router("/v1/admin/:adminId:string", &controller.AdminController{}, "get:Get;delete:Delete;put:Put") } diff --git a/service/db/v1/router/application.go b/service/db/v1/router/application.go index c00cd58..4a72333 100644 --- a/service/db/v1/router/application.go +++ b/service/db/v1/router/application.go @@ -6,6 +6,6 @@ import ( ) func init() { - feedify.Router("/db/v1/application", &controller.ApplicationController{}, "get:GetList;post:Post") - feedify.Router("/db/v1/application/:applicationId:string", &controller.ApplicationController{}, "get:Get;delete:Delete;put:Put") + feedify.Router("/v1/application", &controller.ApplicationController{}, "get:GetList;post:Post") + feedify.Router("/v1/application/:applicationId:string", &controller.ApplicationController{}, "get:Get;delete:Delete;put:Put") } diff --git a/service/db/v1/router/default.go b/service/db/v1/router/default.go index a966f56..82db7f3 100644 --- a/service/db/v1/router/default.go +++ b/service/db/v1/router/default.go @@ -7,6 +7,5 @@ import ( func init() { feedify.Router("/", &controller.DefaultController{}, "get:Get") - feedify.Router("/db/", &controller.DefaultController{}, "get:Get") - feedify.Router("/db/v1", &controller.DefaultController{}, "get:Get") + feedify.Router("/v1", &controller.DefaultController{}, "get:Get") } diff --git a/service/db/v1/router/entry.go b/service/db/v1/router/entry.go index e1ac42f..4adad9f 100644 --- a/service/db/v1/router/entry.go +++ b/service/db/v1/router/entry.go @@ -6,9 +6,9 @@ import ( ) func init() { - feedify.Router("/db/v1/application/:applicationId:string/entry", &controller.EntryController{}, "post:Post") - feedify.Router("/db/v1/application/:applicationId:string/entry/:feedEntryId:int", &controller.EntryController{}, "get:Get;delete:Delete;put:Put") + feedify.Router("/v1/application/:applicationId:string/entry", &controller.EntryController{}, "post:Post") + feedify.Router("/v1/application/:applicationId:string/entry/:feedEntryId:int", &controller.EntryController{}, "get:Get;delete:Delete;put:Put") - feedify.Router("/db/v1/application/:applicationId:string/feed/:feedId:int/entry", &controller.EntryController{}, "get:GetListByFeed;post:PostToFeed") - feedify.Router("/db/v1/application/:applicationId:string/feed/:feedId:int/entry/:feedEntryId:int", &controller.EntryController{}, "get:Get;delete:Remove") + feedify.Router("/v1/application/:applicationId:string/feed/:feedId:int/entry", &controller.EntryController{}, "get:GetListByFeed;post:PostToFeed") + feedify.Router("/v1/application/:applicationId:string/feed/:feedId:int/entry/:feedEntryId:int", &controller.EntryController{}, "get:Get;delete:Remove") } diff --git a/service/db/v1/router/feed.go b/service/db/v1/router/feed.go index 7d63270..bf70232 100644 --- a/service/db/v1/router/feed.go +++ b/service/db/v1/router/feed.go @@ -6,6 +6,6 @@ import ( ) func init() { - feedify.Router("/db/v1/application/:applicationId:string/feed", &controller.FeedController{}, "get:GetList;post:Post") - feedify.Router("/db/v1/application/:applicationId:string/feed/:feedId:int", &controller.FeedController{}, "get:Get;delete:Delete;put:Put") + feedify.Router("/v1/application/:applicationId:string/feed", &controller.FeedController{}, "get:GetList;post:Post") + feedify.Router("/v1/application/:applicationId:string/feed/:feedId:int", &controller.FeedController{}, "get:Get;delete:Delete;put:Put") } diff --git a/service/db/v1/router/org.go b/service/db/v1/router/org.go index 5c197d2..6a6b08a 100644 --- a/service/db/v1/router/org.go +++ b/service/db/v1/router/org.go @@ -6,6 +6,6 @@ import ( ) func init() { - feedify.Router("/db/v1/org", &controller.OrgController{}, "get:GetList;post:Post") - feedify.Router("/db/v1/org/:orgId:string", &controller.OrgController{}, "get:Get;delete:Delete;put:Put") + feedify.Router("/v1/org", &controller.OrgController{}, "get:GetList;post:Post") + feedify.Router("/v1/org/:orgId:string", &controller.OrgController{}, "get:Get;delete:Delete;put:Put") } diff --git a/service/db/v1/router/token.go b/service/db/v1/router/token.go index 216db37..f3be790 100644 --- a/service/db/v1/router/token.go +++ b/service/db/v1/router/token.go @@ -6,9 +6,9 @@ import ( ) func init() { - feedify.Router("/db/v1/org/:orgId:string/token", &controller.TokenController{}, "get:GetOrgList;post:PostOrg") - feedify.Router("/db/v1/org/:orgId:string/token/:tokenId:string", &controller.TokenController{}, "get:GetOrg;delete:DeleteOrg") + feedify.Router("/v1/org/:orgId:string/token", &controller.TokenController{}, "get:GetOrgList;post:PostOrg") + feedify.Router("/v1/org/:orgId:string/token/:tokenId:string", &controller.TokenController{}, "get:GetOrg;delete:DeleteOrg") - feedify.Router("/db/v1/admin/:adminId:string/token", &controller.TokenController{}, "get:GetAdminList;post:PostAdmin") - feedify.Router("/db/v1/admin/:adminId:string/token/:tokenId:string", &controller.TokenController{}, "get:GetAdmin;delete:DeleteAdmin") + feedify.Router("/v1/admin/:adminId:string/token", &controller.TokenController{}, "get:GetAdminList;post:PostAdmin") + feedify.Router("/v1/admin/:adminId:string/token/:tokenId:string", &controller.TokenController{}, "get:GetAdmin;delete:DeleteAdmin") } From 31a8a335a052af46b2150d83a04b22f4665d8742 Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Wed, 28 Jan 2015 23:30:42 +0100 Subject: [PATCH 24/25] Extended system status metrics; Bug fixes; --- service/stream/model/event.go | 12 ++++++------ service/stream/static/lib/channel.js | 8 ++++---- service/system/controller/status.go | 5 +++++ 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/service/stream/model/event.go b/service/stream/model/event.go index 209236a..eb8049b 100644 --- a/service/stream/model/event.go +++ b/service/stream/model/event.go @@ -21,18 +21,18 @@ type Event struct { const archiveSize = 100 -var archive = list.New() +var Archive = list.New() func NewArchive(event Event) { - if archive.Len() >= archiveSize { - archive.Remove(archive.Front()) + if Archive.Len() >= archiveSize { + Archive.Remove(Archive.Front()) } - archive.PushBack(event) + Archive.PushBack(event) } func GetEvents(lastReceived int) []Event { - events := make([]Event, 0, archive.Len()) - for event := archive.Front(); event != nil; event = event.Next() { + events := make([]Event, 0, Archive.Len()) + for event := Archive.Front(); event != nil; event = event.Next() { e := event.Value.(Event) if e.Timestamp > int64(lastReceived) { events = append(events, e) diff --git a/service/stream/static/lib/channel.js b/service/stream/static/lib/channel.js index 372e90f..8a6e711 100644 --- a/service/stream/static/lib/channel.js +++ b/service/stream/static/lib/channel.js @@ -104,7 +104,7 @@ var Channel = (function() { } Channel.prototype.getWebSocketConnection = function() { - this._socket = new WebSocket('ws://localhost:10100/service/stream/ws/join?chid=' + this.id); + this._socket = new WebSocket('ws://localhost:10100/stream/ws/join?chid=' + this.id); self = this this._socket.onmessage = function(event) { @@ -124,7 +124,7 @@ var Channel = (function() { var lastReceived = 0; var isWait = false; - this.getJSON('http://localhost:10100/service/stream/lp/join?chid=' + this.id, function() { + this.getJSON('http://localhost:10100/stream/lp/join?chid=' + this.id, function() { }) self = this; @@ -133,7 +133,7 @@ var Channel = (function() { return; } isWait = true; - self.getJSON("http://localhost:10100/service/stream/lp/fetch?lastReceived=" + lastReceived, function(data, code) { + self.getJSON("http://localhost:10100/stream/lp/fetch?lastReceived=" + lastReceived, function(data, code) { if (code == 4) { isWait = false @@ -158,7 +158,7 @@ var Channel = (function() { return { send: function(data) { - self.post("/service/stream/lp/post", {chid: self.id, data: JSON.stringify(data)}, function(status) { + self.post("/stream/lp/post", {chid: self.id, data: JSON.stringify(data)}, function(status) { }); } }; diff --git a/service/system/controller/status.go b/service/system/controller/status.go index fd1212a..c9610ed 100644 --- a/service/system/controller/status.go +++ b/service/system/controller/status.go @@ -5,6 +5,8 @@ import ( "runtime" "strconv" "github.com/feedlabs/feedify" + "github.com/feedlabs/elasticfeed/service/stream/controller/room" + "github.com/feedlabs/elasticfeed/service/stream/model" ) type StatusController struct { @@ -26,6 +28,9 @@ func (this *StatusController) Get() { "mem_alloc_heap": strconv.Itoa(int(memstats.HeapAlloc)), "mem_alloc_total": strconv.Itoa(int(memstats.TotalAlloc)), "mem_sys": strconv.Itoa(int(memstats.Sys)), + "subscribers": strconv.Itoa(room.Subscribers.Len()), + "waitinglist": strconv.Itoa(room.WaitingList.Len()), + "archived": strconv.Itoa(model.Archive.Len()), } this.Controller.ServeJson() From 44422228a85cc1d6b23664767b0e637f409f2ef8 Mon Sep 17 00:00:00 2001 From: Chris Stasiak Date: Wed, 28 Jan 2015 23:34:55 +0100 Subject: [PATCH 25/25] Extended system status metrics; --- service/system/controller/status.go | 32 ++++++++++++++++++----------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/service/system/controller/status.go b/service/system/controller/status.go index c9610ed..582e939 100644 --- a/service/system/controller/status.go +++ b/service/system/controller/status.go @@ -19,18 +19,26 @@ func (this *StatusController) Get() { runtime.ReadMemStats(&memstats) hostname, _ := os.Hostname() - this.Data["json"] = map[string]string{ - "hostname": hostname, - "pid": strconv.Itoa(os.Getpid()), - "cpus": strconv.Itoa(runtime.NumCPU()), - "goroutines": strconv.Itoa(runtime.NumGoroutine()), - "mem_alloc": strconv.Itoa(int(memstats.Alloc)), - "mem_alloc_heap": strconv.Itoa(int(memstats.HeapAlloc)), - "mem_alloc_total": strconv.Itoa(int(memstats.TotalAlloc)), - "mem_sys": strconv.Itoa(int(memstats.Sys)), - "subscribers": strconv.Itoa(room.Subscribers.Len()), - "waitinglist": strconv.Itoa(room.WaitingList.Len()), - "archived": strconv.Itoa(model.Archive.Len()), + this.Data["json"] = map[string]interface{}{ + "system": map[string]interface{} { + "hostname": hostname, + "pid": strconv.Itoa(os.Getpid()), + "cpus": strconv.Itoa(runtime.NumCPU()), + }, + "go": map[string]interface{} { + "routines": strconv.Itoa(runtime.NumGoroutine()), + }, + "mem": map[string]interface{} { + "mem_alloc": strconv.Itoa(int(memstats.Alloc)), + "mem_alloc_heap": strconv.Itoa(int(memstats.HeapAlloc)), + "mem_alloc_total": strconv.Itoa(int(memstats.TotalAlloc)), + "mem_sys": strconv.Itoa(int(memstats.Sys)), + }, + "stream": map[string]interface{} { + "subscribers": strconv.Itoa(room.Subscribers.Len()), + "waitinglist": strconv.Itoa(room.WaitingList.Len()), + "archived_queue": strconv.Itoa(model.Archive.Len()), + }, } this.Controller.ServeJson()