Skip to content

Commit

Permalink
Implement Go Server for CLI Command Handling ( resolves #727 ) (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
rishavvajpayee authored Sep 26, 2024
1 parent a4b5008 commit 7fec0f0
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 0 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module server

go 1.22.5
Empty file added go.sum
Empty file.
32 changes: 32 additions & 0 deletions internal/api/api.go
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"})
}
5 changes: 5 additions & 0 deletions internal/db/dicedb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
this will be the DiceDB client
*/

package db
11 changes: 11 additions & 0 deletions internal/middleware/ratelimiter.go
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)
})
}
61 changes: 61 additions & 0 deletions main.go
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)
}
}
1 change: 1 addition & 0 deletions pkg/util/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package helpers

0 comments on commit 7fec0f0

Please sign in to comment.