Skip to content

Commit

Permalink
Fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov committed Nov 22, 2024
1 parent 122c23d commit e8072bc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
3 changes: 1 addition & 2 deletions cmd/launcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ func main() {

srv := http.NewHealthCheckServer()
go func() {
if err := srv.Start(); err != nil {
if err := srv.ListenAndServe(); err != nil {
fmt.Printf("Health check server failed to start: %s", err)
os.Exit(1)
}
}()
logs.Logger.Printf("Started healthcheck server on port %d", srv.Port)

runnerType := os.Args[1]
cmd := &commands.LaunchCommand{RunnerType: runnerType}
Expand Down
45 changes: 23 additions & 22 deletions internal/http/healthcheck_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,45 @@ import (
"encoding/json"
"fmt"
"net/http"
"task-runner-launcher/internal/logs"
"time"
)

const (
defaultPort = 5681
healthCheckPath = "/healthz"
readTimeout = 5 * time.Second
writeTimeout = 5 * time.Second
shutdownTimeout = 10 * time.Second
)

type healthCheckResponse struct {
Status string `json:"status"`
}

type Server struct {
Port int
}
func NewHealthCheckServer() *http.Server {
mux := http.NewServeMux()
mux.HandleFunc(healthCheckPath, handleHealthCheck)

func NewHealthCheckServer() *Server {
return &Server{
Port: defaultPort,
return &http.Server{
Addr: fmt.Sprintf(":%d", defaultPort),
Handler: mux,
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
}
}

func (s *Server) Start() error {
http.HandleFunc(healthCheckPath, s.handleHealthCheck)

addr := fmt.Sprintf(":%d", s.Port)

return http.ListenAndServe(addr, nil)
}

func (s *Server) handleHealthCheck(w http.ResponseWriter, r *http.Request) {
func handleHealthCheck(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

res := healthCheckResponse{Status: "ok"}
json.NewEncoder(w).Encode(res)
res := struct {
Status string `json:"status"`
}{Status: "ok"}

if err := json.NewEncoder(w).Encode(res); err != nil {
logs.Logger.Printf("Failed to encode health check response: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}

0 comments on commit e8072bc

Please sign in to comment.