oto/services/core/internal/httpserver/server.go
toki 8ecb101a17 feat(core): OTO Core 초기 서버를 추가한다
독립 Control Plane 마이그레이션에서 core-service 기반을 검증할 수 있도록 health/readiness 엔드포인트와 로컬 실행 진입점을 먼저 마련한다.
2026-06-05 08:24:45 +09:00

62 lines
1.4 KiB
Go

package httpserver
import (
"context"
"net"
"net/http"
)
// Server wraps the HTTP server for the OTO Core service.
type Server struct {
httpServer *http.Server
}
// NewServer creates a new instance of Server.
func NewServer(addr string) *Server {
mux := http.NewServeMux()
// Register health and readiness endpoints
mux.HandleFunc("/healthz", handleHealthz)
mux.HandleFunc("/readyz", handleReadyz)
return &Server{
httpServer: &http.Server{
Addr: addr,
Handler: mux,
},
}
}
// Start starts the HTTP server.
func (s *Server) Start() error {
return s.httpServer.ListenAndServe()
}
// StartListener starts the HTTP server using a custom net.Listener.
// Useful for testing with dynamic ports.
func (s *Server) StartListener(ln net.Listener) error {
return s.httpServer.Serve(ln)
}
// Shutdown gracefully shuts down the server.
func (s *Server) Shutdown(ctx context.Context) error {
return s.httpServer.Shutdown(ctx)
}
func handleHealthz(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
func handleReadyz(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}