# Local Webserver

Minimal local webserver for development with a health endpoint.

## Run

```bash
python3 server.py
```

Defaults:
- `HOST=0.0.0.0`
- `PORT=8808`

Override example:

```bash
HOST=0.0.0.0 PORT=9000 python3 server.py
```

## Endpoints

- `GET /health` -> `200` JSON: `{"status":"ok"}`
- `POST /api/suggestions` -> `201` JSON: stores a portal improvement suggestion for engineering review
- `GET /api/suggestions` -> `200` JSON: returns all submitted suggestions

## Test

```bash
python3 -m unittest discover -s tests -v
```

## Operational Runbook

1. Start server: `python3 server.py`.
2. Open portal locally: `http://127.0.0.1:8808/self-improvement-portal.html`.
3. Open Founding Engineer queue: `http://127.0.0.1:8808/founding-engineer-review.html`.
4. Verify health locally: `curl -i http://127.0.0.1:8808/health`.
5. Submit a suggestion via API:
```bash
curl -i -X POST http://127.0.0.1:8808/api/suggestions \
  -H 'Content-Type: application/json' \
  -d '{"requester":"Alex","title":"Add priority tags","details":"Allow users to assign suggestion priority."}'
```
6. List submitted suggestions:
```bash
curl -s http://127.0.0.1:8808/api/suggestions | python3 -m json.tool
```
7. Verify remote access from another machine: `curl -i http://<server-ip>:8808/health`.
8. If port is occupied (`OSError: [Errno 98] Address already in use`), set `PORT` to an unused value.
9. Stop with `Ctrl+C`.

## Start On Boot (systemd)

A unit file is provided at `deploy/systemd/local-webserver.service`.

Preferred install (auto-writes correct local user/path):

```bash
sudo bash scripts/install_systemd_service.sh
```

If `8808` is already used by another service (for example Docker publish), install on a different port:

```bash
PORT=8810 sudo bash scripts/install_systemd_service.sh
```

Check status/logs:

```bash
sudo systemctl status local-webserver.service
sudo journalctl -u local-webserver.service -f
```

## Remote Access Troubleshooting

If `http://<server-ip>:8808/health` is unreachable from another machine:

1. Confirm service is running:
```bash
sudo systemctl status local-webserver.service
```
2. Confirm local health endpoint works on the server host:
```bash
curl -i http://127.0.0.1:8808/health
```
3. Confirm the server host IP is correct:
```bash
hostname -I
```
4. Open firewall port `8808` (if firewall is enabled):
```bash
sudo ufw allow 8808/tcp
```
5. If running in Docker, verify port publish includes all interfaces (not localhost-only):
```bash
docker ps --format 'table {{.Names}}\t{{.Ports}}'
```
Expected publish includes `0.0.0.0:8808->8808/tcp` or `*:8808->8808/tcp`.

Run the full diagnostics bundle:
```bash
bash scripts/diagnose_remote_access.sh 8808
```
