-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
52 lines (45 loc) · 1.08 KB
/
server.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
package aqua
import (
"net/http"
"github.com/ForeverZi/aqua/encoder"
"github.com/ForeverZi/aqua/handler"
"github.com/ForeverZi/aqua/log"
"github.com/ForeverZi/aqua/wconn"
)
func NewServer() *Server {
logger := log.New()
mux := handler.NewExHandler(encoder.JSON)
return &Server{
Logger: logger,
Mux: mux,
}
}
type Server struct {
Logger wconn.Logger
Mux handler.Mux
}
func (s *Server) ListenAndServe(addr string) *http.Server {
logger := s.Logger
hub := wconn.NewHub(wconn.CustomerMsgHandler(s.Mux),
wconn.SetLogger(logger))
mux := http.NewServeMux()
mux.Handle("/ws", hub)
server := &http.Server{
Handler: mux,
Addr: addr,
}
go func() {
logger.Println("aqua start", "listen:", addr)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Println("aqua interrupt", "err", err)
}
logger.Println("aqua server stopped...")
}()
return server
}
func (s *Server) HandleFunc(code handler.ActionCode, f handler.ResponseFunc){
s.Mux.HandleFunc(code, f)
}
func (s *Server) Encoder() encoder.MsgProto {
return s.Mux.Encoder()
}