# Program God API

- Runtime: Node.js (Express)
- Port: 3100

## Run

```bash
npm install
npm start
```

## Environment

Copy `.env.example` to `.env` and fill values.

## PM2

```bash
pm2 start ecosystem.config.js --env production
pm2 save
```

## GitHub Actions

`/.github/workflows/deploy.yml` is configured to deploy via SSH and restart PM2.
Ensure repo secrets are set:
- `SERVER_HOST`
- `SERVER_USER`
- `SERVER_SSH_KEY`
- `SERVER_PORT`

## Endpoints

- `GET /health`
- `GET /api/modules`
- `GET /api/modules/platforms?hwid=<HWID>`
- `GET /api/modules/ads`
- `POST /api/modules/:id/ad-click`
- `POST /api/modules/:id/download`

## Database Init (Python)

```bash
pip install -r requirements.txt
python scripts/init_db.py
python scripts/seed_db.py
```

Uses `.env` for DB connection.

## Adpick Sync Cron

Adpick offers are synchronized once immediately on server startup and then every 1 minute using `node-cron`.

Implementation:
- `src/services/adpickSync.js`
- Cron expression: `* * * * *`
- API source: `https://adpick.co.kr/apis/offers.php?affid=1ae1d4&os=&adtype=&category=&order=rand`

The sync stores normalized offers in `adpick_offers` and the host banner area reads from `GET /api/modules/ads`.
Current Adpick response does not expose a price field, so sort priority is:
1. `reward_amount` when such a field is present
2. video material
3. banner/image availability
4. remaining count

## Daily Free Starts / Ad Unlock

Every module is allowed 3 free starts per day for each `hwid`.
After that, one ad click unlocks one additional start for that specific module.

Implementation:
- `src/services/moduleAccess.js`
- Table: `module_daily_usage`
- Enforcement point: `POST /api/modules/:id/download`
- Unlock point: `POST /api/modules/:id/ad-click`

## Module Files

Store DLLs on the API server:

```
./modules/<moduleId>/<moduleId>.dll
./modules/_shared/*.dll
```

- Put the module assembly itself in its own module folder.
- Put common or reusable dependency DLLs in `./modules/_shared`.
- Any extra `.dll` files still left in a module folder are sent as module-specific dependencies.
- DLLs in `./modules/_shared` are sent separately as shared dependencies.
