Skip to content

Commit 0312d5c

Browse files
committed
wired socket
1 parent baaf9aa commit 0312d5c

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

cmd/go-postgres-sockets/main.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"embed"
55
"github.com/bebo-dot-dev/go-postgres-sockets/server/api"
66
"github.com/bebo-dot-dev/go-postgres-sockets/server/database"
7+
"github.com/bebo-dot-dev/go-postgres-sockets/server/socket"
78
"github.com/gorilla/handlers"
9+
"github.com/gorilla/mux"
810
"io/fs"
911
"log"
1012
"net/http"
@@ -16,13 +18,15 @@ var embeddedFiles embed.FS
1618
func main() {
1719
log.Printf("Server started")
1820

19-
listener := database.NewPostgresDbListener()
20-
listener.Listen()
21-
2221
service := api.NewNotificationsApiService()
2322
controller := api.NewNotificationsApiController(service)
2423

2524
router := api.NewRouter(controller)
25+
26+
hub := registerSocketHandler(router)
27+
listener := database.NewPostgresDbListener(hub)
28+
listener.Listen()
29+
2630
router.PathPrefix("/").Handler(http.FileServer(getEmbeddedFileSystem()))
2731

2832
corsOrigins := handlers.AllowedOrigins([]string{"https://editor.swagger.io"})
@@ -31,6 +35,16 @@ func main() {
3135
log.Fatal(http.ListenAndServe(":8080", handlers.CORS(corsOrigins, corsHeaders, corsMethods)(router)))
3236
}
3337

38+
func registerSocketHandler(router *mux.Router) *socket.Hub {
39+
hub := socket.NewSocketHub()
40+
41+
router.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
42+
socket.ServeSocket(hub, w, r)
43+
})
44+
45+
return hub
46+
}
47+
3448
func getEmbeddedFileSystem() http.FileSystem {
3549
f, err := fs.Sub(embeddedFiles, "static")
3650
if err != nil {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module github.com/bebo-dot-dev/go-postgres-sockets
33
go 1.16
44

55
require (
6-
github.com/bebo-dot-dev/go-postgres-sockets/server v0.0.0-20210731215509-b681350459bc
6+
github.com/bebo-dot-dev/go-postgres-sockets/server v0.0.0-20210801091928-baaf9aa50a16
77
github.com/gorilla/handlers v1.5.1
88
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ github.com/bebo-dot-dev/go-postgres-sockets/server v0.0.0-20210731214855-0ea14f8
66
github.com/bebo-dot-dev/go-postgres-sockets/server v0.0.0-20210731214855-0ea14f8f9c02/go.mod h1:EDiz/1U4PJBnGV0L24ksnxeBKLo0CJZyH2mexZE1tMs=
77
github.com/bebo-dot-dev/go-postgres-sockets/server v0.0.0-20210731215509-b681350459bc h1:3UwDG57T6XauVnFRaAKet+jMQ59y6t1iVi9YcGITzGo=
88
github.com/bebo-dot-dev/go-postgres-sockets/server v0.0.0-20210731215509-b681350459bc/go.mod h1:EDiz/1U4PJBnGV0L24ksnxeBKLo0CJZyH2mexZE1tMs=
9+
github.com/bebo-dot-dev/go-postgres-sockets/server v0.0.0-20210801085520-11a525b5ee84 h1:anxb2aAXuGE++cezaozNHpSwZNnzk+6MG0U/ZZyBUEY=
10+
github.com/bebo-dot-dev/go-postgres-sockets/server v0.0.0-20210801085520-11a525b5ee84/go.mod h1:TvRvBsD2NAjwklFzsDjw8b0LFa06gMUJbDx/U8fisqQ=
11+
github.com/bebo-dot-dev/go-postgres-sockets/server v0.0.0-20210801091450-d390bf5b782e h1:SOuKnW3WANxPwlLRH/QRxH5DM3IraMNTD+xo428OoFE=
12+
github.com/bebo-dot-dev/go-postgres-sockets/server v0.0.0-20210801091450-d390bf5b782e/go.mod h1:TvRvBsD2NAjwklFzsDjw8b0LFa06gMUJbDx/U8fisqQ=
13+
github.com/bebo-dot-dev/go-postgres-sockets/server v0.0.0-20210801091928-baaf9aa50a16 h1:3rycyUBPl3DsXCt7ihx4Mzo6jPKTXeO0AJhBzt9GqvY=
14+
github.com/bebo-dot-dev/go-postgres-sockets/server v0.0.0-20210801091928-baaf9aa50a16/go.mod h1:TvRvBsD2NAjwklFzsDjw8b0LFa06gMUJbDx/U8fisqQ=
915
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
1016
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1117
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
@@ -20,6 +26,8 @@ github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH
2026
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
2127
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
2228
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
29+
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
30+
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
2331
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
2432
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
2533
github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8=

0 commit comments

Comments
 (0)