-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (49 loc) · 1.06 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
package main
import (
"fmt"
"io"
"net"
"os"
)
const addr = "0.0.0.0:6379"
type Store map[string]*StoreItem
var store = make(Store)
func main() {
l, err := net.Listen("tcp", addr)
if err != nil {
fmt.Printf("Failed to bind to %s\n", addr)
os.Exit(1)
}
persistenceEngine := NewPersistenceEngine(&store)
persistenceEngine.Start()
fmt.Printf("Starting Redis server at %s\n", addr)
// Listen for inputs and respond
for {
conn, err := l.Accept()
if err != nil {
panic(err)
}
go func(conn net.Conn) {
defer conn.Close()
buf := make([]byte, 2014) // store out stuff somewhere
for {
len, err := conn.Read(buf)
if err != nil {
if err != io.EOF {
fmt.Printf("Error reading: %#v\n", err)
}
break
}
// Parse the command out
command := buf[:len]
response := Parse(command, persistenceEngine)
// Write the response back to the connection
_, responseErr := conn.Write([]byte(response + "\r\n"))
if responseErr != nil {
fmt.Printf("Error reading: %#v\n", err)
break
}
}
}(conn)
}
}