2017-07-04 06:39:18 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
2017-10-28 03:01:03 +03:00
|
|
|
"github.com/tidwall/doppio"
|
2017-07-04 06:39:18 +03:00
|
|
|
"github.com/tidwall/redcon"
|
|
|
|
)
|
|
|
|
|
2017-10-28 22:23:13 +03:00
|
|
|
type conn struct {
|
|
|
|
is doppio.InputStream
|
|
|
|
addr string
|
|
|
|
}
|
|
|
|
|
2017-07-04 06:39:18 +03:00
|
|
|
func main() {
|
2017-10-28 22:23:13 +03:00
|
|
|
var conns = make(map[int]*conn)
|
2017-07-04 06:39:18 +03:00
|
|
|
var keys = make(map[string]string)
|
2017-10-28 03:01:03 +03:00
|
|
|
var events doppio.Events
|
|
|
|
events.Serving = func(wake func(id int) bool) (action doppio.Action) {
|
|
|
|
log.Printf("serving at tcp port 6380")
|
|
|
|
log.Printf("serving on unix socket")
|
|
|
|
return
|
|
|
|
}
|
2017-10-28 22:23:13 +03:00
|
|
|
events.Opened = func(id int, addr string) (out []byte, opts doppio.Options, action doppio.Action) {
|
|
|
|
conns[id] = &conn{}
|
|
|
|
return
|
|
|
|
}
|
2017-10-28 03:01:03 +03:00
|
|
|
events.Closed = func(id int) (action doppio.Action) {
|
|
|
|
delete(conns, id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
events.Data = func(id int, in []byte) (out []byte, action doppio.Action) {
|
2017-10-28 22:23:13 +03:00
|
|
|
c := conns[id]
|
|
|
|
data := c.is.Begin(in)
|
2017-10-28 03:01:03 +03:00
|
|
|
var n int
|
|
|
|
var complete bool
|
|
|
|
var err error
|
|
|
|
var args [][]byte
|
|
|
|
for action == doppio.None {
|
|
|
|
complete, args, _, data, err = redcon.ReadNextCommand(data, args[:0])
|
|
|
|
if err != nil {
|
|
|
|
action = doppio.Close
|
|
|
|
out = redcon.AppendError(out, err.Error())
|
2017-07-04 06:39:18 +03:00
|
|
|
break
|
|
|
|
}
|
2017-10-28 03:01:03 +03:00
|
|
|
if !complete {
|
|
|
|
break
|
2017-07-04 06:39:18 +03:00
|
|
|
}
|
2017-10-28 03:01:03 +03:00
|
|
|
if len(args) > 0 {
|
|
|
|
n++
|
2017-07-04 06:39:18 +03:00
|
|
|
switch strings.ToUpper(string(args[0])) {
|
|
|
|
default:
|
2017-10-28 03:01:03 +03:00
|
|
|
out = redcon.AppendError(out, "ERR unknown command '"+string(args[0])+"'")
|
2017-07-04 06:39:18 +03:00
|
|
|
case "PING":
|
2017-10-28 22:34:12 +03:00
|
|
|
if len(args) > 2 {
|
|
|
|
out = redcon.AppendError(out, "ERR wrong number of arguments for '"+string(args[0])+"' command")
|
|
|
|
} else if len(args) == 2 {
|
|
|
|
out = redcon.AppendBulk(out, args[1])
|
|
|
|
} else {
|
|
|
|
out = redcon.AppendString(out, "PONG")
|
|
|
|
}
|
|
|
|
case "ECHO":
|
|
|
|
if len(args) != 2 {
|
|
|
|
out = redcon.AppendError(out, "ERR wrong number of arguments for '"+string(args[0])+"' command")
|
|
|
|
} else {
|
|
|
|
out = redcon.AppendBulk(out, args[1])
|
|
|
|
}
|
2017-07-04 06:39:18 +03:00
|
|
|
case "SHUTDOWN":
|
2017-10-28 03:01:03 +03:00
|
|
|
out = redcon.AppendString(out, "OK")
|
|
|
|
action = doppio.Shutdown
|
|
|
|
case "QUIT":
|
|
|
|
out = redcon.AppendString(out, "OK")
|
|
|
|
action = doppio.Close
|
2017-07-04 06:39:18 +03:00
|
|
|
case "GET":
|
|
|
|
if len(args) != 2 {
|
2017-10-28 03:01:03 +03:00
|
|
|
out = redcon.AppendError(out, "ERR wrong number of arguments for '"+string(args[0])+"' command")
|
|
|
|
} else {
|
|
|
|
val, ok := keys[string(args[1])]
|
|
|
|
if !ok {
|
|
|
|
out = redcon.AppendNull(out)
|
|
|
|
} else {
|
|
|
|
out = redcon.AppendBulkString(out, val)
|
|
|
|
}
|
2017-07-04 06:39:18 +03:00
|
|
|
}
|
2017-10-28 03:01:03 +03:00
|
|
|
case "SET":
|
|
|
|
if len(args) != 3 {
|
|
|
|
out = redcon.AppendError(out, "ERR wrong number of arguments for '"+string(args[0])+"' command")
|
2017-07-04 06:39:18 +03:00
|
|
|
} else {
|
2017-10-28 03:01:03 +03:00
|
|
|
keys[string(args[1])] = string(args[2])
|
|
|
|
out = redcon.AppendString(out, "OK")
|
2017-07-04 06:39:18 +03:00
|
|
|
}
|
|
|
|
case "DEL":
|
|
|
|
if len(args) < 2 {
|
2017-10-28 03:01:03 +03:00
|
|
|
out = redcon.AppendError(out, "ERR wrong number of arguments for '"+string(args[0])+"' command")
|
|
|
|
} else {
|
|
|
|
var n int
|
|
|
|
for i := 1; i < len(args); i++ {
|
|
|
|
if _, ok := keys[string(args[1])]; ok {
|
|
|
|
n++
|
|
|
|
delete(keys, string(args[1]))
|
|
|
|
}
|
2017-07-04 06:39:18 +03:00
|
|
|
}
|
2017-10-28 03:01:03 +03:00
|
|
|
out = redcon.AppendInt(out, int64(n))
|
2017-07-04 06:39:18 +03:00
|
|
|
}
|
2017-10-28 22:23:13 +03:00
|
|
|
case "FLUSHDB":
|
|
|
|
keys = make(map[string]string)
|
|
|
|
out = redcon.AppendString(out, "OK")
|
2017-07-04 06:39:18 +03:00
|
|
|
}
|
|
|
|
}
|
2017-10-28 03:01:03 +03:00
|
|
|
}
|
2017-10-28 22:23:13 +03:00
|
|
|
c.is.End(data)
|
2017-10-28 03:01:03 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err := doppio.Serve(events, "tcp://0.0.0.0:6380", "unix://socket")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-07-04 06:39:18 +03:00
|
|
|
}
|