-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
74 lines (62 loc) · 1.41 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
package main
import (
"fmt"
"io"
"net"
"github.com/mmacdo54/go-redis-clone/internal/configuration"
"github.com/mmacdo54/go-redis-clone/internal/connection"
"github.com/mmacdo54/go-redis-clone/internal/handlers"
"github.com/mmacdo54/go-redis-clone/internal/resp"
"github.com/mmacdo54/go-redis-clone/internal/storage"
)
func main() {
store, err := storage.InitStore()
if err != nil {
fmt.Println(err)
panic(err)
}
config, err := configuration.InitConfig()
if err != nil {
fmt.Println(err)
panic(err)
}
l, err := net.Listen("tcp", ":6379")
if err != nil {
fmt.Println(err)
panic(err)
}
fmt.Println("Listening for tcp connections on port 6379")
for {
conn, err := l.Accept()
if err != nil {
fmt.Println(err)
continue
}
go handleConnection(conn, store, config)
}
}
func handleConnection(conn net.Conn, store storage.Store, config configuration.Config) {
defer conn.Close()
c := connection.NewConnection(&conn)
if !config.Requirepass {
c.Validated = true
}
for {
reader := resp.NewRespReader(conn)
writer := resp.NewRespWriter(conn)
val, err := reader.ReadResp()
if err != nil {
if err == io.EOF {
fmt.Println("Client disconnected")
break
}
if err := writer.WriteErrorResp(err); err != nil {
fmt.Println(err)
}
}
response := handlers.HandleRespValue(val, &c, store, config)
if response.Type != resp.TYPE_VOID {
writer.WriteResp(response)
}
}
}