-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (75 loc) · 2.2 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
package main
import (
"context"
"fmt"
"log"
"net"
"net/http"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/quangkhaidam93/go-web-server/chat"
"github.com/quangkhaidam93/go-web-server/controllers"
"github.com/quangkhaidam93/go-web-server/example/kafka"
"github.com/quangkhaidam93/go-web-server/middlewares"
"github.com/quangkhaidam93/go-web-server/models"
"google.golang.org/grpc"
)
func main() {
// gRPC Server
go func() {
lis, err := net.Listen("tcp", ":3002")
if err != nil {
panic("[Error] failed to listen on port 3002 due to: " + err.Error())
}
grpcServer := grpc.NewServer()
chatServer := chat.Server{}
chat.RegisterChatServiceServer(grpcServer, &chatServer)
if err := grpcServer.Serve(lis); err != nil {
panic("[Error] failed to start gRPC server due to: " + err.Error())
}
}()
// Http Server
users := []models.BasicUser{
{ID: 1, Name: "Emma"},
{ID: 2, Name: "Bruno"},
{ID: 3, Name: "Rick"},
{ID: 4, Name: "Lena"},
}
if err := godotenv.Load(); err != nil {
log.Fatal(err)
}
router := gin.Default()
models.ConnectDatabase()
// Kafka producer
producer, err := kafka.SetupProducer()
if err != nil {
fmt.Println(err)
}
defer producer.Close()
// Kafka consumer
store := &kafka.NotificationStore{
Data: make(kafka.UserNotifications),
}
ctx, cancel := context.WithCancel(context.Background())
go kafka.SetupConsumerGroup(ctx, store)
defer cancel()
// Router handler
router.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "ok"})
})
public := router.Group("/api")
public.GET("/posts", controllers.FindPosts)
public.GET("/posts/:id", controllers.FindPost)
public.POST("/login", controllers.Login)
public.POST("/sign-up", controllers.SignUp)
public.GET("/ping", controllers.CallGrpc)
public.POST("/send", kafka.SendMessageHandler(producer, users))
protected := router.Group("/api")
protected.Use(middlewares.JwtAuthMiddleware)
protected.POST("/posts", controllers.CreatePost)
protected.PATCH("/posts/:id", controllers.UpdatePost)
protected.DELETE("/posts/:id", controllers.DeletePost)
if err := router.Run(":3001"); err != nil {
panic("[Error] failed to start Gin server due to: " + err.Error())
}
}