Skip to content

Commit

Permalink
Merge pull request #7 from machinefi/add-log
Browse files Browse the repository at this point in the history
add http api error log
  • Loading branch information
huangzhiran authored Aug 29, 2024
2 parents f4b5145 + 2a60ab7 commit bca725d
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/sequencer/api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,28 @@ func (s *httpServer) verifyToken(c *gin.Context) {
}

if tok == "" {
slog.Error("empty authorization token")
return
}

tok = strings.TrimSpace(strings.Replace(tok, "Bearer", " ", 1))

clientID, err := s.jwk.VerifyToken(tok)
if err != nil {
slog.Error("failed to verify token", "error", err)
c.JSON(http.StatusUnauthorized, apitypes.NewErrRsp(errors.Wrap(err, "invalid credential token")))
return
}
client := s.clients.ClientByIoID(clientID)
if client == nil {
slog.Error("failed to get client by ioid", "client_id", clientID)
c.JSON(http.StatusUnauthorized, apitypes.NewErrRsp(errors.New("invalid credential token")))
return
}

ctx := clients.WithClientID(c.Request.Context(), client)
c.Request = c.Request.WithContext(ctx)
slog.Info("verify token succeed", "client_id", clientID)
}

func (s *httpServer) receiveDeviceData(c *gin.Context) {
Expand Down Expand Up @@ -130,10 +134,12 @@ func (s *httpServer) queryDeviceState(c *gin.Context) {

dev := &models.Device{ID: imei}
if err := event.FetchByPrimary(s.ctx, dev); err != nil {
slog.Error("failed to query device", "error", err)
c.JSON(http.StatusInternalServerError, apitypes.NewErrRsp(err))
return
}
if dev.Status == models.CREATED {
slog.Error("device is not propsaled", "device_id", dev.ID)
c.JSON(http.StatusBadRequest, apitypes.NewErrRsp(errors.Errorf("device %s is not propsaled", dev.ID)))
return
}
Expand Down

0 comments on commit bca725d

Please sign in to comment.