-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (50 loc) · 909 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
57
58
59
60
61
package main
import (
"bufio"
"fmt"
"io"
"net"
"reflect"
"strings"
)
func main() {
listener, err := net.Listen("tcp", ":6379")
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Listening on port 6379...")
initializeRdb()
for {
connection, err := listener.Accept()
if err != nil {
fmt.Println(err)
return
}
go func() {
resp := bufio.NewReader(connection)
defer connection.Close()
for {
v, err := parse(resp)
if err != nil {
if err == io.EOF {
return
}
fmt.Println(err)
}
if reflect.DeepEqual(v, Value{}) {
continue
}
command := strings.ToUpper(v.array[0].bulk)
args := v.array[1:]
handler, ok := handlers[command]
if !ok {
connection.Write(writeRESP(unknownCommand(v.array[0].bulk)))
continue
}
result := handler(args)
connection.Write(writeRESP(result))
}
}()
}
}