Skip to content

Commit d48a006

Browse files
authored
Merge pull request #6 from RO-29/feature_linter
linter changes + makefile + vendored
2 parents 060f9d1 + dddc692 commit d48a006

File tree

368 files changed

+161637
-65
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

368 files changed

+161637
-65
lines changed

Gopkg.lock

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Gopkg.toml example
2+
#
3+
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
4+
# for detailed Gopkg.toml documentation.
5+
#
6+
# required = ["github.com/user/thing/cmd/thing"]
7+
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
8+
#
9+
# [[constraint]]
10+
# name = "github.com/user/project"
11+
# version = "1.0.0"
12+
#
13+
# [[constraint]]
14+
# name = "github.com/user/project2"
15+
# branch = "dev"
16+
# source = "github.com/myfork/project2"
17+
#
18+
# [[override]]
19+
# name = "github.com/x/y"
20+
# version = "2.4.0"
21+
#
22+
# [prune]
23+
# non-go = false
24+
# go-tests = true
25+
# unused-packages = true
26+
27+
28+
[[constraint]]
29+
branch = "master"
30+
name = "github.com/alash3al/go-pubsub"
31+
32+
[[constraint]]
33+
branch = "master"
34+
name = "github.com/bclicn/color"
35+
36+
[[constraint]]
37+
name = "github.com/gorilla/websocket"
38+
version = "1.2.0"
39+
40+
[[constraint]]
41+
name = "github.com/labstack/echo"
42+
version = "3.2.6"
43+
44+
[prune]
45+
go-tests = true
46+
unused-packages = true

Makefile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
test:
2+
go test -v ./...
3+
4+
install-gometalinter:
5+
go get -v -u github.com/alecthomas/gometalinter
6+
gometalinter --install
7+
8+
LINT=$(eval export GOGC=400)\
9+
gometalinter --enable-all -D dupl -D lll -D gas -D goconst -D interfacer -D safesql -D test -D testify -D vetshadow\
10+
--tests --vendor --warn-unmatched-nolint --deadline=10m --concurrency=2 --enable-gc ./...
11+
lint: install-gometalinter
12+
$(LINT)
13+
14+
install-dep:
15+
go get -v -u github.com/golang/dep/cmd/dep
16+
17+
dep-init: install-dep
18+
dep init
19+
20+
dep-ensure: install-dep
21+
dep ensure
22+
23+
dep-update: install-dep
24+
dep ensure -update
25+
26+
.PHONY:: \
27+
test \
28+
lint \
29+
dep-ensure \
30+
dep-update \

flags.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,26 @@ import (
66
)
77

88
var (
9-
FLAG_HTTP_ADDR = flag.String("listen", ":4040", "the http address to listen on")
10-
FLAG_ALLOWED_ORIGIN = flag.String("origin", "*", "the allowed websocket origin(s), it accepts a comma separated list of domains, * means anything")
11-
FLAG_WEBHOOK_URL = flag.String("webhook", "http://localhost:8000", "the webhook")
12-
FLAG_WEBHOOK_EVENTS = flag.String("events", "connect,disconnect,subscribe,unsubscribe", "the events to be sent to the webhook")
13-
FLAG_PUBLISH_ENDPOINT = flag.String("publish", "/publish", "the publish endpoint, just make it as secure as you can")
14-
)
15-
16-
var (
17-
VERSION = "2.0"
18-
WEBHOOK_EVENTS = map[string]bool{}
9+
//FlagHTTPAddr ...
10+
FlagHTTPAddr = flag.String("listen", ":4040", "the http address to listen on")
11+
//FlagAllowedOrigin ...
12+
FlagAllowedOrigin = flag.String("origin", "*", "the allowed websocket origin(s), it accepts a comma separated list of domains, * means anything")
13+
//FlagWebhookURL ...
14+
FlagWebhookURL = flag.String("webhook", "http://localhost:8000", "the webhook")
15+
//FlagWebhookEvents ...
16+
FlagWebhookEvents = flag.String("events", "connect,disconnect,subscribe,unsubscribe", "the events to be sent to the webhook")
17+
//FlagPublishEndpoint ...
18+
FlagPublishEndpoint = flag.String("publish", "/publish", "the publish endpoint, just make it as secure as you can")
19+
//Version ...
20+
Version = "2.0"
21+
//WebhookEvents ..
22+
WebhookEvents = map[string]bool{}
1923
)
2024

