-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
5 changed files
with
93 additions
and
7 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
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,61 @@ | ||
package httpserver | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"sync" | ||
) | ||
|
||
// Server represents an HTTP server for health checks and other utilities | ||
type Server struct { | ||
server *http.Server | ||
port string | ||
mu sync.Mutex | ||
} | ||
|
||
// NewServer creates a new HTTP server with a healthz endpoint | ||
func NewServer(port string) *Server { | ||
mux := http.NewServeMux() | ||
s := &Server{ | ||
port: port, | ||
server: &http.Server{ | ||
Addr: fmt.Sprintf(":%v", port), | ||
Handler: mux, | ||
}, | ||
} | ||
|
||
// Add healthz endpoint | ||
mux.HandleFunc("/healthz", s.healthzHandler) | ||
|
||
return s | ||
} | ||
|
||
// healthzHandler responds with a 200 OK status for health checks | ||
func (s *Server) healthzHandler(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte("OK")) | ||
} | ||
|
||
// Start starts the HTTP server in a separate goroutine | ||
func (s *Server) Start() error { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
fmt.Printf("Starting HTTP server on port %v\n", s.port) | ||
go func() { | ||
if err := s.server.ListenAndServe(); err != http.ErrServerClosed { | ||
fmt.Printf("HTTP server error: %v\n", err) | ||
} | ||
}() | ||
|
||
return nil | ||
} | ||
|
||
// Shutdown gracefully shuts down the HTTP server | ||
func (s *Server) Shutdown(ctx context.Context) error { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
return s.server.Shutdown(ctx) | ||
} |
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