-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (65 loc) · 2.22 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
package main
import (
"flag"
"fmt"
"go-chat/handlers"
"go-chat/middleware"
"net/http"
"os"
"go-chat/models"
"go-chat/socket"
"github.com/gorilla/mux"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/joho/godotenv"
)
func init() {
// Load the .env file
e := godotenv.Load()
if e != nil {
fmt.Print(e)
}
}
func main() {
// cli flags
logRequest := flag.Bool("logRequest", false, "Switch to turn on development configuration")
flag.Parse()
// Reference the db and close connection when this function returns
db := models.GetDB()
defer db.Close()
// Create a new websocket hub
wsHub := socket.NewHub()
// Create a router
router := mux.NewRouter()
// CORS middleware
router.Use(middleware.CORSHandler)
// TODO: Debug
// JWT middleware
// router.Use(middleware.JwtAuthentication)
if *logRequest {
router.Use(middleware.LogReqBody)
}
// Routes
router.HandleFunc("/user", handlers.GetUserHandler).Methods("GET", "OPTIONS")
router.HandleFunc("/users", handlers.GetUsersHandler).Methods("GET", "OPTIONS")
router.HandleFunc("/user/new", handlers.CreateUserHandler).Methods("POST", "OPTIONS")
router.HandleFunc("/login", handlers.Authenticate).Methods("POST", "OPTIONS")
// Ticketing route for ws authentication
router.HandleFunc("/ws/auth", handlers.HandleWebSocketAuth).Methods("POST", "OPTIONS")
// Websocket connection
router.HandleFunc("/ws/{roomID}", wsHub.HandleWebSocketConns).Methods("GET", "POST")
router.HandleFunc("/chat/conversations/new", handlers.CreateConversation).Methods("POST", "OPTIONS")
router.HandleFunc("/chat/conversations", handlers.GetConversationsByUserID).Methods("GET", "OPTIONS")
router.HandleFunc("/chat/conversations/{conversationID}", handlers.GetConversation).Methods("GET", "OPTIONS")
router.HandleFunc("/chat/conversations/{conversationID}/messages", handlers.GetMessagesByConversationID).Methods("GET", "OPTIONS")
// Start listening for incoming chat messages
// go handlers.HandleWebSocketMessages()
// Get port from .env file, we did not specify any port so this should return an empty string when tested locally
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
// Server
if err := http.ListenAndServe(":"+port, router); err != nil {
fmt.Print(err)
}
}