-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98b5742
commit fb99200
Showing
7 changed files
with
175 additions
and
150 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
package db | ||
|
||
func getKey(key string) (string, error) { | ||
val, err := rdb.Get(ctx, key).Result() | ||
func (db *DiceDB) getKey(key string) (string, error) { | ||
val, err := db.Client.Get(db.Ctx, key).Result() | ||
return val, err | ||
} | ||
|
||
func setKey(key, value string) error { | ||
err := rdb.Set(ctx, key, value, 0).Err() | ||
func (db *DiceDB) setKey(key, value string) error { | ||
err := db.Client.Set(db.Ctx, key, value, 0).Err() | ||
return err | ||
} | ||
|
||
func deleteKeys(keys []string) error { | ||
err := rdb.Del(ctx, keys...).Err() | ||
func (db *DiceDB) deleteKeys(keys []string) error { | ||
err := db.Client.Del(db.Ctx, keys...).Err() | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"log" | ||
"net/http" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"server/internal/db" | ||
util "server/pkg/util" | ||
) | ||
|
||
type HTTPServer struct { | ||
httpServer *http.Server | ||
DiceClient *db.DiceDB | ||
} | ||
|
||
// HandlerMux wraps ServeMux and forces REST paths to lowercase | ||
// and attaches a rate limiter with the handler | ||
type HandlerMux struct { | ||
mux *http.ServeMux | ||
} | ||
|
||
func (cim *HandlerMux) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
// Convert the path to lowercase before passing to the underlying mux. | ||
r.URL.Path = strings.ToLower(r.URL.Path) | ||
cim.mux.ServeHTTP(w, r) | ||
} | ||
|
||
func NewHTTPServer(addr string, mux *http.ServeMux, client *db.DiceDB) *HTTPServer { | ||
caseInsensitiveMux := &HandlerMux{ | ||
mux: mux, | ||
} | ||
|
||
return &HTTPServer{ | ||
httpServer: &http.Server{ | ||
Addr: addr, | ||
Handler: caseInsensitiveMux, | ||
ReadHeaderTimeout: 5 * time.Second, | ||
}, | ||
DiceClient: client, | ||
} | ||
} | ||
|
||
func (s *HTTPServer) Run(ctx context.Context) error { | ||
var wg sync.WaitGroup | ||
|
||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
log.Printf("Starting server at %s\n", s.httpServer.Addr) | ||
if err := s.httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { | ||
log.Fatalf("HTTP server error: %v", err) | ||
} | ||
}() | ||
|
||
<-ctx.Done() | ||
log.Println("Shutting down server...") | ||
return s.Shutdown() | ||
} | ||
|
||
func (s *HTTPServer) Shutdown() error { | ||
if err := s.DiceClient.Client.Close(); err != nil { | ||
log.Printf("Failed to close dice client: %v", err) | ||
} | ||
|
||
return s.httpServer.Shutdown(context.Background()) | ||
} | ||
|
||
func (s *HTTPServer) HealthCheck(w http.ResponseWriter, request *http.Request) { | ||
util.JSONResponse(w, http.StatusOK, map[string]string{"message": "Server is running"}) | ||
} | ||
|
||
func (s *HTTPServer) CliHandler(w http.ResponseWriter, r *http.Request) { | ||
diceCmds, err := util.ParseHTTPRequest(r) | ||
if err != nil { | ||
http.Error(w, "Error parsing HTTP request", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
resp := s.DiceClient.ExecuteCommand(diceCmds) | ||
util.JSONResponse(w, http.StatusOK, resp) | ||
} | ||
|
||
func (s *HTTPServer) SearchHandler(w http.ResponseWriter, request *http.Request) { | ||
util.JSONResponse(w, http.StatusOK, map[string]string{"message": "Search results"}) | ||
} |
Oops, something went wrong.