# Getting started

## Pre-requirements

### Go runtime
Go version 1.17

### Database
Postgres

### Queue
RabbitMQ

### Cache
Not using yet


## How to start

### I. Install Golang `v1.17`

### II. Install Postgres (Can install via [Docker](https://github.com/UpmeshLTD/urus-aio/blob/3f3413e5e2d4c0a27cb6b789c2525376ff079120/docs/docker.md))

### III. Config environments

Each service will have a `.env.example` that defined all env we need.

First, copy and rename `.env.example` to `.env` then fill the correct values to `.env`

#### Authentication service
Currently, authentication is work by request to the authenticate service using gRPC.

The environments of authentication service as bellow:
```text
AUTH_BUYER_HOST=api.devten.upmesh.io
AUTH_BUYER_PORT=80
AUTH_USER_HOST=api.devten.upmesh.io
AUTH_USER_PORT=80
```


#### Shipment service
By default, the mock queue will be used. Can replace by set env in shipment service.
```text
BROKER_URI=amqp://rabbitmq:changemenow@localhost:5671
BROKER_EXCHANGE_NAME=develop-ten
```

The runtime log will only print to the console log. In addition, we can configure it to send to the CloudWatch Logs. Please contact the DevOps team to get the configs.
```text
AWS_REGION=ap-southeast-1

AWS_CLOUDWATCH_LOG_BATCH_FREQUENCY=10 # In seconds
AWS_CLOUDWATCH_LOG_GROUP_NAME=<input the group name>
AWS_CLOUDWATCH_LOG_STREAM_NAME_PREFIX=<input the stream name>
```


#### PDF service
To run on local, need to install the 3rd party library.

https://wkhtmltopdf.org/downloads.html

Extract and save the library into a folder, then copy the `executable-file-path` and set it into the `pdf/.env` as bellow. Also set the temporary dir to save the temporary results.
```text
# Unix
PDF_TEMP_DIR=/tmp
PDF_BIN_PATH=/usr/bin/wkhtmltopdf

# Windows
PDF_TEMP_DIR=C:/tmp
PDF_BIN_PATH=C:/wkhtmltopdf/bin/wkhtmltopdf.exe
```

S3 environments, where the PDF files stored. Please contact the DevOps team to get the configs.
```text
AWS_REGION=ap-southeast-1

PDF_AWS_S3_BUCKET=<input the bucket name>
PDF_AWS_S3_FOLDER=<input the folder name>
PDF_AWS_S3_PRESIGNED_URL_TTL_IN_MINUTE=10
```

Set the AWS secret key credentials. Ignore if you already have configured local aws credentials `~/.aws/credentials`
```text
PDF_AWS_KEY=<input your key>
PDF_AWS_SECRET=<input your secret>
```

### IV. Init database
   1. Go to infrastructure `cd infrastructure`
   2. Install packages `go mod download`
   3. Install dev tools `make dev-setup`
   4. Set the database env to your environment variables
      ```text
      DB_HOST=localhost
      DB_PORT=5432
      DB_USERNAME=postgres
      DB_PASSWORD=postgres
      DB_DATABASE=upmeshuat_urus
      ```
   5. Run `make migrate-up`
      ```
      $ make migrate-up
      sql-migrate up -config="./db/migrations/scripts/dbconfig.yml" -env="development" -limit=1000
      Applied 84 migrations
      ```
   6. Run `make seed-data`
      ```
      $ make seed-data
      ...
      2022/11/10 16:24:40 Completed seeding
      ```

### V. Start a service
   1. Go to a service `cd service-name`
   2. Install packages `go mod download`
   3. Copy & rename `.env.example` to `.env` then fill the correct values
   4. Run `make run`

Output success example:

MDM service
```text
$ cd mdm
$ make run
go run cmd/app/main.go run
2022/11/10 15:04:06  🚀 Listening HTTP on port 9002
2022/11/10 15:04:06  🚀 Listening GRPC on port 50002
```

Order service
```text
$ cd paid-order
$ make run
go run cmd/app/main.go run
2022/11/10 15:05:58  🚀 Listening HTTP on port 9003
2022/11/10 15:05:58  🚀 Listening GRPC on port 50003
```

Shipment service
```text
$ cd shipment
$ make run
go run cmd/app/main.go run
2022/11/10 15:07:01  🚀 Listening HTTP on port 9000
2022/11/10 15:07:01  🚀 Listening GRPC on port 50000
```

PDF service
```text
$ cd pdf
$ make run
go run cmd/app/main.go run
2022/11/10 15:07:35  🚀 Listening GRPC on port 50000
```

Cronjob service
```text
$ cd cronjob
$ make run
go run cmd/app/main.go run
2022/11/10 15:08:36 logger level: debug
2022/11/10 15:08:36 successfully initialize ZerologLogger
2022/11/10 15:08:36  🚀 Cronjob service started
2022/11/10 15:08:36 Cronjob `SyncCourierShipmentStatusKerryExpressTH` has been schedule for @every 10
```

### VI. Start with Docker (All in one)
   1. Go to each of services, copy & rename `.env.example` to `.env` then fill the correct values
   2. Go to root project
   3. Run `make up` or `docker-compose up` to start Postgres and services
   4. Go to infrastructure `cd infrastructure` and following the section [IV. Init Database](#iv-init-database) to init database
   
Output success example:
```text
[+] Building 5.1s (12/12) FINISHED
=> [internal] load build definition from Dockerfile                                                                            0.0s
=> => transferring dockerfile: 366B                                                                                            0.0s
=> [internal] load .dockerignore                                                                                               0.0s
=> => transferring context: 2B                                                                                                 0.0s
=> [internal] load metadata for docker.io/library/golang:1.17-alpine                                                           4.1s
=> [1/7] FROM docker.io/library/golang:1.17-alpine@sha256:99ddec1bbfd6d6bca3f9804c02363daee8c8524dae50df7942e8e60788fd17c9     0.0s
=> [internal] load build context                                                                                               0.0s
=> => transferring context: 21.96kB                                                                                            0.0s
...
...
upmesh_shipment_container        | 2022/11/10 17:54:30  🚀 Listening HTTP on port 8000
upmesh_shipment_container        | 2022/11/10 17:54:30  🚀 Listening GRPC on port 9000
```

## Create new service
Clone from package `service-template`