-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
41 lines (33 loc) · 622 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
package main
import (
"log"
"net"
)
func main() {
ln, err := net.Listen("tcp", ":6379")
if err != nil {
log.Panic(err)
}
log.Println("Listening on port 6379.")
for {
conn, err := ln.Accept()
if err != nil {
log.Println(err)
}
go handleConn(conn)
}
}
func handleConn(conn net.Conn) {
defer func() {
if r := recover(); r != nil {
log.Println(conn.RemoteAddr(), " terminated.")
}
}()
defer conn.Close()
log.Println("New connection from ", conn.RemoteAddr())
for {
command := parseResp(conn)
response := handler(command)
conn.Write([]byte(response))
}
}