# User - Roboshop Project

The "User" microservice is responsible for handling user logins and registrations in the RobotShop e-commerce portal. It was developed using Node.js and requires a version above 18.

The following code can create the Ec2 instance user for us

```plaintext
{
  "MaxCount": 1,
  "MinCount": 1,
  "ImageId": "ami-0089b8e98cd95257d",
  "InstanceType": "t3.micro",
  "EbsOptimized": true,
  "NetworkInterfaces": [
    {
      "DeviceIndex": 0,
      "AssociatePublicIpAddress": true,
      "SubnetId": "subnet-086a045ade7eae99f",
      "Groups": [
        "sg-0c2e3fdd288a74d3a"
      ]
    }
  ],
  "TagSpecifications": [
    {
      "ResourceType": "instance",
      "Tags": [
        {
          "Key": "Name",
          "Value": "user"
        }
      ]
    },
    {
      "ResourceType": "spot-instances-request",
      "Tags": [
        {
          "Key": "Name",
          "Value": "user"
        }
      ]
    }
  ],
  "InstanceMarketOptions": {
    "MarketType": "spot",
    "SpotOptions": {
      "InstanceInterruptionBehavior": "stop",
      "SpotInstanceType": "persistent"
    }
  },
  "PrivateDnsNameOptions": {
    "HostnameType": "ip-name",
    "EnableResourceNameDnsARecord": true,
    "EnableResourceNameDnsAAAARecord": false
  }
}
```

To set up the Node.js repositories, the vendor provides a script that can be run using the command:

```plaintext
curl -sL https://rpm.nodesource.com/setup_lts.x | bash
```

After that, Node.js can be installed using

```plaintext
yum install nodejs -y 
```

Since the application has no RPM software, every step needs to be configured manually.

To follow best practices, the application is set up to run as a non-root user. The application user `useradd roboshop` is created and will only be used to run the application, not to log in to the server. The application is kept in a standard location and an app directory is set up using the command

```plaintext
mkdir /app
```

The application code is then downloaded using

```plaintext
curl -L -o /tmp/user.zip https://roboshop-artifacts.s3.amazonaws.com/user.zip 
unzip /tmp/user.zip
```

Like most applications, this one has some common dependencies, which can be downloaded using `npm install` in the app directory. A new service is then set up in systemd using the following configuration file located at

vim /etc/systemd/system/user.service

Add the following;

```plaintext
[Unit] 
Description = User Service 
[Service] 
User=roboshop Environment=MONGO=true Environment=REDIS_HOST=Enter-Redis-IP
Environment=MONGO_URL="mongodb://MONGODB-IP:27017/users" 
ExecStart=/bin/node /app/server.js SyslogIdentifier=user

[Install] 
WantedBy=multi-user.target
```

The service can be loaded using

```plaintext
systemctl daemon-reload
```

The schema must be loaded into the database to fully enable the application's functionality. This can be done by installing the MongoDB client using the following repo file located at

```plaintext
vim /etc/yum.repos.d/mongo.repo 
```

```plaintext
[mongodb-org-4.2] 
name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/ 
gpgcheck=0 
enabled=1
```

```plaintext
yum install mongodb-org-shell -y
```

Then, the schema can be loaded using

```plaintext
mongo --host MONGODB-SERVER-IPADDRESS </app/schema/user.js
```
