-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (50 loc) · 971 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
49
50
51
52
53
54
55
56
package main
import (
"github.com/medusar/lucas/command"
"github.com/medusar/lucas/protocol"
"log"
"net"
"net/http"
_ "net/http/pprof"
)
func main() {
l, err := net.Listen("tcp", ":6380")
if err != nil {
log.Fatal(err)
}
defer l.Close()
log.Println("server stared on address:", l.Addr())
go http.ListenAndServe(":8080", http.DefaultServeMux)
//start server
go command.LoopAndInvoke()
for {
con, err := l.Accept()
if err != nil {
log.Fatal(err)
}
go serve(con)
}
}
func serve(con net.Conn) {
defer con.Close()
//r := protocol.NewRedisConn(con)
r := protocol.NewBufRedisConn(con)
for {
req, err := r.ReadRequest()
log.Println("req:", req)
if err != nil {
log.Println("Failed to read,", err)
break
}
redisCmd, err := command.ParseRequest(req)
if err != nil {
r.WriteError(err.Error())
continue
}
err = command.Execute(r, redisCmd)
if err != nil {
log.Println("Failecd to write", err)
break
}
}
}