Skip to content

Commit 8f16347

Browse files
committed
refactor(inventory): centralize http constants
1 parent 210ba5f commit 8f16347

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

services/inventory/server/server.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"encoding/json"
99
"errors"
1010
"fmt"
11-
"io/ioutil"
11+
"io"
1212
"log"
1313
"net/http"
1414
"time"
@@ -20,6 +20,22 @@ import (
2020
"github.com/EvalOps/keep/pkg/telemetry"
2121
)
2222

23+
const (
24+
readHeaderTimeout = 10 * time.Second
25+
readTimeout = 30 * time.Second
26+
writeTimeout = 30 * time.Second
27+
idleTimeout = 60 * time.Second
28+
middlewareTimeout = 30 * time.Second
29+
30+
deviceIDParam = "deviceID"
31+
statusKey = "status"
32+
statusOKValue = "ok"
33+
statusUpdated = "updated"
34+
dbErrorMsg = "db error"
35+
headerContentType = "Content-Type"
36+
contentTypeJSON = "application/json"
37+
)
38+
2339
type Config struct {
2440
Addr string
2541
DSN string
@@ -62,7 +78,7 @@ func NewServer(cfg Config) (*Server, error) {
6278
r.Use(middleware.RequestID)
6379
r.Use(middleware.Logger)
6480
r.Use(middleware.Recoverer)
65-
r.Use(middleware.Timeout(30 * time.Second))
81+
r.Use(middleware.Timeout(middlewareTimeout))
6682

6783
// Health endpoint (no auth required)
6884
r.Get("/health", s.health)
@@ -85,10 +101,10 @@ func NewServer(cfg Config) (*Server, error) {
85101
s.http = &http.Server{
86102
Addr: cfg.Addr,
87103
Handler: r,
88-
ReadHeaderTimeout: 10 * time.Second,
89-
ReadTimeout: 30 * time.Second,
90-
WriteTimeout: 30 * time.Second,
91-
IdleTimeout: 60 * time.Second,
104+
ReadHeaderTimeout: readHeaderTimeout,
105+
ReadTimeout: readTimeout,
106+
WriteTimeout: writeTimeout,
107+
IdleTimeout: idleTimeout,
92108
}
93109
return s, nil
94110
}
@@ -142,7 +158,7 @@ func (s *Server) configureTLS() (*tls.Config, error) {
142158

143159
// Configure client certificate authentication if CA is provided
144160
if s.cfg.ClientCA != "" {
145-
caCert, err := ioutil.ReadFile(s.cfg.ClientCA)
161+
caCert, err := io.ReadFile(s.cfg.ClientCA)
146162
if err != nil {
147163
return nil, fmt.Errorf("failed to read client CA certificate: %w", err)
148164
}
@@ -192,14 +208,14 @@ func (s *Server) requireClientCertMiddleware(next http.Handler) http.Handler {
192208

193209
func (s *Server) health(w http.ResponseWriter, r *http.Request) {
194210
w.WriteHeader(http.StatusOK)
195-
if err := json.NewEncoder(w).Encode(map[string]string{"status": "ok"}); err != nil {
211+
if err := json.NewEncoder(w).Encode(map[string]string{statusKey: statusOKValue}); err != nil {
196212
log.Printf("failed to encode health response: %v", err)
197213
}
198214
}
199215

200216
// updateDevicePosture handles posture update requests
201217
func (s *Server) updateDevicePosture(w http.ResponseWriter, r *http.Request) {
202-
deviceID := chi.URLParam(r, "deviceID")
218+
deviceID := chi.URLParam(r, deviceIDParam)
203219
if deviceID == "" {
204220
http.Error(w, "device id required", http.StatusBadRequest)
205221
return
@@ -218,7 +234,7 @@ func (s *Server) updateDevicePosture(w http.ResponseWriter, r *http.Request) {
218234
http.Error(w, "db error", http.StatusInternalServerError)
219235
return
220236
}
221-
if err := json.NewEncoder(w).Encode(map[string]string{"status": "updated"}); err != nil {
237+
if err := json.NewEncoder(w).Encode(map[string]string{statusKey: statusUpdated}); err != nil {
222238
log.Printf("failed to encode posture update response: %v", err)
223239
}
224240
}
@@ -284,7 +300,7 @@ func (s *Server) registerDevice(w http.ResponseWriter, r *http.Request) {
284300
http.Error(w, "db error", http.StatusInternalServerError)
285301
return
286302
}
287-
if err := json.NewEncoder(w).Encode(map[string]string{"status": "ok"}); err != nil {
303+
if err := json.NewEncoder(w).Encode(map[string]string{statusKey: statusOKValue}); err != nil {
288304
log.Printf("failed to encode registration response: %v", err)
289305
}
290306
}

services/inventory/server/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ package server
22

33
import "testing"
44

5-
func TestEnsureSchema(t *testing.T) {
5+
func TestEnsureSchema(_ *testing.T) {
66
// This test is a placeholder. Schema creation is validated via integration tests.
77
}

0 commit comments

Comments
 (0)