-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·48 lines (38 loc) · 944 Bytes
/
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
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"log"
"github.com/valyala/fasthttp"
)
var addr = flag.String("addr", ":8080", "http service address")
func serveHome(ctx *fasthttp.RequestCtx) {
log.Println(string(ctx.Path()))
if !ctx.IsGet() {
ctx.Error("Method not allowed", fasthttp.StatusMethodNotAllowed)
return
}
fasthttp.ServeFile(ctx, "../home.html")
}
func main() {
flag.Parse()
hub := newHub()
go hub.run()
requestHandler := func(ctx *fasthttp.RequestCtx) {
switch string(ctx.Path()) {
case "/":
serveHome(ctx)
case "/ws":
serveWs(ctx, hub)
default:
ctx.Error("Unsupported path", fasthttp.StatusNotFound)
}
}
server := fasthttp.Server{
Name: "ChatExample",
Handler: requestHandler,
}
log.Fatal(server.ListenAndServe(*addr))
}