Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite to use config files #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
node_modules
npm-debug.log
dist
.env
config/production.json
config/config.development.json
18 changes: 14 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
*.DS_Store
*.env*
dist
node_modules
node_modules/
dist/
.DS_Store

# Environment-specific config files
config/*
!config/config.example.json

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
11 changes: 9 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
FROM node:21-alpine

# Set the working directory in the container
WORKDIR /usr/src/app
WORKDIR /app

# Copy package.json and package-lock.json (if available)
COPY package*.json ./
Expand All @@ -13,16 +13,23 @@ RUN npm install --verbose --no-audit --ignore-scripts
# Copy the rest of your application's source code
COPY . .

# Create config directory and copy example config
RUN mkdir -p /app/config && \
cp /app/config/config.example.json /app/config/config.production.json

# Compile app
RUN npm run build

# Expose the port your shipper runs on
EXPOSE 3000

# Set production environment
ENV NODE_ENV=production

# Command to start shipper
CMD ["npm", "start"]

# ######## NICE #########
# After this, run the container with:
# docker run -p 3000:3000 --env-file .env shipper
# docker run -p 3000:3000 -v $(pwd)/config:/app/config shipper
# #################
82 changes: 48 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,46 @@ Postage calculation API for Dogebox pre-orders

Requires NodeJS 18 or upwards OR docker.

Create a .env file with the following contents:
Copy the example configuration file and modify it for your environment:

```bash
cp config/config.example.json config/config.development.json
```
AUSPOST_API_KEY=your-api-key-here
ALLOWED_ORIGINS=http://localhost:3000,https://yourdomain.com
HANDLING_COST_IN_DOGE=1
DOGE_TO_AUD=1

Edit `config/config.development.json` with your settings:

```json
{
"port": 3000,
"auspostApiKey": "your-api-key-here",
"dogeToAudRate": 0.15,
"handlingCostInDoge": 30,
"allowedOrigins": "*"
}
```
Note: the HANDLING_COST_IN_DOGE and DOGE_TO_AUD value should be set to the correct Doge amount value

The application supports different environments through configuration files:

- `config/config.development.json` - Used when NODE_ENV=development or not set
- `config/config.production.json` - Used when NODE_ENV=production
- `config/config.test.json` - Used when NODE_ENV=test

### Running in Prod (via Docker container)

```
```bash
# Build the container
docker build -t shipper .

# Run the container
docker run -p 3000:3000 --env-file .env shipper
# Run the container (mount config directory)
docker run -p 3000:3000 -v $(pwd)/config:/app/config shipper

# Interact with container
http <address>:3000/shipping/calc sku=b0rk country=PT
```

### Run in Prod (bare metal)

```
```bash
# Install NodeJS 18 or upwards
nvm install 18

Expand All @@ -46,50 +61,49 @@ npm start

### Run dev server (not suitable for prod)

```
```bash
# Requires NodeJS 18 or greater
npm install
npm run dev
```

---


### Interacting with API

#### On success:

*Request*
_Request_

```
```bash
http localhost:3000/shipping/calc sku="b0rk" country="PT" postcode="90210"
```

*Response*
_Response_

```
```json
{
"deliveryAdviceURL": "https://auspost.com.au/sending/delivery-speeds-and-coverage/international-delivery-times",
"options": [
{
"id": "INT_PARCEL_COR_OWN_PACKAGING",
"label": "Courier",
"price": "134.15"
"id": "INT_PARCEL_COR_OWN_PACKAGING",
"label": "Courier",
"price": "134.15"
},
{
"id": "INT_PARCEL_EXP_OWN_PACKAGING",
"label": "Express",
"price": "69.15"
"id": "INT_PARCEL_EXP_OWN_PACKAGING",
"label": "Express",
"price": "69.15"
},
{
"id": "INT_PARCEL_STD_OWN_PACKAGING",
"label": "Standard",
"price": "54.15"
"id": "INT_PARCEL_STD_OWN_PACKAGING",
"label": "Standard",
"price": "54.15"
},
{
"id": "INT_PARCEL_AIR_OWN_PACKAGING",
"label": "Economy Air",
"price": "51.65"
"id": "INT_PARCEL_AIR_OWN_PACKAGING",
"label": "Economy Air",
"price": "51.65"
}
],
"success": true
Expand All @@ -98,20 +112,20 @@ http localhost:3000/shipping/calc sku="b0rk" country="PT" postcode="90210"

#### On error:

*Example bad request:*
_Example bad request:_

```
```bash
http localhost:3000/shipping/calc sku="bL0rk" country="CHICKEN" postcode="90210"
```

*Response:*
_Response:_

```
```json
{
"error": "BAD_INPUT",
"reasons": [
"Invalid SKU. Received \"bL0rk\", expected one of standard, founders, b0rk",
"Malformed country code. Received \"CHICKEN\", expected 2 letter A-Z"
"Invalid SKU. Received \"bL0rk\", expected one of standard, founders, b0rk",
"Malformed country code. Received \"CHICKEN\", expected 2 letter A-Z"
],
"success": false
}
Expand Down
47 changes: 47 additions & 0 deletions config/config.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"port": 3000,
"auspost": {
"apiKey": "your-api-key-here",
"baseURL": "https://api.auspost.com.au"
},
"dogeToAudRate": 0.15,
"handlingCost": 30,
"editions": {
"standard": {
"price": 3699,
"name": "Standard Edition",
"dimensions": { "length": 15, "width": 15, "height": 15 },
"weight": 0.7
},
"founders": {
"price": 6999,
"name": "Founders Edition",
"dimensions": { "length": 20, "width": 20, "height": 20 },
"weight": 1.0
},
"b0rk": {
"price": 9001,
"name": "Full B0rk Edition",
"dimensions": { "length": 25, "width": 25, "height": 25 },
"weight": 1.2
}
},
"fixedDomesticServices": {
"standard": [
{ "name": "Regular", "price": 14.95 },
{ "name": "Express", "price": 18.95 }
],
"founders": [
{ "name": "Regular", "price": 16.95 },
{ "name": "Express", "price": 20.95 }
],
"b0rk": [
{ "name": "Regular", "price": 19.95 },
{ "name": "Express", "price": 24.95 }
]
},
"deliveryAdvice": {
"domestic": "https://auspost.com.au/business/shipping/delivery-speeds-and-coverage",
"international": "https://auspost.com.au/sending/delivery-speeds-and-coverage/international-delivery-times"
}
}
23 changes: 22 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2"
"express": "^4.19.2",
"zod": "^3.24.2",
"zod-validation-error": "^3.4.0"
},
"devDependencies": {
"@types/cors": "^2.8.17",
Expand Down
Loading