-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
121 lines (97 loc) · 3.46 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Copyright (c) 2020.
* MbungeApp Inc all rights reserved
*/
package main
import (
"fmt"
"github.com/MbungeApp/mbunge-core/config"
userHandler "github.com/MbungeApp/mbunge-core/v1/user/handler"
userRepo "github.com/MbungeApp/mbunge-core/v1/user/repository"
_userService "github.com/MbungeApp/mbunge-core/v1/user/service"
mqtt "github.com/eclipse/paho.mqtt.golang"
"go.mongodb.org/mongo-driver/mongo"
"net"
eventHandler "github.com/MbungeApp/mbunge-core/v1/news/handler"
eventRepo "github.com/MbungeApp/mbunge-core/v1/news/repository"
_eventService "github.com/MbungeApp/mbunge-core/v1/news/service"
webinarHandler "github.com/MbungeApp/mbunge-core/v1/webinar/handler"
_webinarService "github.com/MbungeApp/mbunge-core/v1/webinar/service"
mpHandler "github.com/MbungeApp/mbunge-core/v1/mp/handler"
mpRepo "github.com/MbungeApp/mbunge-core/v1/mp/repository"
_mpService "github.com/MbungeApp/mbunge-core/v1/mp/service"
_dashboardHandler "github.com/MbungeApp/mbunge-core/v1/dashboard/handler"
_dashboardService "github.com/MbungeApp/mbunge-core/v1/dashboard/service"
"net/http"
_ "github.com/MbungeApp/mbunge-core/docs"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
echoSwagger "github.com/swaggo/echo-swagger"
)
var (
e *echo.Echo
mqttConn mqtt.Client
client *mongo.Client
)
func init() {
e = echo.New()
mqttConn = config.ConnectMqtt()
client = config.ConnectDB()
}
// @title Mbunge App API
// @version 1.0
// @description Version one of the api.
// @termsOfService http://dashboard.mbungeapp.tech/terms/
// @contact.name API Support
// @contact.url http://dashboard.mbungeapp.tech/support
// @contact.email 858wpwaweru@gmail.com
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:5000
func main() {
// middleware
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{http.MethodGet, http.MethodPut, http.MethodPost, http.MethodDelete},
}))
// User
userRepository := userRepo.NewUserRepository(client)
userService := _userService.NewUserServiceImpl(userRepository)
userHandler.NewUserRestHandler(e, userService)
// Events
eventRepository := eventRepo.NewEventRepository(client)
eventService := _eventService.NewEventService(eventRepository)
eventHandler.NewEventRestHandler(e, eventService)
// Webinar
webinarService := _webinarService.NewWebinarServiceImpl(client)
webinarHandler.NewParticipationRestHandler(e, webinarService)
webinarHandler.NewWebsocketHandler(e, webinarService)
webinar := webinarHandler.NewMqttWebinarHandler(&mqttConn)
// MP
mpRepository := mpRepo.NewMpRepository(client)
mpService := _mpService.NewMpService(mpRepository)
mpHandler.NewMpRestHandler(e, mpService)
// Dashboard
dashboardService := _dashboardService.NewDashboardServiceImpl(client)
_dashboardHandler.NewDashboardHandler(e, dashboardService)
// Swagger docs
e.GET("/swagger/*", echoSwagger.WrapHandler)
// mqtt
go listen(mqttConn, "topics/server", webinar.SystemActions)
e.Use(middleware.Logger())
e.Use(middleware.Recover())
l, err := net.Listen("tcp4", ":5000")
if err != nil {
e.Logger.Fatal(l)
}
e.Listener = l
e.Logger.Fatal(e.Start(""))
}
func listen(client mqtt.Client, topic string, handler mqtt.MessageHandler) {
token := client.Subscribe(topic, 0, handler)
if token != nil {
fmt.Printf("Subscribed to topic: %s\n", topic)
} else {
fmt.Printf("Failed to subscribe to topic: %s\n", topic)
}
}