diff options
| author | alex <[email protected]> | 2026-07-03 17:51:51 +0200 |
|---|---|---|
| committer | alex <[email protected]> | 2026-07-03 17:51:51 +0200 |
| commit | eaff064342ab2baa09cc2ea15f11726c666c3407 (patch) | |
| tree | 4163529bbfe5b309d2f29778389d26d6700a4df0 | |
| parent | e9a9d8694eda14a75a9af3781deded16c6f41cc0 (diff) | |
| download | redis-clone-eaff064342ab2baa09cc2ea15f11726c666c3407.tar.xz redis-clone-eaff064342ab2baa09cc2ea15f11726c666c3407.zip | |
basic dictionary server with set and get todo: mutex to protect server
| -rw-r--r-- | .gitignore | 25 | ||||
| -rw-r--r-- | main.go | 79 |
2 files changed, 102 insertions, 2 deletions
@@ -0,0 +1,25 @@ +# Binaries and executables +# (This catches the compiled files generated by 'go build') +*.exe +*.exe~ +*.dll +*.so +*.dylib +bin/ +__debug_bin + +# OS generated files +.DS_Store +Thumbs.db + +# IDE specific files +.idea/ +.vscode/ +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? + +# Optional: If you use a .env file for local configuration/ports +.env @@ -1,7 +1,82 @@ package main -import "fmt" +import ( + "bufio" + "fmt" + "net" + "os" + "strings" +) + +var database map[string]string func main() { - fmt.Println("Hello, World!") + database = make(map[string]string) + + listener, err := net.Listen("tcp", ":6379") + if err != nil { + fmt.Println("error starting server: ", err) + os.Exit(1) + } + defer listener.Close() + fmt.Println("Redis clone listening on port 6379...") + + for { + conn, err := listener.Accept() + if err != nil { + fmt.Println("Error accepting connection:", err) + continue + } + go handleConnection(conn) + } +} + +func handleGet(conn net.Conn, key string) { + value, exists := database[key] + if exists { + // Redis protocol format for a string reply + conn.Write([]byte("+" + value + "\r\n")) + } else { + // Redis protocol format for a Nil/Null response + conn.Write([]byte("$-1\r\n")) + } +} + +func handleSet(conn net.Conn, key, value string) { + database[key] = value + conn.Write([]byte("+OK\r\n")) } + +func handleConnection(conn net.Conn) { + defer conn.Close() + fmt.Println("Client connected:", conn.RemoteAddr()) + + reader := bufio.NewReader(conn) + for { + message, err := reader.ReadString('\n') + if err != nil { + fmt.Println("Client disconnected:", conn.RemoteAddr()) + return + } + + message = strings.TrimSpace(message) + + fmt.Printf("Received: %s\n", message) + + args := strings.Fields(message) + if len(args) == 0 { + continue + } + + command := strings.ToUpper(args[0]) + + switch { + case command == "GET" && len(args) == 2: + handleGet(conn, args[1]) + case command == "SET" && len(args) == 3: + handleSet(conn, args[1], args[2]) + default: + conn.Write([]byte("-ERR unknown command or wrong arguments\r\n")) + } + } +}
\ No newline at end of file |
