From 201cba86a9d0063021997b76226211aa1ccd7100 Mon Sep 17 00:00:00 2001 From: claudemiro Date: Sat, 31 Jan 2015 15:12:54 -0300 Subject: [PATCH] There is no reason to export this functions and this types. --- ipe/app.go | 44 +++++++++++------------ ipe/app_test.go | 4 +-- ipe/channel.go | 36 +++++++++---------- ipe/config.go | 28 +++++++-------- ipe/connection.go | 8 ++--- ipe/errors.go | 88 ++++++++++++++++++++++----------------------- ipe/events.go | 84 +++++++++++++++++++++---------------------- ipe/ipe.go | 4 +-- ipe/rest.go | 2 +- ipe/router.go | 2 +- ipe/routes.go | 16 ++++----- ipe/subscription.go | 8 ++--- ipe/webhooks.go | 50 +++++++++++++------------- ipe/websockets.go | 14 ++++---- 14 files changed, 193 insertions(+), 195 deletions(-) diff --git a/ipe/app.go b/ipe/app.go index fcd21bf..ffe2913 100644 --- a/ipe/app.go +++ b/ipe/app.go @@ -14,7 +14,7 @@ import ( ) // An App -type App struct { +type app struct { sync.Mutex Name string @@ -27,22 +27,22 @@ type App struct { WebHooks bool URLWebHook string - Channels map[string]*Channel `json:"-"` - Connections map[string]*Connection `json:"-"` + Channels map[string]*channel `json:"-"` + Connections map[string]*connection `json:"-"` Stats *expvar.Map `json:"-"` } // Alloc memory for Connections and Channels -func (a *App) Init() { - a.Connections = make(map[string]*Connection) - a.Channels = make(map[string]*Channel) +func (a *app) Init() { + a.Connections = make(map[string]*connection) + a.Channels = make(map[string]*channel) a.Stats = expvar.NewMap(fmt.Sprintf("%s (%s)", a.Name, a.AppID)) } // Only Presence channels -func (a *App) PresenceChannels() []*Channel { - var channels []*Channel +func (a *app) PresenceChannels() []*channel { + var channels []*channel for _, c := range a.Channels { if c.IsPresence() { @@ -54,8 +54,8 @@ func (a *App) PresenceChannels() []*Channel { } // Only Private channels -func (a *App) PrivateChannels() []*Channel { - var channels []*Channel +func (a *app) PrivateChannels() []*channel { + var channels []*channel for _, c := range a.Channels { if c.IsPrivate() { @@ -67,8 +67,8 @@ func (a *App) PrivateChannels() []*Channel { } // Only Public channels -func (a *App) PublicChannels() []*Channel { - var channels []*Channel +func (a *app) PublicChannels() []*channel { + var channels []*channel for _, c := range a.Channels { if c.IsPublic() { @@ -80,7 +80,7 @@ func (a *App) PublicChannels() []*Channel { } // Disconnect Socket -func (a *App) Disconnect(socketID string) { +func (a *app) Disconnect(socketID string) { log.Infof("Disconnecting socket %+v", socketID) conn, err := a.FindConnection(socketID) @@ -113,7 +113,7 @@ func (a *App) Disconnect(socketID string) { } // Connect a new Subscriber -func (a *App) Connect(conn *Connection) { +func (a *app) Connect(conn *connection) { log.Infof("Adding a new Connection %s to app %s", conn.SocketID, a.Name) a.Lock() defer a.Unlock() @@ -124,7 +124,7 @@ func (a *App) Connect(conn *Connection) { } // Find a Connection on this app -func (a *App) FindConnection(socketID string) (*Connection, error) { +func (a *app) FindConnection(socketID string) (*connection, error) { conn, exists := a.Connections[socketID] if exists { @@ -135,7 +135,7 @@ func (a *App) FindConnection(socketID string) (*Connection, error) { } // DeleteChannel removes the channel from app -func (a *App) RemoveChannel(c *Channel) { +func (a *app) RemoveChannel(c *channel) { log.Infof("Remove the channel %s from app %s", c.ChannelID, a.Name) a.Lock() defer a.Unlock() @@ -158,7 +158,7 @@ func (a *App) RemoveChannel(c *Channel) { } // Add a new Channel to this APP -func (a *App) AddChannel(c *Channel) { +func (a *app) AddChannel(c *channel) { log.Infof("Adding a new channel %s to app %s", c.ChannelID, a.Name) a.Lock() @@ -183,7 +183,7 @@ func (a *App) AddChannel(c *Channel) { // Returns a Channel from this app // If not found then the channel is created and added to this app -func (a *App) FindOrCreateChannelByChannelID(n string) *Channel { +func (a *app) FindOrCreateChannelByChannelID(n string) *channel { c, err := a.FindChannelByChannelID(n) if err != nil { @@ -195,7 +195,7 @@ func (a *App) FindOrCreateChannelByChannelID(n string) *Channel { } // Find the channel by channel ID -func (a *App) FindChannelByChannelID(n string) (*Channel, error) { +func (a *app) FindChannelByChannelID(n string) (*channel, error) { c, exists := a.Channels[n] if exists { @@ -205,16 +205,16 @@ func (a *App) FindChannelByChannelID(n string) (*Channel, error) { return nil, errors.New("Channel does not exists") } -func (a *App) Publish(c *Channel, event RawEvent, ignore string) error { +func (a *app) Publish(c *channel, event rawEvent, ignore string) error { a.Stats.Add("TotalUniqueMessages", 1) return c.Publish(a, event, ignore) } -func (a *App) Unsubscribe(c *Channel, conn *Connection) error { +func (a *app) Unsubscribe(c *channel, conn *connection) error { return c.Unsubscribe(a, conn) } -func (a *App) Subscribe(c *Channel, conn *Connection, data string) error { +func (a *app) Subscribe(c *channel, conn *connection, data string) error { return c.Subscribe(a, conn, data) } diff --git a/ipe/app_test.go b/ipe/app_test.go index c54ce5b..b83e198 100644 --- a/ipe/app_test.go +++ b/ipe/app_test.go @@ -11,9 +11,9 @@ import ( var id = 0 -func newApp() *App { +func newApp() *app { - a := App{Name: "Test", AppID: strconv.Itoa(id), Key: "123", Secret: "123", OnlySSL: false, ApplicationDisabled: false, UserEvents: true} + a := app{Name: "Test", AppID: strconv.Itoa(id), Key: "123", Secret: "123", OnlySSL: false, ApplicationDisabled: false, UserEvents: true} a.Init() id++ diff --git a/ipe/channel.go b/ipe/channel.go index 24acbc5..0309da9 100644 --- a/ipe/channel.go +++ b/ipe/channel.go @@ -15,46 +15,46 @@ import ( ) // A Channel -type Channel struct { +type channel struct { sync.Mutex CreatedAt time.Time ChannelID string - Subscriptions map[string]*Subscription + Subscriptions map[string]*subscription } // Return true if the channel has at least one subscriber -func (c *Channel) IsOccupied() bool { +func (c *channel) IsOccupied() bool { return c.TotalSubscriptions() > 0 } // Check if the type of the channel is presence or is private -func (c *Channel) IsPresenceOrPrivate() bool { +func (c *channel) IsPresenceOrPrivate() bool { return c.IsPresence() || c.IsPrivate() } // Check if the type of the channel is public -func (c *Channel) IsPublic() bool { +func (c *channel) IsPublic() bool { return !c.IsPresenceOrPrivate() } // Check if the type of the channel is presence -func (c *Channel) IsPresence() bool { +func (c *channel) IsPresence() bool { return strings.HasPrefix(c.ChannelID, "presence-") } // Check if the type of the channel is private -func (c *Channel) IsPrivate() bool { +func (c *channel) IsPrivate() bool { return strings.HasPrefix(c.ChannelID, "private-") } // Get the total of subscribers -func (c *Channel) TotalSubscriptions() int { +func (c *channel) TotalSubscriptions() int { return len(c.Subscriptions) } // Get the total of users. -func (c *Channel) TotalUsers() int { +func (c *channel) TotalUsers() int { total := make(map[string]int) for _, s := range c.Subscriptions { @@ -65,7 +65,7 @@ func (c *Channel) TotalUsers() int { } // Add a new subscriber to the channel -func (c *Channel) Subscribe(a *App, conn *Connection, channelData string) error { +func (c *channel) Subscribe(a *app, conn *connection, channelData string) error { log.Infof("Subscribing %s to channel %s", conn.SocketID, c.ChannelID) c.Lock() @@ -105,7 +105,7 @@ func (c *Channel) Subscribe(a *App, conn *Connection, channelData string) error a.TriggerMemberAddedHook(c, subscription) // pusher_internal:subscription_succeeded - data := make(map[string]SubscriptionSucceeedEventPresenceData) + data := make(map[string]subscriptionSucceeedEventPresenceData) data["presence"] = newSubscriptionSucceedEventPresenceData(c) js, err = json.Marshal(data) @@ -129,14 +129,14 @@ func (c *Channel) Subscribe(a *App, conn *Connection, channelData string) error } // IsSubscribed check if the user is subscribed -func (c *Channel) IsSubscribed(conn *Connection) bool { +func (c *channel) IsSubscribed(conn *connection) bool { _, exists := c.Subscriptions[conn.SocketID] return exists } // Remove the subscriber from the channel // It destroy the channel if the channels does not have any subscribers. -func (c *Channel) Unsubscribe(a *App, conn *Connection) error { +func (c *channel) Unsubscribe(a *app, conn *connection) error { log.Infof("Unsubscribing %s from channel %s", conn.SocketID, c.ChannelID) c.Lock() @@ -169,14 +169,14 @@ func (c *Channel) Unsubscribe(a *App, conn *Connection) error { } // Create a new Channel -func newChannel(channelID string) *Channel { +func newChannel(channelID string) *channel { log.Infof("Creating a new channel: %s", channelID) - return &Channel{ChannelID: channelID, CreatedAt: time.Now(), Subscriptions: make(map[string]*Subscription)} + return &channel{ChannelID: channelID, CreatedAt: time.Now(), Subscriptions: make(map[string]*subscription)} } // Publish a MemberAddedEvent to all subscriptions -func (c *Channel) PublishMemberAddedEvent(a *App, data string, subscription *Subscription) { +func (c *channel) PublishMemberAddedEvent(a *app, data string, subscription *subscription) { for _, subs := range c.Subscriptions { if subs != subscription { subs.Connection.Publish(newMemberAddedEvent(c.ChannelID, data)) @@ -185,7 +185,7 @@ func (c *Channel) PublishMemberAddedEvent(a *App, data string, subscription *Sub } // Publish a MemberRemovedEvent to all subscriptions -func (c *Channel) PublishMemberRemovedEvent(a *App, subscription *Subscription) { +func (c *channel) PublishMemberRemovedEvent(a *app, subscription *subscription) { for _, subs := range c.Subscriptions { if subs != subscription { subs.Connection.Publish(newMemberRemovedEvent(c.ChannelID, subscription)) @@ -194,7 +194,7 @@ func (c *Channel) PublishMemberRemovedEvent(a *App, subscription *Subscription) } // Publish messages to all Subscribers -func (c *Channel) Publish(a *App, event RawEvent, ignore string) error { +func (c *channel) Publish(a *app, event rawEvent, ignore string) error { b, err := event.Data.MarshalJSON() if err != nil { diff --git a/ipe/config.go b/ipe/config.go index 42d71cf..df16c6c 100644 --- a/ipe/config.go +++ b/ipe/config.go @@ -10,44 +10,44 @@ import ( ) // The config file -type ConfigFile struct { +type configFile struct { Host string // The host, eg: :8080 will start on 0.0.0.0:8080 Expvar bool User string Password string - Apps []*App + Apps []*app } // Error for App not found var AppNotFoundError = errors.New("App not found") // Initialize Apps -func (c *ConfigFile) Init() { +func (c *configFile) Init() { for _, app := range c.Apps { app.Init() } } -func (c *ConfigFile) WasProvidedUserAndPassword() bool { +func (c *configFile) WasProvidedUserAndPassword() bool { return len(strings.TrimSpace(c.User)) > 0 && len(strings.TrimSpace(c.Password)) > 0 } // Returns an App with by appID -func (c *ConfigFile) GetAppByAppID(appID string) (*App, error) { - for _, app := range c.Apps { - if app.AppID == appID { - return app, nil +func (c *configFile) GetAppByAppID(appID string) (*app, error) { + for _, a := range c.Apps { + if a.AppID == appID { + return a, nil } } - return &App{}, AppNotFoundError + return &app{}, AppNotFoundError } // Returns an App with by key -func (c *ConfigFile) GetAppByKey(key string) (*App, error) { - for _, app := range c.Apps { - if app.Key == key { - return app, nil +func (c *configFile) GetAppByKey(key string) (*app, error) { + for _, a := range c.Apps { + if a.Key == key { + return a, nil } } - return &App{}, AppNotFoundError + return &app{}, AppNotFoundError } diff --git a/ipe/connection.go b/ipe/connection.go index 63bc56a..648bf7b 100644 --- a/ipe/connection.go +++ b/ipe/connection.go @@ -10,20 +10,20 @@ import ( ) // An User Connection -type Connection struct { +type connection struct { SocketID string Socket *websocket.Conn } // Create a new Subscriber -func newConnection(socketID string, s *websocket.Conn) *Connection { +func newConnection(socketID string, s *websocket.Conn) *connection { log.Infof("Creating a new Subscriber %+v", socketID) - return &Connection{SocketID: socketID, Socket: s} + return &connection{SocketID: socketID, Socket: s} } // Publish the message to websocket atached to this client -func (conn *Connection) Publish(m interface{}) { +func (conn *connection) Publish(m interface{}) { go func() { if err := conn.Socket.WriteJSON(m); err != nil { log.Errorf("Error publishing message to connection %+v, %s", conn, err) diff --git a/ipe/errors.go b/ipe/errors.go index 9a3c775..6e8265c 100644 --- a/ipe/errors.go +++ b/ipe/errors.go @@ -5,114 +5,114 @@ package ipe // Base interface -type WebsocketError interface { +type websocketError interface { GetCode() int GetMsg() string } // Base struct -type BaseWebsocketError struct { +type baseWebsocketError struct { Code int Msg string } -func (e BaseWebsocketError) GetCode() int { +func (e baseWebsocketError) GetCode() int { return e.Code } -func (e BaseWebsocketError) GetMsg() string { +func (e baseWebsocketError) GetMsg() string { return e.Msg } // Unsupprted protocol version -type UnsupportedProtocolVersionError struct { - BaseWebsocketError +type unsupportedProtocolVersionError struct { + baseWebsocketError } -func newUnsupportedProtocolVersionError() UnsupportedProtocolVersionError { - return UnsupportedProtocolVersionError{ - BaseWebsocketError{Code: UNSUPPORTED_PROTOCOL_VERSION, Msg: "Unsupported protocol version"}, +func newUnsupportedProtocolVersionError() unsupportedProtocolVersionError { + return unsupportedProtocolVersionError{ + baseWebsocketError{Code: UNSUPPORTED_PROTOCOL_VERSION, Msg: "Unsupported protocol version"}, } } // The application does not exists // See the configuration file -type ApplicationDoesNotExistsError struct { - BaseWebsocketError +type applicationDoesNotExistsError struct { + baseWebsocketError } -func newApplicationDoesNotExistsError() ApplicationDoesNotExistsError { - return ApplicationDoesNotExistsError{ - BaseWebsocketError{Code: APPLICATION_DOES_NOT_EXISTS, Msg: "Could not found an app with the given key"}, +func newApplicationDoesNotExistsError() applicationDoesNotExistsError { + return applicationDoesNotExistsError{ + baseWebsocketError{Code: APPLICATION_DOES_NOT_EXISTS, Msg: "Could not found an app with the given key"}, } } // The user did not send the protocol version -type NoProtocolVersionSuppliedError struct { - BaseWebsocketError +type noProtocolVersionSuppliedError struct { + baseWebsocketError } -func newNoProtocolVersionSuppliedError() NoProtocolVersionSuppliedError { - return NoProtocolVersionSuppliedError{ - BaseWebsocketError{Code: NO_PROTOCOL_VERSION_SUPPLIED, Msg: "No protocol version supplied"}, +func newNoProtocolVersionSuppliedError() noProtocolVersionSuppliedError { + return noProtocolVersionSuppliedError{ + baseWebsocketError{Code: NO_PROTOCOL_VERSION_SUPPLIED, Msg: "No protocol version supplied"}, } } // When the application is disabled. // See the configuration file -type ApplicationDisabledError struct { - BaseWebsocketError +type applicationDisabledError struct { + baseWebsocketError } -func newApplicationDisabledError() NoProtocolVersionSuppliedError { - return NoProtocolVersionSuppliedError{ - BaseWebsocketError{Code: APPLICATION_DISABLED, Msg: "Application disabled"}, +func newApplicationDisabledError() noProtocolVersionSuppliedError { + return noProtocolVersionSuppliedError{ + baseWebsocketError{Code: APPLICATION_DISABLED, Msg: "Application disabled"}, } } // When the application only accepts SSL connections -type ApplicationOnlyAccepsSSLError struct { - BaseWebsocketError +type applicationOnlyAccepsSSLError struct { + baseWebsocketError } -func newApplicationOnlyAccepsSSLError() ApplicationOnlyAccepsSSLError { - return ApplicationOnlyAccepsSSLError{ - BaseWebsocketError{Code: APPLICATION_ONLY_ACCEPTS_SSL, Msg: "Application only accepts SSL connections, reconnect using wss://"}, +func newApplicationOnlyAccepsSSLError() applicationOnlyAccepsSSLError { + return applicationOnlyAccepsSSLError{ + baseWebsocketError{Code: APPLICATION_ONLY_ACCEPTS_SSL, Msg: "Application only accepts SSL connections, reconnect using wss://"}, } } // When the user send an invalid version -type InvalidVersionStringFormatError struct { - BaseWebsocketError +type invalidVersionStringFormatError struct { + baseWebsocketError } -func newInvalidVersionStringFormatError() InvalidVersionStringFormatError { - return InvalidVersionStringFormatError{ - BaseWebsocketError{Code: INVALID_VERSION_STRING_FORMAT, Msg: "Invalid version string format"}, +func newInvalidVersionStringFormatError() invalidVersionStringFormatError { + return invalidVersionStringFormatError{ + baseWebsocketError{Code: INVALID_VERSION_STRING_FORMAT, Msg: "Invalid version string format"}, } } // Used when the error was internal // * Decoding json // * Writing to output -type GenericReconnectImmediatelyError struct { - BaseWebsocketError +type genericReconnectImmediatelyError struct { + baseWebsocketError } -func newGenericReconnectImmediatelyError() GenericReconnectImmediatelyError { - return GenericReconnectImmediatelyError{ - BaseWebsocketError{Code: GENERIC_RECONNECT_IMMEDIATELY, Msg: "Generic reconnect immediately"}, +func newGenericReconnectImmediatelyError() genericReconnectImmediatelyError { + return genericReconnectImmediatelyError{ + baseWebsocketError{Code: GENERIC_RECONNECT_IMMEDIATELY, Msg: "Generic reconnect immediately"}, } } // When pusher wants to send an Generic error, it only send the message, the code become nil // Currently I do not know how to send nil, so I send GENERIC_ERROR -type GenericError struct { - BaseWebsocketError +type genericError struct { + baseWebsocketError } -func newGenericError(msg string) GenericError { - return GenericError{ - BaseWebsocketError{Code: GENERIC_ERROR, Msg: msg}, +func newGenericError(msg string) genericError { + return genericError{ + baseWebsocketError{Code: GENERIC_ERROR, Msg: msg}, } } diff --git a/ipe/events.go b/ipe/events.go index b1f67e6..5148820 100644 --- a/ipe/events.go +++ b/ipe/events.go @@ -18,24 +18,24 @@ import ( // "channelData": "extra data" // } // } -type SubscribeEventData struct { +type subscribeEventData struct { Channel string `json:"channel"` Auth string `json:"auth,omitempty"` ChannelData string `json:"channel_data,omitempty"` } -type SubscribeEvent struct { +type subscribeEvent struct { Event string `json:"event"` - Data SubscribeEventData `json:"data"` + Data subscribeEventData `json:"data"` } // Create a new subscribe event with the specified channel and data -func newSubscribeEvent(channel, auth, channelData string) SubscribeEvent { - data := SubscribeEventData{Channel: channel, Auth: auth, ChannelData: channelData} - return SubscribeEvent{Event: "pusher:subscribe", Data: data} +func newSubscribeEvent(channel, auth, channelData string) subscribeEvent { + data := subscribeEventData{Channel: channel, Auth: auth, ChannelData: channelData} + return subscribeEvent{Event: "pusher:subscribe", Data: data} } -type UnsubscribeEventData struct { +type unsubscribeEventData struct { Channel string `json:"channel"` } @@ -45,30 +45,30 @@ type UnsubscribeEventData struct { // "channel": "The channel" // } // } -type UnsubscribeEvent struct { +type unsubscribeEvent struct { Event string `json:"event"` - Data UnsubscribeEventData `json:"data"` + Data unsubscribeEventData `json:"data"` } // Create a new unsubscribe event for the specified channel -func newUnsubscribeEvent(channel string) UnsubscribeEvent { - data := UnsubscribeEventData{Channel: channel} - return UnsubscribeEvent{Event: "pusher:unsubscribe", Data: data} +func newUnsubscribeEvent(channel string) unsubscribeEvent { + data := unsubscribeEventData{Channel: channel} + return unsubscribeEvent{Event: "pusher:unsubscribe", Data: data} } // { // "event": "pusher_internal:subscription_succeeded", // "channel": "the channel" // } -type SubscriptionSucceededEvent struct { +type subscriptionSucceededEvent struct { Event string `json:"event"` Channel string `json:"channel"` Data string `json:"data"` } // Create a new subscription succeed event for the specified channel -func newSubscriptionSucceededEvent(channel, data string) SubscriptionSucceededEvent { - return SubscriptionSucceededEvent{Event: "pusher_internal:subscription_succeeded", Channel: channel, Data: data} +func newSubscriptionSucceededEvent(channel, data string) subscriptionSucceededEvent { + return subscriptionSucceededEvent{Event: "pusher_internal:subscription_succeeded", Channel: channel, Data: data} } // Data Subscription Succeed @@ -89,14 +89,14 @@ func newSubscriptionSucceededEvent(channel, data string) SubscriptionSucceededEv // \"count\": 2 // } // }" -type SubscriptionSucceeedEventPresenceData struct { +type subscriptionSucceeedEventPresenceData struct { Ids []string `json:"ids"` Hash map[string]interface{} `json:"hash"` Count int `json:"count"` } -func newSubscriptionSucceedEventPresenceData(c *Channel) SubscriptionSucceeedEventPresenceData { - event := SubscriptionSucceeedEventPresenceData{} +func newSubscriptionSucceedEventPresenceData(c *channel) subscriptionSucceeedEventPresenceData { + event := subscriptionSucceeedEventPresenceData{} var ids []string hash := make(map[string]interface{}, c.TotalSubscriptions()) @@ -121,28 +121,28 @@ func newSubscriptionSucceedEventPresenceData(c *Channel) SubscriptionSucceeedEve // "event": "pusher:pong", // "data": {} // } -type PongEvent struct { +type pongEvent struct { Event string `json:"event"` Data string `json:"data"` } // Create a new pong event -func newPongEvent() PongEvent { - return PongEvent{Event: "pusher:pong", Data: "{}"} +func newPongEvent() pongEvent { + return pongEvent{Event: "pusher:pong", Data: "{}"} } // { // "event": "pusher:ping", // "data": {} // } -type PingEvent struct { +type pingEvent struct { Event string `json:"event"` Data string `json:"data"` } // Create a new ping event -func newPingEvent() PingEvent { - return PingEvent{Event: "pusher:ping", Data: "{}"} +func newPingEvent() pingEvent { + return pingEvent{Event: "pusher:ping", Data: "{}"} } // { @@ -152,7 +152,7 @@ func newPingEvent() PingEvent { // "code": 4000 // } // } -type ErrorEvent struct { +type errorEvent struct { Event string `json:"event"` Data interface{} `json:"data"` } @@ -161,7 +161,7 @@ type ErrorEvent struct { // Pusher protocol is very strange in some parts // It send null in some errors. // So I created this GENERIC_ERROR thing, just to verify if the json must have null on the error code -func newErrorEvent(code int, message string) ErrorEvent { +func newErrorEvent(code int, message string) errorEvent { var data interface{} if code == GENERIC_ERROR { @@ -182,7 +182,7 @@ func newErrorEvent(code int, message string) ErrorEvent { } } - return ErrorEvent{Event: "pusher:error", Data: data} + return errorEvent{Event: "pusher:error", Data: data} } // { @@ -192,19 +192,19 @@ func newErrorEvent(code int, message string) ErrorEvent { // "activity_timeout" : 120 // } // } -type ConnectionEstablishedEventData struct { +type connectionEstablishedEventData struct { SocketId string `json:"socket_id"` ActivityTimeout int `json:"activity_timeout"` } -type ConnectionEstablishedEvent struct { +type connectionEstablishedEvent struct { Event string `json:"event"` Data string `json:"data"` } // Create a new connection established event using the specified socketId -func newConnectionEstablishedEvent(socketId string) ConnectionEstablishedEvent { - data := ConnectionEstablishedEventData{SocketId: socketId, ActivityTimeout: 120} +func newConnectionEstablishedEvent(socketId string) connectionEstablishedEvent { + data := connectionEstablishedEventData{SocketId: socketId, ActivityTimeout: 120} b, err := json.Marshal(data) @@ -212,7 +212,7 @@ func newConnectionEstablishedEvent(socketId string) ConnectionEstablishedEvent { panic("events: Could not Marshal json ConnectionEstablishedEvent") } - return ConnectionEstablishedEvent{Event: "pusher:connection_established", Data: string(b)} + return connectionEstablishedEvent{Event: "pusher:connection_established", Data: string(b)} } // { @@ -220,14 +220,14 @@ func newConnectionEstablishedEvent(socketId string) ConnectionEstablishedEvent { // "channel": "presence-example-channel", // "data": String // } -type MemberAddedEvent struct { +type memberAddedEvent struct { Event string `json:"event"` Channel string `json:"channel"` Data string `json:"data"` } -func newMemberAddedEvent(channel, data string) MemberAddedEvent { - return MemberAddedEvent{Event: "pusher_internal:member_added", Channel: channel, Data: data} +func newMemberAddedEvent(channel, data string) memberAddedEvent { + return memberAddedEvent{Event: "pusher_internal:member_added", Channel: channel, Data: data} } // { @@ -235,13 +235,13 @@ func newMemberAddedEvent(channel, data string) MemberAddedEvent { // "channel": "presence-example-channel", // "data": String // } -type MemberRemovedEvent struct { +type memberRemovedEvent struct { Event string `json:"event"` Channel string `json:"channel"` Data string `json:"data"` } -func newMemberRemovedEvent(channel string, s *Subscription) MemberRemovedEvent { +func newMemberRemovedEvent(channel string, s *subscription) memberRemovedEvent { data, err := json.Marshal(struct { UserID string `json:"user_id"` }{ @@ -252,7 +252,7 @@ func newMemberRemovedEvent(channel string, s *Subscription) MemberRemovedEvent { log.Error(err) } - return MemberRemovedEvent{Event: "pusher_internal:member_removed", Channel: channel, Data: string(data)} + return memberRemovedEvent{Event: "pusher_internal:member_removed", Channel: channel, Data: string(data)} } // { @@ -260,19 +260,19 @@ func newMemberRemovedEvent(channel string, s *Subscription) MemberRemovedEvent { // "channel": "The channel", // "data": {} // } -type RawEvent struct { +type rawEvent struct { Event string `json:"event"` Channel string `json:"channel"` Data json.RawMessage `json:"data"` } -type ResponseEvent struct { +type responseEvent struct { Event string `json:"event"` Channel string `json:"channel"` Data interface{} `json:"data"` } // The response event that is broadcasted to the client sockets -func newResponseEvent(name, channel string, data interface{}) ResponseEvent { - return ResponseEvent{Event: name, Channel: channel, Data: data} +func newResponseEvent(name, channel string, data interface{}) responseEvent { + return responseEvent{Event: name, Channel: channel, Data: data} } diff --git a/ipe/ipe.go b/ipe/ipe.go index 72a4c66..1382944 100644 --- a/ipe/ipe.go +++ b/ipe/ipe.go @@ -11,7 +11,7 @@ import ( ) // Conf holds the global configuration state -var Conf ConfigFile +var Conf configFile // Start Parse the configuration file and starts the ipe server func Start(configfile string) error { @@ -26,7 +26,7 @@ func Start(configfile string) error { } Conf.Init() - router := NewRouter() + router := newRouter() if err := http.ListenAndServe(Conf.Host, router); err != nil { return err diff --git a/ipe/rest.go b/ipe/rest.go index 2888550..37a1054 100644 --- a/ipe/rest.go +++ b/ipe/rest.go @@ -68,7 +68,7 @@ func postEvents(w http.ResponseWriter, r *http.Request) { for _, c := range input.Channels { channel := app.FindOrCreateChannelByChannelID(c) - app.Publish(channel, RawEvent{Event: input.Name, Channel: c, Data: input.Data}, input.SocketID) + app.Publish(channel, rawEvent{Event: input.Name, Channel: c, Data: input.Data}, input.SocketID) } w.Header().Set("Content-Type", "application/json;charset=UTF-8") diff --git a/ipe/router.go b/ipe/router.go index 50b89be..61b32de 100644 --- a/ipe/router.go +++ b/ipe/router.go @@ -14,7 +14,7 @@ import ( // NewRouter is a function that returns a new configured Router // It add the necessary middlewares -func NewRouter() *mux.Router { +func newRouter() *mux.Router { router := mux.NewRouter().StrictSlash(true) if Conf.Expvar { diff --git a/ipe/routes.go b/ipe/routes.go index c8e9613..5662eb4 100644 --- a/ipe/routes.go +++ b/ipe/routes.go @@ -9,7 +9,7 @@ import ( ) // A route -type Route struct { +type route struct { Name string Method string Pattern string @@ -17,38 +17,36 @@ type Route struct { RequiresRestAuth bool } -type Routes []Route - -var routes = Routes{ - Route{ +var routes = []route{ + route{ "PostEvents", "POST", "/apps/{app_id}/events", postEvents, true, }, - Route{ + route{ "GetChannels", "GET", "/apps/{app_id}/channels", getChannels, true, }, - Route{ + route{ "GetChannel", "GET", "/apps/{app_id}/channels/{channel_name}", getChannel, true, }, - Route{ + route{ "GetChannelUsers", "GET", "/apps/{app_id}/channels/{channel_name}/users", getChannelUsers, true, }, - Route{ + route{ "Websocket", "GET", "/app/{key}", diff --git a/ipe/subscription.go b/ipe/subscription.go index 9f9a65d..21dca0f 100644 --- a/ipe/subscription.go +++ b/ipe/subscription.go @@ -5,13 +5,13 @@ package ipe // A Channel Subscription -type Subscription struct { - Connection *Connection +type subscription struct { + Connection *connection Id string Data string } // Create a new Subscription -func newSubscription(conn *Connection, data string) *Subscription { - return &Subscription{Connection: conn, Data: data} +func newSubscription(conn *connection, data string) *subscription { + return &subscription{Connection: conn, Data: data} } diff --git a/ipe/webhooks.go b/ipe/webhooks.go index f066edb..b722d5e 100644 --- a/ipe/webhooks.go +++ b/ipe/webhooks.go @@ -33,12 +33,12 @@ import ( // // X-Pusher-Key: A Pusher app may have multiple tokens. The oldest active token will be used, identified by this key. // X-Pusher-Signature: A HMAC SHA256 hex digest formed by signing the POST payload (body) with the token’s secret. -type WebHook struct { +type webHook struct { TimeMs int64 `json:"time_ms"` - Events []HookEvent `json:"events"` + Events []hookEvent `json:"events"` } -type HookEvent struct { +type hookEvent struct { Name string `json:"name"` Channel string `json:"channel"` Event string `json:"event,omitempty"` @@ -47,36 +47,36 @@ type HookEvent struct { UserId string `json:"user_id,omitempty"` } -func newChannelOcuppiedHook(channel *Channel) HookEvent { - return HookEvent{Name: "channel_occupied", Channel: channel.ChannelID} +func newChannelOcuppiedHook(channel *channel) hookEvent { + return hookEvent{Name: "channel_occupied", Channel: channel.ChannelID} } -func newChannelVacatedHook(channel *Channel) HookEvent { - return HookEvent{Name: "channel_vacated", Channel: channel.ChannelID} +func newChannelVacatedHook(channel *channel) hookEvent { + return hookEvent{Name: "channel_vacated", Channel: channel.ChannelID} } -func newMemberAddedHook(channel *Channel, s *Subscription) HookEvent { - return HookEvent{Name: "member_added", Channel: channel.ChannelID, UserId: s.Id} +func newMemberAddedHook(channel *channel, s *subscription) hookEvent { + return hookEvent{Name: "member_added", Channel: channel.ChannelID, UserId: s.Id} } -func newMemberRemovedHook(channel *Channel, s *Subscription) HookEvent { - return HookEvent{Name: "member_removed", Channel: channel.ChannelID, UserId: s.Id} +func newMemberRemovedHook(channel *channel, s *subscription) hookEvent { + return hookEvent{Name: "member_removed", Channel: channel.ChannelID, UserId: s.Id} } -func newClientHook(channel *Channel, s *Subscription, event string, data interface{}) HookEvent { - return HookEvent{Name: "client_event", Channel: channel.ChannelID, Event: event, Data: data, SocketID: s.Connection.SocketID} +func newClientHook(channel *channel, s *subscription, event string, data interface{}) hookEvent { + return hookEvent{Name: "client_event", Channel: channel.ChannelID, Event: event, Data: data, SocketID: s.Connection.SocketID} } // channel_occupied // { "name": "channel_occupied", "channel": "test_channel" } -func (a *App) TriggerChannelOccupiedHook(c *Channel) { +func (a *app) TriggerChannelOccupiedHook(c *channel) { event := newChannelOcuppiedHook(c) triggerHook(event.Name, a, c, event) } // channel_vacated // { "name": "channel_vacated", "channel": "test_channel" } -func (a *App) TriggerChannelVacatedHook(c *Channel) { +func (a *app) TriggerChannelVacatedHook(c *channel) { event := newChannelVacatedHook(c) triggerHook(event.Name, a, c, event) } @@ -89,7 +89,7 @@ func (a *App) TriggerChannelVacatedHook(c *Channel) { // "socket_id": "socket_id of the sending socket", // "user_id": "user_id associated with the sending socket" # Only for presence channels // } -func (a *App) TriggerClientEventHook(c *Channel, s *Subscription, client_event string, data interface{}) { +func (a *app) TriggerClientEventHook(c *channel, s *subscription, client_event string, data interface{}) { event := newClientHook(c, s, client_event, data) if c.IsPresence() { @@ -104,7 +104,7 @@ func (a *App) TriggerClientEventHook(c *Channel, s *Subscription, client_event s // "channel": "presence-your_channel_name", // "user_id": "a_user_id" // } -func (a *App) TriggerMemberAddedHook(c *Channel, s *Subscription) { +func (a *app) TriggerMemberAddedHook(c *channel, s *subscription) { event := newMemberAddedHook(c, s) triggerHook(event.Name, a, c, event) } @@ -114,21 +114,21 @@ func (a *App) TriggerMemberAddedHook(c *Channel, s *Subscription) { // "channel": "presence-your_channel_name", // "user_id": "a_user_id" // } -func (a *App) TriggerMemberRemovedHook(c *Channel, s *Subscription) { +func (a *app) TriggerMemberRemovedHook(c *channel, s *subscription) { event := newMemberRemovedHook(c, s) triggerHook(event.Name, a, c, event) } -func triggerHook(name string, app *App, c *Channel, event HookEvent) { - if !app.WebHooks { - log.Infof("Webhooks are not enabled for app: %s", app.Name) +func triggerHook(name string, a *app, c *channel, event hookEvent) { + if !a.WebHooks { + log.Infof("Webhooks are not enabled for app: %s", a.Name) return } go func() { log.Infof("Triggering %s event", name) - hook := WebHook{TimeMs: time.Now().Unix()} + hook := webHook{TimeMs: time.Now().Unix()} hook.Events = append(hook.Events, event) @@ -144,15 +144,15 @@ func triggerHook(name string, app *App, c *Channel, event HookEvent) { var req *http.Request - req, err = http.NewRequest("POST", app.URLWebHook, bytes.NewReader(js)) + req, err = http.NewRequest("POST", a.URLWebHook, bytes.NewReader(js)) if err != nil { log.Errorf("Error creating request: %+v", err) return } req.Header.Set("Content-Type", "application/json") - req.Header.Set("X-Pusher-Key", app.Key) - req.Header.Set("X-Pusher-Signature", utils.HashMAC(js, []byte(app.Secret))) + req.Header.Set("X-Pusher-Key", a.Key) + req.Header.Set("X-Pusher-Signature", utils.HashMAC(js, []byte(a.Secret))) log.V(1).Infof("%+v", req.Header) log.V(1).Infof("%+v", string(js)) diff --git a/ipe/websockets.go b/ipe/websockets.go index 6882cd8..1b49dbd 100644 --- a/ipe/websockets.go +++ b/ipe/websockets.go @@ -26,7 +26,7 @@ var upgrader = websocket.Upgrader{ } // Handle open Subscriber. -func onOpen(conn *websocket.Conn, w http.ResponseWriter, r *http.Request, sessionID string, app *App) WebsocketError { +func onOpen(conn *websocket.Conn, w http.ResponseWriter, r *http.Request, sessionID string, app *app) websocketError { params := r.URL.Query() p := params.Get("protocol") @@ -62,7 +62,7 @@ func onOpen(conn *websocket.Conn, w http.ResponseWriter, r *http.Request, sessio } // Handle the close event -func onClose(sessionID string, app *App) { +func onClose(sessionID string, app *app) { app.Disconnect(sessionID) } @@ -70,7 +70,7 @@ func onClose(sessionID string, app *App) { // // If there is an unrecoverable error then break the loop, // otherwise just keep going. -func onMessage(conn *websocket.Conn, w http.ResponseWriter, r *http.Request, sessionID string, app *App) { +func onMessage(conn *websocket.Conn, w http.ResponseWriter, r *http.Request, sessionID string, app *app) { var event struct { Event string `json:"event"` } @@ -102,7 +102,7 @@ func onMessage(conn *websocket.Conn, w http.ResponseWriter, r *http.Request, ses emitWSError(newGenericReconnectImmediatelyError(), conn) } case "pusher:subscribe": - subscribeEvent := SubscribeEvent{} + subscribeEvent := subscribeEvent{} if err := json.Unmarshal(message, &subscribeEvent); err != nil { emitWSError(newGenericReconnectImmediatelyError(), conn) @@ -142,7 +142,7 @@ func onMessage(conn *websocket.Conn, w http.ResponseWriter, r *http.Request, ses emitWSError(newGenericReconnectImmediatelyError(), conn) } case "pusher:unsubscribe": - unsubscribeEvent := UnsubscribeEvent{} + unsubscribeEvent := unsubscribeEvent{} if err := json.Unmarshal(message, &unsubscribeEvent); err != nil { emitWSError(newGenericReconnectImmediatelyError(), conn) @@ -171,7 +171,7 @@ func onMessage(conn *websocket.Conn, w http.ResponseWriter, r *http.Request, ses emitWSError(newGenericError("To send client events, you must enable this feature in the Settings."), conn) } - clientEvent := RawEvent{} + clientEvent := rawEvent{} if err := json.Unmarshal(message, &clientEvent); err != nil { log.Error(err) @@ -237,7 +237,7 @@ func wsHandler(w http.ResponseWriter, r *http.Request) { } // Emit an Websocket ErrorEvent -func emitWSError(err WebsocketError, conn *websocket.Conn) { +func emitWSError(err websocketError, conn *websocket.Conn) { event := newErrorEvent(err.GetCode(), err.GetMsg())