-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
103 lines (89 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"fmt"
"log"
"my-redis-go/operations"
"my-redis-go/resp"
"net"
)
//TODO: use config to pass host and port and other params
// func setupFlags() {
// flag.StringVar(&config.Host, "host", "0.0.0.0", "host for the dice server")
// flag.IntVar(&config.Port, "port", 7379, "port for the dice server")
// flag.Parse()
// }
type Server struct {
listenAddr string
listener net.Listener
quitCh chan struct{}
}
func spinServer(listenAddr string) *Server {
return &Server{
listenAddr: listenAddr,
quitCh: make(chan struct{}),
}
}
func (server *Server) Start() error {
listener, err := net.Listen("tcp", server.listenAddr)
if err != nil {
log.Fatal(err)
}
defer listener.Close()
server.listener = listener
go server.establishConnection()
<-server.quitCh
return nil
}
func (server *Server) establishConnection() {
for {
fmt.Println("Accepting New Connections\n")
conn, err := server.listener.Accept()
if err != nil {
log.Fatal(err)
}
fmt.Println("New Connection:", conn.RemoteAddr())
go server.requestHandler(conn)
}
}
func (server *Server) requestHandler(conn net.Conn) {
defer conn.Close()
for {
buf := make([]byte, 2048)
n, err := conn.Read(buf)
if err != nil {
print("hello error")
log.Fatal(err)
}
msg := buf[:n]
// print("hello")
// print(string(msg))
// echo := "+OK\r\n"
commands, _ := resp.ParseRequest(msg)
for _, command := range commands {
dataType, err := operations.ExecuteCommand(command)
fmt.Println(dataType.ToString())
if err != resp.EmptyRedisError {
fmt.Println("else error")
conn.Write([]byte(err.ToString() + "\n"))
} else {
fmt.Println("else")
if dataType == nil {
fmt.Println("else nil")
conn.Write([]byte("(nil)" + "\n"))
} else {
fmt.Println("elsewow")
out := fmt.Sprintf("$%d\r\n%s\r\n", len(dataType.ToString()), dataType.ToString())
fmt.Println(out)
conn.Write([]byte(out))
}
}
}
// conn.Write([]byte(echo))
// log.Println("Done Writing!")
fmt.Println(string(msg))
}
}
func main() {
server := spinServer(":4000")
server.Start()
}