25+
//InitFlags ...
2126
func InitFlags() {
2227
flag.Parse()
23-
for _, e := range strings.Split(strings.ToLower(*FLAG_WEBHOOK_EVENTS), ",") {
24-
WEBHOOK_EVENTS[strings.TrimSpace(e)] = true
28+
for _, e := range strings.Split(strings.ToLower(*FlagWebhookEvents), ",") {
29+
WebhookEvents[strings.TrimSpace(e)] = true
2530
}
2631
}

main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
56
"github.com/bclicn/color"
67
)
78

@@ -14,12 +15,12 @@ func main() {
1415
}()
1516

1617
// parsing the command line flags
17-
fmt.Println(color.BGreen("[*] Welcome to WSIFY"), color.BCyan(VERSION))
18+
fmt.Println(color.BGreen("[*] Welcome to WSIFY"), color.BCyan(Version))
1819
InitFlags()
1920

2021
// start the pub/sub server
21-
fmt.Println(color.BGreen("[*] Listening for connections on address"), color.BCyan(*FLAG_HTTP_ADDR), color.BGreen(" ..."))
22-
if err := InitWsServer(*FLAG_HTTP_ADDR); err != nil {
22+
fmt.Println(color.BGreen("[*] Listening for connections on address"), color.BCyan(*FlagHTTPAddr), color.BGreen(" ..."))
23+
if err := InitWsServer(*FlagHTTPAddr); err != nil {
2324
fmt.Println(color.BRed("[!] Error: ") + color.BLightYellow(err.Error()))
2425
return
2526
}

message.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package main
22

3-
// the message that will be polled from redis
3+
// Message that will be polled from redis
44
type Message struct {
55
Payload interface{} `json:"payload,omitempty"`
66
To []string `json:"to,omitempty"`
77
Topic string `json:"channel,omitempty"`
88
Time int64 `json:"time,omitempty"`
99
}
1010

11-
// checks whether the specified user is allowed
12-
// to recieve this message or not
11+
// IsUserAllowed checks whether the specified user is allowed
12+
// to receive this message or not
1313
func (m *Message) IsUserAllowed(u string) bool {
1414
if len(m.To) < 1 {
1515
return true

server.go

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import (
1414
)
1515

1616
var (
17-
// Default websocket upgrader
17+
// WSUpgrader is Default websocket upgrader
1818
WSUpgrader = websocket.Upgrader{
1919
ReadBufferSize: 1024,
2020
WriteBufferSize: 1024,
2121
CheckOrigin: func(r *http.Request) bool {
22-
for _, origin := range strings.Split(*FLAG_ALLOWED_ORIGIN, ",") {
22+
for _, origin := range strings.Split(*FlagAllowedOrigin, ",") {
2323
origin = strings.TrimSpace(origin)
2424
if origin == "*" || origin == r.Host {
2525
return true
@@ -30,11 +30,11 @@ var (
3030
EnableCompression: true,
3131
}
3232

33-
// Default broker
33+
//Broker default
3434
Broker = pubsub.NewBroker()
3535
)
3636

37-
// the websocket request handler
37+
// WSHandler is the websocket request handler
3838
func WSHandler(c echo.Context) error {
3939
defer (func() {
4040
if err := recover(); err != nil {
@@ -53,10 +53,10 @@ func WSHandler(c echo.Context) error {
5353
if err != nil {
5454
return nil
5555
}
56-
defer conn.Close()
56+
defer conn.Close() // nolint: errcheck
5757
subscriber, err := Broker.Attach()
5858
if err != nil {
59-
conn.WriteJSON(map[string]string{
59+
conn.WriteJSON(map[string]string{ // nolint: errcheck
6060
"error": "Sorry, couldn't allocate resources for you",
6161
})
6262
return nil
@@ -67,30 +67,7 @@ func WSHandler(c echo.Context) error {
6767
closeCh <- true
6868
return nil
6969
})
70-
go (func() {
71-
var action Event
72-
stop := false
73-
for !stop {
74-
if conn.ReadJSON(&action) != nil {
75-
stop = true
76-
break
77-
}
78-
if action.Action == "subscribe" || action.Action == "unsubscribe" {
79-
if !TriggerWebhook(Event{Action: action.Action, Key: key, Value: action.Value}) {
80-
conn.WriteJSON(map[string]string{
81-
"error": "You aren't allowed to access the requested resource",
82-
})
83-
continue
84-
}
85-
}
86-
if action.Action == "subscribe" {
87-
Broker.Subscribe(subscriber, action.Value)
88-
} else if action.Action == "unsubscribe" {
89-
Broker.Unsubscribe(subscriber, action.Value)
90-
}
91-
}
92-
closeCh <- true
93-
})()
70+
goRoutineAction(conn, closeCh, subscriber, key)
9471
for !closed {
9572
select {
9673
case <-closeCh:
@@ -114,31 +91,48 @@ func WSHandler(c echo.Context) error {
11491
return nil
11592
}
11693

117-
// publish handler
94+
func goRoutineAction(conn *websocket.Conn, closeCh chan bool, subscriber *pubsub.Subscriber, key string) {
95+
go (func() {
96+
var action Event
97+
for {
98+
if conn.ReadJSON(&action) != nil {
99+
break
100+
}
101+
if action.Action == "subscribe" || action.Action == "unsubscribe" {
102+
if !TriggerWebhook(Event{Action: action.Action, Key: key, Value: action.Value}) {
103+
conn.WriteJSON(map[string]string{ // nolint: errcheck
104+
"error": "You aren't allowed to access the requested resource",
105+
})
106+
continue
107+
}
108+
}
109+
if action.Action == "subscribe" {
110+
Broker.Subscribe(subscriber, action.Value)
111+
} else if action.Action == "unsubscribe" {
112+
Broker.Unsubscribe(subscriber, action.Value)
113+
}
114+
}
115+
closeCh <- true
116+
})()
117+
}
118+
119+
// PublishHandler ...
118120
func PublishHandler(c echo.Context) error {
119121
var msg Message
120122
if err := c.Bind(&msg); err != nil {
121123
return c.JSON(422, map[string]interface{}{
122-
"sucess": false,
123-
"error": err.Error(),
124+
"success": false,
125+
"error": err.Error(),
124126
})
125127
}
126128
Broker.Broadcast(msg, msg.Topic)
127-
return c.JSON(200, map[string]interface{}{
128-
"sucess": true,
129-
"data": msg,
130-
})
131-
}
132-
133-
// welcome handler
134-
func WelcomeHandler(c echo.Context) error {
135129
return c.JSON(200, map[string]interface{}{
136130
"success": true,
137-
"message": "let's go ?",
131+
"data": msg,
138132
})
139133
}
140134

141-
// start the websocket server
135+
// InitWsServer start the websocket server
142136
func InitWsServer(addr string) error {
143137
e := echo.New()
144138

@@ -151,7 +145,7 @@ func InitWsServer(addr string) error {
151145
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{Level: 9}))
152146

153147
e.GET("/subscribe", WSHandler)
154-
e.POST(*FLAG_PUBLISH_ENDPOINT, PublishHandler)
148+
e.POST(*FlagPublishEndpoint, PublishHandler)
155149

156150
return e.Start(addr)
157151
}

0 commit comments

Comments
 (0)