Add scan queue execution and listing endpoint.

Run scans in background with progress/status updates and store scan metrics in memory or PostgreSQL.

Made-with: Cursor
This commit is contained in:
Andrey Lutsenko
2026-04-09 21:49:38 +10:00
parent 284794ec8d
commit 43cb339605
12 changed files with 753 additions and 23 deletions
+59 -2
View File
@@ -6,9 +6,11 @@
- `GET /health` - проверка состояния API.
- `POST /api/scans` - создание задания скана.
- `GET /api/scans` - список сканов.
- `GET /api/scans/{id}` - просмотр созданного задания.
- Валидация CIDR и exclude IP.
- Хранилище в памяти (для старта). БД подключим следующим шагом.
- Два backend-хранилища: in-memory и PostgreSQL.
- После создания scan запускается фоновый discovery с обновлением статуса и прогресса.
## Требования
@@ -40,6 +42,49 @@ cp .env.example .env
HTTP_ADDR=:8080 go run ./cmd/server
```
По умолчанию используется `STORE_BACKEND=memory`.
## PostgreSQL без Docker
### Вариант A: macOS (Homebrew)
```bash
brew install postgresql@16
brew services start postgresql@16
createdb nettopo
createuser nettopo
```
```bash
psql -d postgres -c "alter user nettopo with password 'nettopo';"
psql -d postgres -c "grant all privileges on database nettopo to nettopo;"
```
### Вариант B: Ubuntu/Debian
```bash
sudo apt update
sudo apt install -y postgresql postgresql-contrib
sudo -u postgres psql -c "create user nettopo with password 'nettopo';"
sudo -u postgres psql -c "create database nettopo owner nettopo;"
```
### Вариант C: Windows
- Установить PostgreSQL через официальный installer.
- Создать БД `nettopo` и пользователя `nettopo` (через pgAdmin или `psql`).
### Запуск API с PostgreSQL
```bash
export STORE_BACKEND=postgres
export DB_DSN='postgres://nettopo:nettopo@localhost:5432/nettopo?sslmode=disable'
export RUN_MIGRATIONS=true
go run ./cmd/server
```
При первом запуске автоматически применится миграция `scan_jobs`.
## Быстрая проверка API
Проверка health:
@@ -73,6 +118,18 @@ curl -s -X POST http://localhost:8080/api/scans \
curl -s http://localhost:8080/api/scans/<scan_id>
```
Получить список scan jobs:
```bash
curl -s http://localhost:8080/api/scans?limit=20
```
В ответе у scan есть:
- `status`: `queued`, `running`, `done`, `failed`
- `progress`: 0..100
- `stats.hosts_total`, `stats.hosts_up`
## Запуск как сервис на Linux (systemd)
1. Собрать бинарник:
@@ -101,4 +158,4 @@ systemctl status nettopo-go
- На IIS настроить Reverse Proxy к `http://localhost:8080`.
- Требуются URL Rewrite + ARR.
Следующим шагом добавим готовый шаблон `web.config` для IIS и миграции PostgreSQL.
Шаблон `web.config` лежит в `deploy/web.config.example`.