-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
230 lines (196 loc) · 5.38 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"flag"
"fmt"
"html/template"
"log"
"net/http"
"os"
"time"
"github.com/joho/godotenv"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/pkg/errors"
"gorm.io/datatypes"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
var templates = template.Must(template.ParseGlob("frontend/*.html"))
type Env struct {
db *gorm.DB
// TODO: Should this be a map to user ID?
// TODO: Add time to live to the session id
sessions map[uuid.UUID]string
Environment string
}
func (env *Env) getUserFromSession(sessionId uuid.UUID) (User, error) {
username, ok := env.sessions[sessionId]
if ok {
log.Printf("Session %s is valid", sessionId.String())
user := User{}
// TODO: Check the err here
env.db.Where("username = ?", username).First(&user)
return user, nil
} else {
return User{}, errors.New(fmt.Sprintf("Session %s not found", sessionId.String()))
}
}
func getSessionIdFromCookie(c *gin.Context) (uuid.UUID, error) {
cookie, err := c.Cookie("sessionId")
if err != nil {
return uuid.Nil, err
}
sessionId, err := uuid.Parse(cookie)
if err != nil {
return uuid.Nil, err
}
return sessionId, nil
}
func (env *Env) ServeHome(c *gin.Context) {
writer := c.Writer
request := c.Request
log.Println(request.URL)
if request.URL.Path != "/" {
http.Error(writer, "Not found", http.StatusNotFound)
return
}
if request.Method != "GET" {
http.Error(writer, "Method not allowed", http.StatusMethodNotAllowed)
return
}
board := Board{}
if c.Query("boardId") != "" {
db.First(&board, c.Query("boardId"))
}
templateVars := map[string]interface{}{}
sessionId, _ := getSessionIdFromCookie(c)
user, err := env.getUserFromSession(sessionId)
if err != nil {
templateVars["loggedIn"] = false
err = templates.ExecuteTemplate(writer, "whiteboard.html", templateVars)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
}
return;
} else {
templateVars["loggedIn"] = true
}
userIsMemberOfBoard := env.isUserMemberOfBoard(user, board)
if !userIsMemberOfBoard {
templateVars["error"] = "You don't have permission for this board"
} else {
templateVars["boardName"] = board.BoardName
templateVars["boardId"] = board.ID
}
if board.ID == 0 && user.ID != 0 {
// User is logged in but hasn't selected a board, not an error
templateVars["error"] = nil
}
templateVars["env"] = env.Environment
err = templates.ExecuteTemplate(writer, "whiteboard.html", templateVars)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
}
}
type Point struct {
X float32 `json:"x"`
Y float32 `json:"y"`
}
type Line struct {
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
Id uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primary_key"`
Points datatypes.JSON
BoardId int
Board Board
}
type Board struct {
gorm.Model
BoardName string `gorm:"not null"`
}
type User struct {
gorm.Model
Username string `gorm:"unique;not null"`
PasswordHash string `gorm:"not null"`
}
type BoardMember struct {
BoardID uint `gorm:"primaryKey;autoincrement:false"`
UserID uint `gorm:"primaryKey;autoincrement:false"`
CreatedAt time.Time
UpdatedAt time.Time
}
var db *gorm.DB
func main() {
// TODO: format check in ws
// TODO: Auth middleware rather than manual auth in each route
flag.Parse()
log.SetFlags(0)
err := godotenv.Load()
if err != nil {
log.Println("Could not load .env file")
}
db, err = gorm.Open(postgres.Open(os.Getenv("DATABASE_URL")), &gorm.Config{})
if err != nil {
panic("Failed to connect to database %v")
}
err = db.AutoMigrate(&Board{})
if err != nil {
log.Fatalf("Failed to migrate %v: ", err)
}
err = db.AutoMigrate(&Line{})
if err != nil {
log.Fatalf("Failed to migrate %v: ", err)
}
err = db.AutoMigrate(&User{})
if err != nil {
log.Fatalf("Failed to migrate %v: ", err)
}
err = db.AutoMigrate(&BoardMember{})
if err != nil {
log.Fatalf("Failed to migrate %v: ", err)
}
boardHubs := make([]*Hub, 0)
r := gin.Default()
// TODO: Move over to using gin for template rendering
r.LoadHTMLGlob("frontend/*.html")
environmentToRun := os.Getenv("ENV")
if environmentToRun == "" {
environmentToRun = "dev"
}
env := &Env{db: db, sessions: make(map[uuid.UUID]string), Environment: environmentToRun}
log.Printf("Running in %s mode", env.Environment)
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "Pong",
})
})
r.GET("/ws", func(context *gin.Context) {
// TODO: Surely can just pass a slice?
tempBoardHubs, err := env.serveWs(boardHubs, context)
if err != nil {
log.Printf("User couldn't join board")
err = templates.ExecuteTemplate(context.Writer, "notAuthorized.html", nil)
if err != nil {
http.Error(context.Writer, err.Error(), http.StatusInternalServerError)
}
return
}
boardHubs = tempBoardHubs
})
r.GET("/", env.ServeHome)
r.POST("/board", env.PostBoard)
r.GET("/board", env.NewBoard)
r.GET("/board/:boardId", env.GetBoard)
r.POST("/signup", env.CreateUser)
r.GET("/signup", env.NewUser)
r.POST("/signin", env.SignInUser)
r.GET("/signin", env.SignInPage)
r.GET("/user/:userId", env.GetUser)
r.GET("/boards", env.GetBoardsForUser)
r.POST("/board/:boardId/add_user", env.AddUserToBoard)
r.POST("/board/:boardId/remove_user", env.RemoveUserFromBoard)
r.GET("/board/:boardId/members", env.GetBoardMembers)
port := os.Getenv("PORT")
r.Run(":" + port)
}