mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-13 19:55:14 +08:00
Return PONG response from server
This commit is contained in:
@@ -12,15 +12,13 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"sync"
|
||||
|
||||
"github.com/kelvinmwinuka/memstore/serialization"
|
||||
"github.com/tidwall/resp"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Listener interface {
|
||||
Accept() (net.Conn, error)
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
TLS bool `json:"tls" yaml:"tls"`
|
||||
Key string `json:"key" yaml:"key"`
|
||||
@@ -29,18 +27,24 @@ type Config struct {
|
||||
Port uint16 `json:"port" yaml:"port"`
|
||||
}
|
||||
|
||||
type Data struct {
|
||||
mu sync.Mutex
|
||||
data map[string]interface{}
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
config Config
|
||||
data Data
|
||||
}
|
||||
|
||||
func (server *Server) hanndleConnection(conn net.Conn) {
|
||||
rw := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
|
||||
// sw := bufio.NewWriter(os.Stdout)
|
||||
connRW := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
|
||||
respWriter := resp.NewWriter(connRW)
|
||||
|
||||
var line [][]byte
|
||||
|
||||
for {
|
||||
b, _, err := rw.ReadLine()
|
||||
b, _, err := connRW.ReadLine()
|
||||
|
||||
if err != nil && err == io.EOF {
|
||||
fmt.Println(err)
|
||||
@@ -54,7 +58,24 @@ func (server *Server) hanndleConnection(conn net.Conn) {
|
||||
// sw.Write(bytes.Join(line, []byte("\\r\\n")))
|
||||
// sw.Flush()
|
||||
|
||||
serialization.Decode(string(bytes.Join(line, []byte("\r\n"))))
|
||||
if cmd, err := serialization.Decode(string(bytes.Join(line, []byte("\r\n")))); err != nil {
|
||||
fmt.Println(err)
|
||||
// Return error to client
|
||||
continue
|
||||
} else {
|
||||
// Return encoded message to client
|
||||
|
||||
if len(cmd) == 1 && cmd[0] == "PING" {
|
||||
serialization.EncodeSimpleString(respWriter, "PONG")
|
||||
connRW.Flush()
|
||||
}
|
||||
|
||||
if len(cmd) == 2 && cmd[0] == "PING" {
|
||||
fmt.Println(cmd)
|
||||
serialization.EncodeSimpleString(respWriter, cmd[1])
|
||||
connRW.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
line = [][]byte{}
|
||||
}
|
||||
|
Reference in New Issue
Block a user