-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.go
44 lines (37 loc) · 1.19 KB
/
views.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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/google/uuid"
)
func MustRead(file string) string {
b, err := os.ReadFile(file)
if err != nil {
log.Fatalf("failed to read file %s: %s", file, err)
}
return string(b)
}
var indexHTML = MustRead("./frontend/dist/home.html")
var roomHTML = MustRead("./frontend/dist/app.html")
// The home page serves a page with a button to create a new room
func HomeHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(indexHTML))
}
// NewRoomHandler just creates a UUID for a new room, then redirects the user.
//
// It would be cool to store all recent room IDs in a cookie,
// then render the list on the homepage for a user to return to.
// But for now we just forward them to a room URL, which will autogenerate the room.
func NewRoomHandler(w http.ResponseWriter, r *http.Request) {
// Homepage requested a new room
//TODO could instead do some sort of TinyURL style Base58(SHA256(url, username))
id := uuid.New()
roomPath := fmt.Sprintf("/room/%s", id)
http.Redirect(w, r, roomPath, http.StatusFound)
}
// RoomHandler serves the room assets
func RoomHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(roomHTML))
}