-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
142 lines (118 loc) · 3.03 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// the value object looks like this when the command is SET name sabin
// Value{
// typ: "array",
// array: []Value{
// Value{typ: "bulk", bulk: "SET"},
// Value{typ: "bulk", bulk: "name"},
// Value{typ: "bulk", bulk: "sabin"},
// },
// }
package main
import (
"context"
"flag"
"fmt"
"net"
"os"
"strings"
"github.com/cristalhq/acmd"
)
func main() {
config := &Configuration{}
cmds := []acmd.Command{
{
Name: "ping",
Description: "Ping the application",
ExecFunc: func(ctx context.Context, args []string) error {
fmt.Println("Pong!")
return nil
},
},
{
Name: "start",
Description: "set port of the application",
ExecFunc: func(ctx context.Context, args []string) error {
fs := flag.NewFlagSet("port", flag.ContinueOnError)
portFlag := fs.Int("port", 6379, "Port number to use")
if err := fs.Parse(args); err != nil {
return err
}
config.port = *portFlag
fmt.Println("Server is listening.....")
portConfiguration := fmt.Sprintf(":%d", config.port)
listener, err := net.Listen("tcp", portConfiguration)
if err != nil {
fmt.Println("Error while starting listener: ", err)
return err
}
// start with persistance file
persistance, err := NewPersistance("db.sack")
if err != nil {
fmt.Println(err)
return err
}
defer persistance.Close()
// read logs from file and save it in memory
// does not have the Read method without using the ~file.Read~ option
persistance.Read(func(value Value) {
command := strings.ToUpper(value.array[0].bulk)
args := value.array[1:]
handler, ok := Handlers[command]
if !ok {
fmt.Println("Invalid Command: ", command)
return
}
handler(args)
})
// start listening on the specified port
conn, err := listener.Accept()
if err != nil {
fmt.Println("Error while listening: ", err)
return err
}
// starting the server
fmt.Println("Starting the server::: ")
defer conn.Close()
for {
resp := NewResp(conn)
value, err := resp.Read()
if err != nil {
fmt.Println(err)
return err
}
if value.typ != "array" {
fmt.Println("Invalid request, expected array")
continue
}
if len(value.array) == 0 {
fmt.Println("Invalid request, expected array length > 0")
continue
}
command := strings.ToUpper(value.array[0].bulk)
args := value.array[1:]
writer := NewWriter(conn)
handler, ok := Handlers[command]
if !ok {
fmt.Println("Invalid command: ", command)
writer.Write(Value{typ: "string", str: ""})
continue
}
if command == "SET" || command == "HSET" {
persistance.Write(value)
}
result := handler(args)
writer.Write(result)
return nil
}
},
},
}
app := acmd.RunnerOf(cmds, acmd.Config{
AppName: "sack",
AppDescription: "a simple ping command library",
})
if err := app.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}