-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
111 lines (88 loc) · 2.43 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
package main
import (
"database/sql"
"github.com/cassiozareck/realchat/chat"
"github.com/cassiozareck/realchat/db"
"github.com/cassiozareck/realchat/shared"
"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
"log"
"net/http"
"strconv"
)
var conn *sql.DB
func main() {
conn = shared.ConnectToDB()
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Fatal("Error closing DB")
}
}(conn)
router := gin.Default()
router.GET("/chat", GetChat)
router.GET("/new", NewChat)
router.POST("/send", SendMessage)
log.Fatal(router.Run(":8080"))
}
// GetChat is an endpoint that will retrieve messages from a given chat
// id. It's response is a list of messages in json format with these spec:
//
// type Message struct {
// ID uint32 `json:"id"`
// Text string `json:"text"`
// Timestamp time.Time `json:"timestamp"`
// ChatID uint32 `json:"chat_id"`
// SenderID uint32 `json:"sender_id"`
// }
func GetChat(c *gin.Context) {
id := c.Query("id")
checkAndLog("Url Param 'id' is: " + id)
chatID, err := strconv.Atoi(id)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Conversion error: " + err.Error()})
return
}
chat, err := chat.GetChat(db.NewChatDBImp(conn), uint32(chatID))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
messages, err := chat.GetMessages()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, messages)
}
func NewChat(c *gin.Context) {
chat, err := chat.NewChat(db.NewChatDBImp(conn))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.String(http.StatusOK, strconv.Itoa(int(chat.GetID())))
}
func SendMessage(c *gin.Context) {
var incomingMessage shared.IncomingMessage
if err := c.ShouldBindJSON(&incomingMessage); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
chat, err := chat.GetChat(db.NewChatDBImp(conn), incomingMessage.ChatID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error getting chat: " + err.Error()})
return
}
err = chat.SendMessage(incomingMessage)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error sending message: " + err.Error()})
return
}
c.String(http.StatusOK, strconv.Itoa(int(chat.GetID())))
}
func checkAndLog(s string) {
if shared.LOG {
log.Println(s)
}
}