-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
134 lines (118 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
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"context"
"errors"
"log"
"net/http"
"os"
"text/template"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/gorilla/websocket"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Config struct {
db *databaseConfig
tmpl *templateConfig
upgrader websocket.Upgrader
JWT_SECRET []byte
Clients []Client
}
type templateConfig struct {
home *template.Template
login *template.Template
signup *template.Template
chat *template.Template
}
type databaseConfig struct {
mongoClient *mongo.Client
userColl *mongo.Collection
roomColl *mongo.Collection
messageColl *mongo.Collection
}
type Client struct {
Conn *websocket.Conn
Username string
RoomCode int
}
func main() {
cfg, err := initalizeCfg()
if err != nil {
log.Fatal("error initalizing cfg : " + err.Error())
}
defer cfg.db.mongoClient.Disconnect(context.TODO())
r := chi.NewRouter()
r.Use(middleware.Logger)
fs := http.FileServer(http.Dir("./static"))
r.Handle("/static/*", http.StripPrefix("/static/", fs))
r.Get("/", cfg.homeHandler)
r.Get("/login", cfg.GetLoginHandler)
r.Post("/login", cfg.PostLoginHandler)
r.Get("/signup", cfg.GetSignupHandler)
r.Post("/signup", cfg.PostSignupHandler)
r.Post("/room/create", cfg.createRoomHandler)
r.Post("/room/join", cfg.joinRoomHandler)
r.Get("/chat/{code:[0-9]+}", cfg.chatHandler)
r.Get("/ws/{code:[0-9]+}", cfg.wsHandler)
log.Println("listening on http://localhost:8080")
http.ListenAndServe("localhost:8080", r)
}
func initalizeCfg() (Config, error) {
homeTemplate, err := template.ParseFiles("./templates/home.html")
if err != nil {
return Config{}, errors.New("error parsing home.html : " + err.Error())
}
loginTemplate, err := template.ParseFiles("./templates/login.html")
if err != nil {
return Config{}, errors.New("error parsing login.html : " + err.Error())
}
signupTemplate, err := template.ParseFiles("./templates/signup.html")
if err != nil {
return Config{}, errors.New("error parsing signup.html : " + err.Error())
}
chatTemplate, err := template.ParseFiles("./templates/chat.html")
if err != nil {
return Config{}, errors.New("error parsing chat.html : " + err.Error())
}
tmpl := templateConfig{
home: homeTemplate,
login: loginTemplate,
signup: signupTemplate,
chat: chatTemplate,
}
upgrader := websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
if err = godotenv.Load(); err != nil {
return Config{}, errors.New("error .env file not found : " + err.Error())
}
JWT_SECRET := os.Getenv("JWT_SECRET")
if JWT_SECRET == "" {
return Config{}, errors.New("error JWT_SECRET not found in .env : " + err.Error())
}
MONGO_URI := os.Getenv("MONGO_URI")
if MONGO_URI == "" {
return Config{}, errors.New("error MONGO_URI not found in .env : " + err.Error())
}
mongoClient, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(MONGO_URI))
if err != nil {
return Config{}, errors.New("error could not connect to mongoDB : " + err.Error())
}
dbCfg := databaseConfig{
mongoClient: mongoClient,
userColl: mongoClient.Database("sckt").Collection("user"),
roomColl: mongoClient.Database("sckt").Collection("room"),
messageColl: mongoClient.Database("sckt").Collection("message"),
}
cfg := Config{
db: &dbCfg,
tmpl: &tmpl,
upgrader: upgrader,
JWT_SECRET: []byte(JWT_SECRET),
Clients: []Client{},
}
return cfg, nil
}