-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Stefano Cappa <[email protected]>
- Loading branch information
Showing
6 changed files
with
359 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package api | ||
|
||
import ( | ||
"api-server/customerrors" | ||
"api-server/db" | ||
"api-server/models" | ||
"api-server/utils" | ||
"encoding/json" | ||
"github.com/gin-contrib/sessions" | ||
"github.com/gin-gonic/gin" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.uber.org/zap" | ||
"golang.org/x/net/context" | ||
"io" | ||
"net/http" | ||
"os" | ||
"time" | ||
) | ||
|
||
type onlineResponse struct { | ||
UUID string `json:"uuid"` | ||
APIToken string `json:"apiToken"` | ||
CreatedAt int64 `json:"createdAt"` | ||
ModifiedAt int64 `json:"modifiedAt"` | ||
} | ||
|
||
// Online struct | ||
type Online struct { | ||
client *mongo.Client | ||
collDevices *mongo.Collection | ||
collProfiles *mongo.Collection | ||
ctx context.Context | ||
logger *zap.SugaredLogger | ||
onlineByUUIDURL string | ||
} | ||
|
||
// NewOnline function | ||
func NewOnline(ctx context.Context, logger *zap.SugaredLogger, client *mongo.Client) *Online { | ||
onlineServerURL := os.Getenv("HTTP_ONLINE_SERVER") + ":" + os.Getenv("HTTP_ONLINE_PORT") | ||
onlineByUUIDURL := onlineServerURL + os.Getenv("HTTP_ONLINE_API") | ||
|
||
return &Online{ | ||
client: client, | ||
collDevices: db.GetCollections(client).Devices, | ||
collProfiles: db.GetCollections(client).Profiles, | ||
ctx: ctx, | ||
logger: logger, | ||
onlineByUUIDURL: onlineByUUIDURL, | ||
} | ||
} | ||
|
||
// GetOnline function | ||
func (handler *Online) GetOnline(c *gin.Context) { | ||
handler.logger.Info("REST - GET - GetOnline called") | ||
|
||
objectID, errID := primitive.ObjectIDFromHex(c.Param("id")) | ||
if errID != nil { | ||
handler.logger.Error("REST - GET - GetOnline - wrong format of the path param 'id'") | ||
c.JSON(http.StatusBadRequest, gin.H{"error": "wrong format of the path param 'id'"}) | ||
return | ||
} | ||
|
||
// retrieve current profile object from database (using session profile as input) | ||
session := sessions.Default(c) | ||
profile, err := utils.GetLoggedProfile(handler.ctx, &session, handler.collProfiles) | ||
if err != nil { | ||
handler.logger.Error("REST - GET - GetOnline - cannot find profile in session") | ||
c.JSON(http.StatusUnauthorized, gin.H{"error": "cannot find profile in session"}) | ||
return | ||
} | ||
|
||
// check if device is in profile (device owned by profile) | ||
if !isDeviceInProfile(&profile, objectID) { | ||
handler.logger.Error("REST - GET - GetOnline - this device is not in your profile") | ||
c.JSON(http.StatusBadRequest, gin.H{"error": "this device is not in your profile"}) | ||
return | ||
} | ||
// get device from db | ||
device, err := handler.getDevice(objectID) | ||
if err != nil { | ||
handler.logger.Error("REST - GET - GetOnline - cannot find device") | ||
c.JSON(http.StatusBadRequest, gin.H{"error": "cannot find device"}) | ||
return | ||
} | ||
|
||
_, result, err := handler.onlineByUUIDService(handler.onlineByUUIDURL + "/" + device.UUID) | ||
if err != nil { | ||
handler.logger.Errorf("REST - GetOnline - cannot get online from remote service = %#v", err) | ||
if re, ok := err.(*customerrors.ErrorWrapper); ok { | ||
handler.logger.Errorf("REST - GetOnline - cannot get online with status = %d, message = %s\n", re.Code, re.Message) | ||
} | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Cannot get online"}) | ||
return | ||
} | ||
handler.logger.Debugf("REST - GetOnline - result = %#v", result) | ||
|
||
onlineResp := onlineResponse{} | ||
err = json.Unmarshal([]byte(result), &onlineResp) | ||
if err != nil { | ||
handler.logger.Errorf("REST - GetOnline - cannot unmarshal JSON response from online remote service = %#v", err) | ||
// TODO manage errors | ||
} | ||
|
||
response := models.Online{} | ||
response.UUID = device.UUID | ||
response.APIToken = onlineResp.APIToken | ||
response.CreatedAt = time.UnixMilli(onlineResp.CreatedAt) | ||
response.ModifiedAt = time.UnixMilli(onlineResp.ModifiedAt) | ||
c.JSON(http.StatusOK, &response) | ||
} | ||
|
||
// TODO this is equals to the method defined in devices_values | ||
func (handler *Online) getDevice(deviceID primitive.ObjectID) (models.Device, error) { | ||
handler.logger.Debug("getDevice - searching device with objectId: ", deviceID) | ||
var device models.Device | ||
err := handler.collDevices.FindOne(handler.ctx, bson.M{ | ||
"_id": deviceID, | ||
}).Decode(&device) | ||
handler.logger.Debug("Device found: ", device) | ||
return device, err | ||
} | ||
|
||
func (handler *Online) onlineByUUIDService(urlOnline string) (int, string, error) { | ||
response, err := http.Get(urlOnline) | ||
if err != nil { | ||
return -1, "", customerrors.Wrap(http.StatusInternalServerError, err, "Cannot call online service via HTTP") | ||
} | ||
defer response.Body.Close() | ||
body, _ := io.ReadAll(response.Body) | ||
return response.StatusCode, string(body), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.