Skip to content

Commit 7ad717c

Browse files
Improve code consistency: extract URL path constants and add documentation
- Add API path constants for consistent URL construction in agent service - Replace hardcoded path segments with named constants (devices, posture, certs, etc.) - Add missing documentation to inventory Server type - Improve maintainability by centralizing URL path definitions Constants added: - pathDevices, pathPosture, pathCerts, pathDevice, pathCA - Consistent usage across updatePosture, registerDevice, obtainCertificate, downloadCA functions Co-authored-by: Amp <amp@ampcode.com> Amp-Thread-ID: https://ampcode.com/threads/T-5be4213f-26eb-400c-bb7b-d4c79b7ee6fe
1 parent 400acd1 commit 7ad717c

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

agent/internal/service/service.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ const (
3131
statusCodeThreshold = 400
3232
permOwnerReadWrite = 0o600
3333
permOwnerReadExecute = 0o700
34+
35+
// API path segments
36+
pathDevices = "devices"
37+
pathPosture = "posture"
38+
pathCerts = "certs"
39+
pathDevice = "device"
40+
pathCA = "ca"
3441
)
3542

3643
// Config holds the service configuration
@@ -271,7 +278,7 @@ func (s *Service) updatePosture() error {
271278
return err
272279
}
273280

274-
endpoint, err := url.JoinPath(strings.TrimSuffix(s.config.InventoryURL, "/"), "v1", "devices", s.config.DeviceID, "posture")
281+
endpoint, err := url.JoinPath(strings.TrimSuffix(s.config.InventoryURL, slash), apiVersion, pathDevices, s.config.DeviceID, pathPosture)
275282
if err != nil {
276283
return fmt.Errorf("build posture endpoint: %w", err)
277284
}
@@ -296,7 +303,7 @@ func (s *Service) registerDevice(publicKey, posture string) error {
296303
return err
297304
}
298305

299-
endpoint, err := url.JoinPath(strings.TrimSuffix(s.config.InventoryURL, "/"), "v1", "devices")
306+
endpoint, err := url.JoinPath(strings.TrimSuffix(s.config.InventoryURL, slash), apiVersion, pathDevices)
300307
if err != nil {
301308
return fmt.Errorf("build registration endpoint: %w", err)
302309
}
@@ -330,7 +337,7 @@ func (s *Service) obtainCertificate() error {
330337
return err
331338
}
332339

333-
endpoint, err := url.JoinPath(strings.TrimSuffix(s.config.AttestURL, slash), apiVersion, "certs", "device")
340+
endpoint, err := url.JoinPath(strings.TrimSuffix(s.config.AttestURL, slash), apiVersion, pathCerts, pathDevice)
334341
if err != nil {
335342
return fmt.Errorf("build certificate endpoint: %w", err)
336343
}
@@ -372,7 +379,7 @@ func (s *Service) obtainCertificate() error {
372379

373380
// downloadCA downloads the root CA certificate
374381
func (s *Service) downloadCA() ([]byte, error) {
375-
endpoint, err := url.JoinPath(strings.TrimSuffix(s.config.AttestURL, slash), apiVersion, "certs", "ca")
382+
endpoint, err := url.JoinPath(strings.TrimSuffix(s.config.AttestURL, slash), apiVersion, pathCerts, pathCA)
376383
if err != nil {
377384
return nil, fmt.Errorf("build CA endpoint: %w", err)
378385
}

services/inventory/server/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type Config struct {
5050
RequireMTLS bool // Whether to require client certificates
5151
}
5252

53+
// Server implements the device inventory HTTP service
5354
type Server struct {
5455
db *sql.DB
5556
http *http.Server

0 commit comments

Comments
 (0)