-
Notifications
You must be signed in to change notification settings - Fork 0
/
httphandlers.go
200 lines (169 loc) · 4.55 KB
/
httphandlers.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/jinzhu/gorm"
"time"
"natschat/components/chat"
"natschat/components/users"
"natschat/config"
"natschat/utils/apierrs"
"natschat/utils/auth"
"natschat/utils/pagination"
//"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"net/http"
)
type Server struct {
config *config.Config
jwt *auth.JWT
db *gorm.DB
hub *Hub
gnats *Gnats
userService *users.Service
chatService *chat.Service
}
func (s *Server) Run() {
r := gin.New()
r.Use(gin.Recovery())
if s.config.Debug {
r.Use(gin.Logger())
}
if s.config.Environment == config.EnvironmentLocal {
corsCfg := cors.DefaultConfig()
corsCfg.AllowAllOrigins = true
corsCfg.AddAllowHeaders("Authorization")
r.Use(cors.New(corsCfg))
}
r.POST("/register", s.registerUser)
r.POST("/login", s.loginHandler)
api := r.Group("/api")
api.Use(s.jwt.AuthenticateUserJWT)
api.POST("/rooms", s.createChatRoomHandler)
api.GET("/rooms", s.listChatRoomsHandler)
api.GET("/rooms/:id/history", s.chatHistoryHandler)
r.GET("/ws", func(c *gin.Context) {
s.serveWs(c.Writer, c.Request)
})
err := r.Run(s.config.Server.Address)
if err != nil {
log.Fatalf("Server died with error: %v", err)
}
}
// serveWs handles websocket requests from the peer.
func (s *Server) serveWs(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
u, err := authenticateSocket(conn, s.jwt)
if err != nil {
if err1 := conn.WriteJSON(Message{
Type: MessageTypeUnauthorizedErr,
Body: ServerErrorMessage{
Message: "Auth error occurred",
},
}); err != nil {
log.Errorf("Got err while sending auth message: %v", err1)
}
if err2 := conn.Close(); err2 != nil {
log.Errorf("Got err while closing socket during auth: %v", err2)
}
return
}
m := Message{
Type: MessageTypeAuthAck,
Body: "Authentication success",
}
if err := conn.WriteJSON(&m); err != nil {
log.Errorf("Err while authack %v", err)
if err2 := conn.Close(); err2 != nil {
log.Errorf("Got err while closing socket during auth: %v", err2)
}
return
}
client := newClient(s.hub, s.gnats, conn, s.chatService, u)
client.hub.register <- client
// Allow collection of memory referenced by the caller by doing all work in
// new goroutines.
go client.writePump()
go client.readPump()
}
type AuthRequest struct {
Token string `binding:"required"`
}
func authenticateSocket(conn *websocket.Conn, jwt *auth.JWT) (*auth.JWTUser, error) {
if err := conn.SetReadDeadline(time.Now().Add(10 * time.Second)); err != nil {
log.Errorf("Error occurred while setting write deadline during auth: %v", err)
return nil, err
}
var r AuthRequest
if err := conn.ReadJSON(&r); err != nil {
return nil, err
}
j, err := jwt.ParseAndValidateJWT(r.Token)
if err != nil {
return nil, err
}
return &j, nil
}
func (s *Server) registerUser(c *gin.Context) {
var ur users.CreateUserRequest
if err := c.ShouldBindJSON(&ur); err != nil {
apierrs.HandleValidationError(c, err)
return
}
u, err := s.userService.CreateUser(ur)
if err != nil {
err.HandleResponse(c)
return
}
c.JSON(http.StatusCreated, u)
}
func (s *Server) loginHandler(c *gin.Context) {
var body users.LoginRequest
if err := c.ShouldBindJSON(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"detail": err.Error()})
return
}
var authResponse users.AuthResponseDTO
var err *apierrs.APIError
if authResponse, err = s.userService.LoginUser(body); err != nil {
err.HandleResponse(c)
return
}
c.JSON(http.StatusOK, authResponse)
}
func (s *Server) createChatRoomHandler(c *gin.Context) {
body := chat.CreateChatRoomRequest{}
if err := c.ShouldBindJSON(&body); err != nil {
apierrs.HandleValidationError(c, err)
return
}
u := auth.GetUserOrPanic(c)
r, err := s.chatService.CreateChatRoom(body.Name, u.ID)
if err != nil {
err.HandleResponse(c)
return
}
c.JSON(http.StatusCreated, r)
}
func (s *Server) listChatRoomsHandler(c *gin.Context) {
rooms, err := s.chatService.ListChatRooms()
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, err.ResponseJSON())
return
}
c.JSON(http.StatusOK, pagination.PageResponse{Results: rooms})
}
func (s *Server) chatHistoryHandler(c *gin.Context) {
id := c.Param("id")
msgs, err := s.chatService.GetChatHistory(id)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, err.ResponseJSON())
return
}
c.JSON(200, pagination.PageResponse{Results: msgs})
}