-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
51 lines (36 loc) · 1.01 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
package main
import (
"flag"
"github.com/julienschmidt/httprouter"
"github.com/valiknet18/WebSocketChat/model"
"log"
"net/http"
"text/template"
"github.com/rs/cors"
)
func serveHome(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
t, err := template.ParseFiles("static/index.html")
if err != nil {
log.Panic(err)
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
t.Execute(w, nil)
}
func main() {
//TODO user httproute package
r := httprouter.New()
r.GET("/ws/:roomHash/connect/:userHash", model.ConnectToRoom)
r.GET("/", serveHome)
r.POST("/users/create", model.UserCreate)
r.POST("/rooms/create", model.CreateRoom)
r.GET("/rooms/get", model.GetRooms)
r.GET("/rooms/users/:roomHash", model.GetRoomUsers)
r.ServeFiles("/static/*filepath", http.Dir("./static/"))
flag.Parse()
handler := cors.Default().Handler(r)
log.Println("Server running on port: 8000")
err := http.ListenAndServe(":8000", handler)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}