-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Go Server for CLI Command Handling ( resolves #727 ) (#1)
- Loading branch information
1 parent
a4b5008
commit 7fec0f0
Showing
7 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
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,3 @@ | ||
module server | ||
|
||
go 1.22.5 |
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,32 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
func JSONResponse(w http.ResponseWriter, r *http.Request, status int, data interface{}) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(status) | ||
if err := json.NewEncoder(w).Encode(data); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
} | ||
|
||
func RegisterRoutes(mux *http.ServeMux) { | ||
mux.HandleFunc("/health", HealthCheck) | ||
mux.HandleFunc("/cli", cliHandler) | ||
mux.HandleFunc("/search", searchHandler) | ||
} | ||
|
||
func HealthCheck(w http.ResponseWriter, r *http.Request) { | ||
JSONResponse(w, r, http.StatusOK, map[string]string{"message": "Server is running"}) | ||
} | ||
|
||
func cliHandler(w http.ResponseWriter, r *http.Request) { | ||
JSONResponse(w, r, http.StatusOK, map[string]string{"message": "cli handler"}) | ||
} | ||
|
||
func searchHandler(w http.ResponseWriter, r *http.Request) { | ||
JSONResponse(w, r, http.StatusOK, map[string]string{"message": "Search results"}) | ||
} |
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,5 @@ | ||
/* | ||
this will be the DiceDB client | ||
*/ | ||
|
||
package db |
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,11 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
func RateLimiter(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
next.ServeHTTP(w, r) | ||
}) | ||
} |
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,61 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"net/http" | ||
"sync" | ||
"time" | ||
|
||
"server/internal/api" | ||
"server/internal/middleware" | ||
) | ||
|
||
type HTTPServer struct { | ||
httpServer *http.Server | ||
} | ||
|
||
func NewHTTPServer(addr string, mux *http.ServeMux) *HTTPServer { | ||
return &HTTPServer{ | ||
httpServer: &http.Server{ | ||
Addr: addr, | ||
Handler: mux, | ||
ReadHeaderTimeout: 5 * time.Second, | ||
}, | ||
} | ||
} | ||
|
||
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 && err != http.ErrServerClosed { | ||
log.Fatalf("HTTP server error: %v", err) | ||
} | ||
}() | ||
|
||
<-ctx.Done() | ||
log.Println("Shutting down server...") | ||
return s.httpServer.Shutdown(context.Background()) | ||
} | ||
|
||
func main() { | ||
mux := http.NewServeMux() | ||
|
||
mux.Handle("/", middleware.RateLimiter(http.HandlerFunc(api.HealthCheck))) | ||
api.RegisterRoutes(mux) | ||
|
||
httpServer := NewHTTPServer(":8080", mux) | ||
|
||
// context for graceful shutdown | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
// run the Http Server | ||
if err := httpServer.Run(ctx); err != nil { | ||
log.Fatalf("Server failed: %v", 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package helpers |