From 3c51d0b6144408479bf46ce3333c9d318d7dfca0 Mon Sep 17 00:00:00 2001 From: Claudemiro Date: Mon, 26 Nov 2018 22:55:00 +0100 Subject: [PATCH] Fix golint issues --- api/handlers.go | 20 ++++++++++++----- api/handlers_test.go | 2 +- app/app.go | 24 ++++++++++++-------- app/webhooks.go | 5 ++++- channel/channel.go | 33 ++++++++++++++++++--------- config/config.go | 5 ++++- connection/connection.go | 2 +- events/events.go | 43 ++++++++++++++++++++++++------------ mocks/socket.go | 2 ++ storage/storage.go | 3 +++ subscription/subscription.go | 4 ++-- websockets/websocket.go | 4 +++- 12 files changed, 100 insertions(+), 47 deletions(-) diff --git a/api/handlers.go b/api/handlers.go index 5f47e75..e3d4f26 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -43,7 +43,7 @@ func prepareQueryString(params url.Values) string { return strings.Join(pieces, "&") } -// Authenticate pusher +// Authentication Authenticate pusher // see: https://gist.github.com/mloughran/376898 // // The signature is a HMAC SHA256 hex digest. @@ -90,7 +90,7 @@ func Authentication(storage storage.Storage) func(http.Handler) http.Handler { } } -// Check if the application is disabled +// CheckAppDisabled Check if the application is disabled func CheckAppDisabled(storage storage.Storage) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { @@ -117,13 +117,15 @@ func CheckAppDisabled(storage storage.Storage) func(http.Handler) http.Handler { } } +// PostEvents handle post events type PostEvents struct{ storage storage.Storage } +// NewPostEvents return a new PostEvents handler func NewPostEvents(storage storage.Storage) *PostEvents { return &PostEvents{storage: storage} } -// ServeHTTPC An event consists of a name and data (typically JSON) which may be sent to all subscribers to a particular channel or channels. +// ServeHTTP An event consists of a name and data (typically JSON) which may be sent to all subscribers to a particular channel or channels. // This is conventionally known as triggering an event. // // The body should contain a Hash of parameters encoded as JSON where data parameter itself is JSON encoded. @@ -192,13 +194,15 @@ func (h *PostEvents) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } +// GetChannels handle get channels type GetChannels struct{ storage storage.Storage } +// NewGetChannels return a new GetChannels handler func NewGetChannels(storage storage.Storage) *GetChannels { return &GetChannels{storage: storage} } -// Allows fetching a hash of occupied channels (optionally filtered by prefix), +// ServeHTTP Allows fetching a hash of occupied channels (optionally filtered by prefix), // and optionally one or more attributes for each channel. // // Notes: @@ -288,13 +292,15 @@ func (h *GetChannels) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } +// GetChannel handle get channel type GetChannel struct{ storage storage.Storage } +// NewGetChannel return a new GetChannel handler func NewGetChannel(storage storage.Storage) *GetChannel { return &GetChannel{storage: storage} } -// Fetch info for one channel +// ServeHTTP Fetch info for one channel // // Example: // { @@ -380,13 +386,15 @@ func (h *GetChannel) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } +// GetChannelUsers handle get users from a channel type GetChannelUsers struct{ storage storage.Storage } +// NewGetChannelUsers return a new GetChannelUsers handler func NewGetChannelUsers(storage storage.Storage) *GetChannelUsers { return &GetChannelUsers{storage: storage} } -// Allowed only for presence-channels +// ServeHTTP Allowed only for presence-channels // // Example: // { diff --git a/api/handlers_test.go b/api/handlers_test.go index ae1b88c..af170cb 100644 --- a/api/handlers_test.go +++ b/api/handlers_test.go @@ -142,7 +142,7 @@ func Test_getChannels_filter_by_presence_prefix_and_user_count(t *testing.T) { } } -// User count only alowed in Presence channels +// User count only allowed in Presence channels func Test_getChannels_filter_by_private_prefix_and_info_user_count(t *testing.T) { appID := testApp.AppID diff --git a/app/app.go b/app/app.go index 21a9293..8ee435c 100644 --- a/app/app.go +++ b/app/app.go @@ -18,7 +18,7 @@ import ( "ipe/subscription" ) -// An App +// Application represents a Pusher application type Application struct { sync.RWMutex @@ -38,6 +38,7 @@ type Application struct { Stats *expvar.Map `json:"-"` } +// NewApplication returns a new Application func NewApplication( name, appID, @@ -83,7 +84,7 @@ func (a *Application) Channels() []*channel.Channel { return channels } -// Only Presence channels +// PresenceChannels Only Presence channels func (a *Application) PresenceChannels() []*channel.Channel { a.RLock() defer a.RUnlock() @@ -99,7 +100,7 @@ func (a *Application) PresenceChannels() []*channel.Channel { return channels } -// Only Private channels +// PrivateChannels Only Private channels func (a *Application) PrivateChannels() []*channel.Channel { a.RLock() defer a.RUnlock() @@ -115,7 +116,7 @@ func (a *Application) PrivateChannels() []*channel.Channel { return channels } -// Only Public channels +// PublicChannels Only Public channels func (a *Application) PublicChannels() []*channel.Channel { a.RLock() defer a.RUnlock() @@ -179,7 +180,7 @@ func (a *Application) Connect(conn *connection.Connection) { a.Stats.Add("TotalConnections", 1) } -// Find a Connection on this Application +// FindConnection Find a Connection on this Application func (a *Application) FindConnection(socketID string) (*connection.Connection, error) { a.RLock() defer a.RUnlock() @@ -193,7 +194,7 @@ func (a *Application) FindConnection(socketID string) (*connection.Connection, e return nil, errors.New("connection not found") } -// DeleteChannel removes the Channel from Application +// RemoveChannel removes the Channel from Application func (a *Application) RemoveChannel(c *channel.Channel) { log.Infof("remove the Channel %s from Application %s", c.ID, a.Name) a.Lock() @@ -216,7 +217,7 @@ func (a *Application) RemoveChannel(c *channel.Channel) { a.Stats.Add("TotalChannels", -1) } -// Add a new Channel to this APP +// AddChannel Add a new Channel to this APP func (a *Application) AddChannel(c *channel.Channel) { log.Infof("adding a new Channel %s to Application %s", c.ID, a.Name) @@ -240,7 +241,7 @@ func (a *Application) AddChannel(c *channel.Channel) { a.Stats.Add("TotalChannels", 1) } -// Returns a Channel from this Application +// FindOrCreateChannelByChannelID Returns a Channel from this Application // If not found then the Channel is created and added to this Application func (a *Application) FindOrCreateChannelByChannelID(n string) *channel.Channel { c, err := a.FindChannelByChannelID(n) @@ -270,7 +271,7 @@ func (a *Application) FindOrCreateChannelByChannelID(n string) *channel.Channel return c } -// Find the Channel by Channel ID +// FindChannelByChannelID Find the Channel by Channel ID func (a *Application) FindChannelByChannelID(n string) (*channel.Channel, error) { a.RLock() defer a.RUnlock() @@ -284,12 +285,16 @@ func (a *Application) FindChannelByChannelID(n string) (*channel.Channel, error) return nil, errors.New("channel does not exists") } +// Publish an event into the channel +// skip the ignore connection func (a *Application) Publish(c *channel.Channel, event events.Raw, ignore string) error { a.Stats.Add("TotalUniqueMessages", 1) return c.Publish(event, ignore) } +// Unsubscribe unsubscribe the given connection from the channel +// remove the channel from the application if it is empty func (a *Application) Unsubscribe(c *channel.Channel, conn *connection.Connection) error { err := c.Unsubscribe(conn) if err != nil { @@ -303,6 +308,7 @@ func (a *Application) Unsubscribe(c *channel.Channel, conn *connection.Connectio return nil } +// Subscribe the connection into the given channel func (a *Application) Subscribe(c *channel.Channel, conn *connection.Connection, data string) error { return c.Subscribe(conn, data) } diff --git a/app/webhooks.go b/app/webhooks.go index d6c2145..2bb41f6 100644 --- a/app/webhooks.go +++ b/app/webhooks.go @@ -86,7 +86,7 @@ func (a *Application) TriggerChannelOccupiedHook(c *channel.Channel) { } } -// channel_vacated +// TriggerChannelVacatedHook channel_vacated // { "name": "channel_vacated", "channel": "test_channel" } func (a *Application) TriggerChannelVacatedHook(c *channel.Channel) { event := newChannelVacatedHook(c) @@ -98,6 +98,7 @@ func (a *Application) TriggerChannelVacatedHook(c *channel.Channel) { } } +// TriggerClientEventHook client_events // { // "name": "client_event", // "channel": "name of the channel the event was published on", @@ -121,6 +122,7 @@ func (a *Application) TriggerClientEventHook(c *channel.Channel, s *subscription } } +// TriggerMemberAddedHook member_added // { // "name": "member_added", // "channel": "presence-your_channel_name", @@ -136,6 +138,7 @@ func (a *Application) TriggerMemberAddedHook(c *channel.Channel, s *subscription } } +// TriggerMemberRemovedHook member_removed // { // "name": "member_removed", // "channel": "presence-your_channel_name", diff --git a/channel/channel.go b/channel/channel.go index 35c6af3..bbf2380 100644 --- a/channel/channel.go +++ b/channel/channel.go @@ -18,8 +18,13 @@ import ( "ipe/utils" ) +// Option constructor function for Channel type Option func(*Channel) + +// ListenerFunc listener function type ListenerFunc func(*Channel, *subscription.Subscription) + +// ClientEventListenerFunc listener for client events type ClientEventListenerFunc func(*Channel, *subscription.Subscription, string, interface{}) // A Channel @@ -51,30 +56,35 @@ func New(channelID string, options ...Option) *Channel { return c } +// WithMemberAddedListener appends the given ListenerFunc into the memberAddedListeners list func WithMemberAddedListener(f ListenerFunc) func(*Channel) { return func(c *Channel) { c.memberAddedListeners = append(c.memberAddedListeners, f) } } +// WithMemberRemovedListener appends the given ListenerFunc into the memberRemovedListeners list func WithMemberRemovedListener(f ListenerFunc) func(*Channel) { return func(c *Channel) { c.memberRemovedListeners = append(c.memberRemovedListeners, f) } } +// WithChannelOccupiedListener appends the given ListenerFunc into the channelOccupiedListeners list func WithChannelOccupiedListener(f ListenerFunc) func(*Channel) { return func(c *Channel) { c.channelOccupiedListeners = append(c.channelOccupiedListeners, f) } } +// WithChannelVacatedListener appends the given ListenerFunc into the channelVacatedListeners list func WithChannelVacatedListener(f ListenerFunc) func(*Channel) { return func(c *Channel) { c.channelVacatedListeners = append(c.channelVacatedListeners, f) } } +// WithClientEventListener appends the given ListenerFunc into the clientEventListeners list func WithClientEventListener(f ClientEventListenerFunc) func(*Channel) { return func(c *Channel) { c.clientEventListeners = append(c.clientEventListeners, f) @@ -95,32 +105,32 @@ func (c *Channel) Subscriptions() []*subscription.Subscription { return subscriptions } -// Return true if the Channel has at least one subscriber +// IsOccupied Return true if the Channel has at least one subscriber func (c *Channel) IsOccupied() bool { return c.TotalSubscriptions() > 0 } -// Check if the type of the Channel is presence or is private +// IsPresenceOrPrivate Check if the type of the Channel is presence or is private func (c *Channel) IsPresenceOrPrivate() bool { return c.IsPresence() || c.IsPrivate() } -// Check if the type of the Channel is public +// IsPublic Check if the type of the Channel is public func (c *Channel) IsPublic() bool { return !c.IsPresenceOrPrivate() } -// Check if the type of the Channel is presence +// IsPresence Check if the type of the Channel is presence func (c *Channel) IsPresence() bool { return utils.IsPresenceChannel(c.ID) } -// Check if the type of the Channel is private +// IsPrivate Check if the type of the Channel is private func (c *Channel) IsPrivate() bool { return utils.IsPrivateChannel(c.ID) } -// Get the total of subscribers +// TotalSubscriptions Get the total of subscribers func (c *Channel) TotalSubscriptions() int { c.RLock() defer c.RUnlock() @@ -128,7 +138,7 @@ func (c *Channel) TotalSubscriptions() int { return len(c.subscriptions) } -// Get the total of users. +// TotalUsers Get the total of users. func (c *Channel) TotalUsers() int { c.RLock() defer c.RUnlock() @@ -142,7 +152,7 @@ func (c *Channel) TotalUsers() int { return len(total) } -// Add a new subscriber to the Channel +// Subscribe Add a new subscriber to the Channel func (c *Channel) Subscribe(conn *connection.Connection, channelData string) error { log.Infof("Subscribing %s to Channel %s", conn.SocketID, c.ID) @@ -219,7 +229,7 @@ func (c *Channel) IsSubscribed(conn *connection.Connection) bool { return exists } -// Remove the subscriber from the Channel +// Unsubscribe Remove the subscriber from the Channel // It destroy the Channel if the channels does not have any subscribers. func (c *Channel) Unsubscribe(conn *connection.Connection) error { log.Infof("unsubscribe %s from Channel %s", conn.SocketID, c.ID) @@ -254,7 +264,7 @@ func (c *Channel) Unsubscribe(conn *connection.Connection) error { return nil } -// Publish a MemberAddedEvent to all subscriptions +// PublishMemberAddedEvent Publish a MemberAddedEvent to all subscriptions func (c *Channel) PublishMemberAddedEvent(data string, subscription *subscription.Subscription) { c.RLock() defer c.RUnlock() @@ -266,7 +276,7 @@ func (c *Channel) PublishMemberAddedEvent(data string, subscription *subscriptio } } -// Publish a MemberRemovedEvent to all subscriptions +// PublishMemberRemovedEvent Publish a MemberRemovedEvent to all subscriptions func (c *Channel) PublishMemberRemovedEvent(subscription *subscription.Subscription) { c.RLock() defer c.RUnlock() @@ -279,6 +289,7 @@ func (c *Channel) PublishMemberRemovedEvent(subscription *subscription.Subscript } // Publish messages to all Subscribers +// skip the ignore connection func (c *Channel) Publish(event events.Raw, ignore string) error { c.RLock() defer c.RUnlock() diff --git a/config/config.go b/config/config.go index f5e54fc..bb9ce0f 100644 --- a/config/config.go +++ b/config/config.go @@ -4,7 +4,7 @@ package config -// The config file +// File config file type File struct { Host string `yaml:"host"` // The host, eg: :8080 will start on 0.0.0.0:8080 SSL SSL `yaml:"ssl"` @@ -12,6 +12,7 @@ type File struct { Apps []Application `yaml:"apps"` } +// SSL related configuration options type SSL struct { Enabled bool `yaml:"enabled"` Host string `yaml:"host"` @@ -19,6 +20,7 @@ type SSL struct { CertFile string `yaml:"cert_file"` } +// Application related configuration options type Application struct { Name string `yaml:"name"` AppID string `yaml:"app_id"` @@ -30,6 +32,7 @@ type Application struct { WebHooks Webhooks `yaml:"webhooks"` } +// Webhooks related configuration options type Webhooks struct { Enabled bool `yaml:"enabled"` URL string `yaml:"url"` diff --git a/connection/connection.go b/connection/connection.go index b176005..65a5fbe 100644 --- a/connection/connection.go +++ b/connection/connection.go @@ -25,7 +25,7 @@ type Connection struct { CreatedAt time.Time } -// Create a new Subscriber +// New Create a new Subscriber func New(socketID string, s Socket) *Connection { log.Infof("Creating a new Subscriber %+v", socketID) diff --git a/events/events.go b/events/events.go index 08d404f..c936dba 100644 --- a/events/events.go +++ b/events/events.go @@ -12,6 +12,14 @@ import ( "ipe/subscription" ) +// SubscribeData data for Subscribe event +type SubscribeData struct { + Channel string `json:"channel"` + Auth string `json:"auth,omitempty"` + ChannelData string `json:"channel_data,omitempty"` +} + +// Subscribe event // { // "event": "pusher:subscribe", // "data": { @@ -20,27 +28,23 @@ import ( // "channelData": "extra data" // } // } -type SubscribeData struct { - Channel string `json:"channel"` - Auth string `json:"auth,omitempty"` - ChannelData string `json:"channel_data,omitempty"` -} - type Subscribe struct { Event string `json:"event"` Data SubscribeData `json:"data"` } -// Create a new subscribe event with the specified channel and data +// NewSubscribe Create a new subscribe event with the specified channel and data func NewSubscribe(channel, auth, channelData string) Subscribe { data := SubscribeData{Channel: channel, Auth: auth, ChannelData: channelData} return Subscribe{Event: "pusher:subscribe", Data: data} } +// UnsubscribeData type UnsubscribeData struct { Channel string `json:"channel"` } +// Unsubscribe event // { // "event": "pusher:unsubscribe", // "data": { @@ -58,6 +62,7 @@ func NewUnsubscribe(channel string) Unsubscribe { return Unsubscribe{Event: "pusher:unsubscribe", Data: data} } +// SubscriptionSucceeded event // { // "event": "pusher_internal:subscription_succeeded", // "channel": "the channel" @@ -73,8 +78,7 @@ func NewSubscriptionSucceeded(channel, data string) SubscriptionSucceeded { return SubscriptionSucceeded{Event: "pusher_internal:subscription_succeeded", Channel: channel, Data: data} } -// Data Subscription Succeed - +// SubscriptionSucceededPresenceData Data Subscription Succeed // "{ // \"presence\": { // \"ids\": [\"11814b369700141b222a3f3791cec2d9\",\"71dd6a29da2a4833336d2a964becf820\"], @@ -97,6 +101,7 @@ type SubscriptionSucceededPresenceData struct { Count int `json:"count"` } +// NewSubscriptionSucceedPresenceData returns new SubscriptionSucceededPresenceData func NewSubscriptionSucceedPresenceData(subscriptions map[string]*subscription.Subscription) SubscriptionSucceededPresenceData { event := SubscriptionSucceededPresenceData{} @@ -123,6 +128,7 @@ func NewSubscriptionSucceedPresenceData(subscriptions map[string]*subscription.S return event } +// Pong event // { // "event": "pusher:pong", // "data": {} @@ -132,11 +138,12 @@ type Pong struct { Data string `json:"data"` } -// Create a new pong event +// NewPong Create a new pong event func NewPong() Pong { return Pong{Event: "pusher:pong", Data: "{}"} } +// Ping event // { // "event": "pusher:ping", // "data": {} @@ -146,11 +153,12 @@ type Ping struct { Data string `json:"data"` } -// Create a new ping event +// NewPing Create a new ping event func NewPing() Ping { return Ping{Event: "pusher:ping", Data: "{}"} } +// Error event // { // "event": "pusher:error", // "data": { @@ -163,7 +171,7 @@ type Error struct { Data interface{} `json:"data"` } -// Create a new error event +// NewError Create a new error event // Pusher protocol is very strange in some parts // It send null in some errors. func NewError(code int, message string) Error { @@ -183,6 +191,7 @@ func NewError(code int, message string) Error { return Error{Event: "pusher:error", Data: data} } +// ConnectionEstablished event // { // "event" : "pusher:connection_established", // "data" : { @@ -195,7 +204,7 @@ type ConnectionEstablished struct { Data string `json:"data"` } -// Create a new connection established event using the specified socketId +// NewConnectionEstablished Create a new connection established event using the specified socketId func NewConnectionEstablished(socketID string) ConnectionEstablished { b, err := json.Marshal(struct { SocketID string `json:"socket_id"` @@ -211,6 +220,7 @@ func NewConnectionEstablished(socketID string) ConnectionEstablished { return ConnectionEstablished{Event: "pusher:connection_established", Data: string(b)} } +// MemberAdded event // { // "event": "pusher_internal:member_added", // "channel": "presence-example-channel", @@ -222,10 +232,12 @@ type MemberAdded struct { Data string `json:"data"` } +// NewMemberAdded creates a new MemberAdded event func NewMemberAdded(channel, data string) MemberAdded { return MemberAdded{Event: "pusher_internal:member_added", Channel: channel, Data: data} } +// MemberRemoved event // { // "event": "pusher_internal:member_removed", // "channel": "presence-example-channel", @@ -237,6 +249,7 @@ type MemberRemoved struct { Data string `json:"data"` } +// NewMemberRemoved returns a new MemberRemoved event func NewMemberRemoved(channel string, userID string) MemberRemoved { data, err := json.Marshal(struct { UserID string `json:"user_id"` @@ -251,6 +264,7 @@ func NewMemberRemoved(channel string, userID string) MemberRemoved { return MemberRemoved{Event: "pusher_internal:member_removed", Channel: channel, Data: string(data)} } +// Raw event, usually used for client events // { // "event": "client-?", // "channel": "The channel", @@ -262,13 +276,14 @@ type Raw struct { Data json.RawMessage `json:"data"` } +// Response event type Response struct { Event string `json:"event"` Channel string `json:"channel"` Data interface{} `json:"data"` } -// The response event that is broadcasted to the client sockets +// NewResponse The response event that is broadcasted to the client sockets func NewResponse(name, channel string, data interface{}) Response { return Response{Event: name, Channel: channel, Data: data} } diff --git a/mocks/socket.go b/mocks/socket.go index cbba28f..938b138 100644 --- a/mocks/socket.go +++ b/mocks/socket.go @@ -4,6 +4,8 @@ package mocks // used in the test suite type MockSocket struct{} +// WriteJSON always returns nil +// used in the test suite func (s MockSocket) WriteJSON(i interface{}) error { return nil } diff --git a/storage/storage.go b/storage/storage.go index c260442..56c7f13 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -19,15 +19,18 @@ type Storage interface { AddApp(application *app.Application) error } +// InMemory in memory implementation of Storage type InMemory struct { sync.RWMutex Apps []*app.Application } +// NewInMemory returns an InMemory storage func NewInMemory() Storage { return &InMemory{} } +// AddApp adds app into memory func (db *InMemory) AddApp(application *app.Application) error { db.Lock() defer db.Unlock() diff --git a/subscription/subscription.go b/subscription/subscription.go index cb7e677..2e1dc75 100644 --- a/subscription/subscription.go +++ b/subscription/subscription.go @@ -6,14 +6,14 @@ package subscription import "ipe/connection" -// A Channel Subscription +// Subscription A Channel Subscription type Subscription struct { Connection *connection.Connection ID string Data string } -// Create a new Subscription +// New Create a new Subscription func New(conn *connection.Connection, data string) *Subscription { return &Subscription{Connection: conn, Data: data} } diff --git a/websockets/websocket.go b/websockets/websocket.go index ec74183..58c36d2 100644 --- a/websockets/websocket.go +++ b/websockets/websocket.go @@ -34,15 +34,17 @@ var upgrader = websocket.Upgrader{ }, } +// Websocket handler for real time websocket messages type Websocket struct { storage storage.Storage } +// NewWebsocket returns a new Websocket handler func NewWebsocket(storage storage.Storage) *Websocket { return &Websocket{storage: storage} } -// Websocket GET /app/{key} +// ServeHTTP Websocket GET /app/{key} func (h *Websocket) ServeHTTP(w http.ResponseWriter, r *http.Request) { conn, err := upgrader.Upgrade(w, r, nil) defer func() {