Backend Engineer Practical Assignment
- there are two services,order service and payment service
- the order service source code is
setel/order
, the payment service code issetel/payment
- when create an order, order service will send a http request to payment service, it generate
confirm_url
for payment service to confirm this order andcancel_url
for payment service to decline this order. - after amount of time (we can config about as order enviroment variable
deliver_time
), the backgroud job of order service will update status of order todelivered
if the current status isconfirmed
. the code for this job is located insetel/order/src/jobs/deliverOrder.ts
- payment service can confirm,decline an order randomly, the current way is inject a function return boolean to PaymentService class, check code as
setel/payment/src/service/payment.ts
line 6, and implement function atsetel/payment/src/server.ts
line 29.
- Deploy two service behind a reverse proxy server like nginx, aws load balancer
- Forward traffic to each service depend on request localtion path
/api/orders
proxy to Order service./api/payments
proxy to Payment Service
- Configure environtment for each service, see
Environment
below.
-
Order service
-
go to order service
cd order
-
build docker image
docker build -t setel-order .
-
to test order service (make sure env was passed to the process via .env file or another way)
yarn install yarn test
-
to run service without using docker
yarn install yarn build yarn start
-
-
Payment service
-
go to payment service folder
cd payment
-
build docker image
docker build -t setel-payment .
-
to run service without using docker
yarn install yarn build yarn start
-
-
Order service
server_port=1368 postgres_url=postgres://setel:setel@localhost:5432/setel order_host=https://setel.dongnguyen.dev payment_host=https://setel.dongnguyen.dev deliver_time=10
order_host
andpayment_host
is the host name of reverse proxy server, it can be the same if two service share same proxy server.deliver_time
is amout time that theconfirmed
order will be move todelivered
state. The unit is seconds. -
Payment service
server_port=1369
-
Create order
- send POST request to
/api/orders
, requirename
(order name),price
(order price)
curl --location --request POST 'https://setel.dongnguyen.dev/api/orders' \ --header 'Content-Type: application/json' \ --data-raw '{ "name":"hello", "price":10 }'
- response
{ "status": true, "result": { "created_at": "2022-05-07T09:40:01.957Z", "updated_at": "2022-05-07T09:40:01.957Z", "id": 79, "name": "hello", "price": 10, "status": "created" } }
- send POST request to
-
Get order status
-
send GET request to.
/api/orders/:id
curl --location --request GET 'https://setel.dongnguyen.dev/api/orders/80'
-
response
{ "status": true, "result": { "id": 80, "name": "hello", "price": 10, "status": "created", "created_at": "2022-05-07T09:41:51.866Z", "updated_at": "2022-05-07T09:41:51.866Z" } }
-
-
Cancel an order
-
send POST request to
/api/orders/cancel/:id
curl --location --request POST 'https://setel.dongnguyen.dev/api/orders/cancel/84'
-
response
{ "status": true, "result": { "id": 84, "name": "hello", "price": 10, "status": "canceled", "created_at": "2022-05-07T09:44:27.477Z", "updated_at": "2022-05-07T09:44:31.365Z" } }
-