@@ -32,6 +32,9 @@ const (
3232 statusOKValue = "ok"
3333 statusUpdated = "updated"
3434 dbErrorMsg = "db error"
35+ deviceIDRequired = "device id required"
36+ badRequestMsg = "bad request"
37+ emptyString = ""
3538 headerContentType = "Content-Type"
3639 contentTypeJSON = "application/json"
3740)
@@ -92,7 +95,7 @@ func NewServer(cfg Config) (*Server, error) {
9295 r .Route ("/devices" , func (r chi.Router ) {
9396 r .Get ("/" , s .listDevices )
9497 r .Post ("/" , s .registerDevice )
95- r .Get ("/{deviceID}" , s .getDevice )
98+ r .Get ("/{deviceID}" , s .getDeviceHandler )
9699 r .Put ("/{deviceID}" , s .updateDevice )
97100 r .Post ("/{deviceID}/posture" , s .updateDevicePosture )
98101 })
@@ -206,7 +209,7 @@ func (s *Server) requireClientCertMiddleware(next http.Handler) http.Handler {
206209 })
207210}
208211
209- func (s * Server ) health (w http.ResponseWriter , r * http.Request ) {
212+ func (s * Server ) health (w http.ResponseWriter , _ * http.Request ) {
210213 w .WriteHeader (http .StatusOK )
211214 if err := json .NewEncoder (w ).Encode (map [string ]string {statusKey : statusOKValue }); err != nil {
212215 log .Printf ("failed to encode health response: %v" , err )
@@ -216,22 +219,22 @@ func (s *Server) health(w http.ResponseWriter, r *http.Request) {
216219// updateDevicePosture handles posture update requests
217220func (s * Server ) updateDevicePosture (w http.ResponseWriter , r * http.Request ) {
218221 deviceID := chi .URLParam (r , deviceIDParam )
219- if deviceID == "" {
220- http .Error (w , "device id required" , http .StatusBadRequest )
222+ if deviceID == emptyString {
223+ http .Error (w , deviceIDRequired , http .StatusBadRequest )
221224 return
222225 }
223226
224227 var payload struct {
225228 Posture string `json:"posture"`
226229 }
227230 if err := json .NewDecoder (r .Body ).Decode (& payload ); err != nil {
228- http .Error (w , "bad request" , http .StatusBadRequest )
231+ http .Error (w , badRequestMsg , http .StatusBadRequest )
229232 return
230233 }
231234
232235 _ , err := s .db .Exec (`UPDATE devices SET posture=$1, last_updated=now() WHERE id=$2` , payload .Posture , deviceID )
233236 if err != nil {
234- http .Error (w , "db error" , http .StatusInternalServerError )
237+ http .Error (w , dbErrorMsg , http .StatusInternalServerError )
235238 return
236239 }
237240 if err := json .NewEncoder (w ).Encode (map [string ]string {statusKey : statusUpdated }); err != nil {
@@ -261,10 +264,10 @@ func ensureSchema(db *sql.DB) error {
261264 return err
262265}
263266
264- func (s * Server ) listDevices (w http.ResponseWriter , r * http.Request ) {
267+ func (s * Server ) listDevices (w http.ResponseWriter , _ * http.Request ) {
265268 rows , err := s .db .Query (`SELECT id, public_key, posture, registered_at, last_updated FROM devices` )
266269 if err != nil {
267- http .Error (w , "db error" , http .StatusInternalServerError )
270+ http .Error (w , dbErrorMsg , http .StatusInternalServerError )
268271 return
269272 }
270273 defer rows .Close ()
@@ -273,7 +276,7 @@ func (s *Server) listDevices(w http.ResponseWriter, r *http.Request) {
273276 for rows .Next () {
274277 var d Device
275278 if err := rows .Scan (& d .ID , & d .PublicKey , & d .Posture , & d .Registered , & d .LastUpdated ); err != nil {
276- http .Error (w , "db error" , http .StatusInternalServerError )
279+ http .Error (w , dbErrorMsg , http .StatusInternalServerError )
277280 return
278281 }
279282 list = append (list , d )
@@ -287,33 +290,33 @@ func (s *Server) listDevices(w http.ResponseWriter, r *http.Request) {
287290func (s * Server ) registerDevice (w http.ResponseWriter , r * http.Request ) {
288291 var d Device
289292 if err := json .NewDecoder (r .Body ).Decode (& d ); err != nil {
290- http .Error (w , "bad request" , http .StatusBadRequest )
293+ http .Error (w , badRequestMsg , http .StatusBadRequest )
291294 return
292295 }
293- if d .ID == "" {
296+ if d .ID == emptyString {
294297 http .Error (w , "id required" , http .StatusBadRequest )
295298 return
296299 }
297300
298301 _ , err := s .db .Exec (`INSERT INTO devices (id, public_key, posture) VALUES ($1, $2, $3) ON CONFLICT (id) DO UPDATE SET public_key = EXCLUDED.public_key, posture = EXCLUDED.posture, last_updated = now()` , d .ID , d .PublicKey , d .Posture )
299302 if err != nil {
300- http .Error (w , "db error" , http .StatusInternalServerError )
303+ http .Error (w , dbErrorMsg , http .StatusInternalServerError )
301304 return
302305 }
303306 if err := json .NewEncoder (w ).Encode (map [string ]string {statusKey : statusOKValue }); err != nil {
304307 log .Printf ("failed to encode registration response: %v" , err )
305308 }
306309}
307310
308- func (s * Server ) getDevice (w http.ResponseWriter , r * http.Request ) {
311+ func (s * Server ) getDeviceHandler (w http.ResponseWriter , r * http.Request ) {
309312 id := chi .URLParam (r , deviceIDParam )
310313 var d Device
311314 if err := s .db .QueryRow (`SELECT id, public_key, posture, registered_at, last_updated FROM devices WHERE id=$1` , id ).Scan (& d .ID , & d .PublicKey , & d .Posture , & d .Registered , & d .LastUpdated ); err != nil {
312315 if errors .Is (err , sql .ErrNoRows ) {
313316 http .NotFound (w , r )
314317 return
315318 }
316- http .Error (w , "db error" , http .StatusInternalServerError )
319+ http .Error (w , dbErrorMsg , http .StatusInternalServerError )
317320 return
318321 }
319322 if err := json .NewEncoder (w ).Encode (d ); err != nil {
@@ -327,13 +330,13 @@ func (s *Server) updateDevice(w http.ResponseWriter, r *http.Request) {
327330 Posture string `json:"posture"`
328331 }
329332 if err := json .NewDecoder (r .Body ).Decode (& payload ); err != nil {
330- http .Error (w , "bad request" , http .StatusBadRequest )
333+ http .Error (w , badRequestMsg , http .StatusBadRequest )
331334 return
332335 }
333336
334337 _ , err := s .db .Exec (`UPDATE devices SET posture=$1, last_updated=now() WHERE id=$2` , payload .Posture , id )
335338 if err != nil {
336- http .Error (w , "db error" , http .StatusInternalServerError )
339+ http .Error (w , dbErrorMsg , http .StatusInternalServerError )
337340 return
338341 }
339342 if err := json .NewEncoder (w ).Encode (map [string ]string {statusKey : statusUpdated }); err != nil {
0 commit comments