-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (64 loc) · 2.09 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
package main
import (
"fmt"
"log"
"net/http"
"github.com/Websocketification/wstf"
"github.com/gorilla/websocket"
)
// Set up main router
var mRouter = wstf.NewRouter()
func main() {
app := wstf.NewApplication(mRouter)
// The middleware use "*" to match everything rather than match the child routers.
mRouter.Use(".*").All(func(req *wstf.Request, res *wstf.Response, next func()) {
fmt.Println("Process cookies here!")
next()
}).All(func(req *wstf.Request, response *wstf.Response, next func()) {
fmt.Println("Request database!")
next()
}).Get(func(req *wstf.Request, response *wstf.Response, next func()) {
fmt.Println("All GET requests!")
next()
}).Post(func(req *wstf.Request, res *wstf.Response, next func()) {
fmt.Println("Log post requests here!")
next()
})
mRouter.Use("/").Get(func(req *wstf.Request, res *wstf.Response, next func()) {
res.Done("Hello, this is a WebSocket server.")
})
// Set up sub router.
mSubRouter := wstf.NewRouter()
type User struct {
ID string
Name string
}
mSubRouter.Use("/{userName}").Get(func(req *wstf.Request, res *wstf.Response, next func()) {
res.Done(User{ID: "Fisher", Name: "Awesome Fisher"})
})
// All else requests.
mSubRouter.Use(".*").All(func(req *wstf.Request, res *wstf.Response, next func()) {
fmt.Println("The request is not processed!")
})
// Adding sub router uses empty string to match the child routers.
mRouter.Push("/users", mSubRouter)
// Redirect requests to wstf handler func.
http.HandleFunc("/WebSocketServer", app.GetWebsocketHandlerFunc(
&websocket.Upgrader{},
func(w http.ResponseWriter, r *http.Request) {
fmt.Println("The http request is not upgradable:", r.URL.Path)
},
func(conn *wstf.Connection, w http.ResponseWriter) (*http.Header, bool) {
// Allow all upgradable request to be upgraded.
return nil, true
},
func(err error, w http.ResponseWriter, r *http.Request) {
fmt.Println("Failed to upgrade to WebSocket:", r.URL.Path)
},
))
fmt.Println("Server will be running at 127.0.0.1:3333")
err := http.ListenAndServe("127.0.0.1:3333", nil)
if err != nil {
log.Fatal(err)
}
}