-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go~
More file actions
63 lines (46 loc) · 1.28 KB
/
main.go~
File metadata and controls
63 lines (46 loc) · 1.28 KB
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
package main
import (
"flag"
"github.com/julienschmidt/httprouter"
"github.com/valiknet18/WebSocketChat/model"
"log"
"net/http"
"os"
"text/template"
"github.com/rs/cors"
)
func serveHome(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if r.URL.Path == "/static/js/app.js" {
http.ServeFile(w, r, "static/js/app.js")
return
}
if r.URL.Path == "/static/css/app.css" {
http.ServeFile(w, r, "static/css/app.css")
return
}
if r.URL.Path == "/static/templates/chat.html" {
http.ServeFile(w, r, "static/templates/chat.html")
return
}
homeTempl := template.Must(template.ParseFiles("static/index.html"))
w.Header().Set("Content-Type", "text/html; charset=utf-8")
homeTempl.Execute(w, r.Host)
}
func main() {
//TODO user httproute package
r := httprouter.New()
r.GET("/ws/:userHash/connect", model.ConnectToRoom)
r.GET("/", serveHome)
r.POST("/room/create", model.CreateRoom)
r.GET("/room/get", model.GetRooms)
r.GET("/room/users/:roomHash", model.GetRoomUsers)
r.POST("/user/connect", model.ConnectUser)
r.ServeFiles("/static/*filepath", http.Dir("./static/"))
flag.Parse()
handler := cors.Default().Handler(r)
err := http.ListenAndServe(":8000", handler)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
log.Println("Server started ")
}