Skip to content

API 文档

IPAM 提供 RESTful API 接口,支持标准的 HTTP 方法和 JSON 数据格式。

API 概览

基础信息

项目内容
基础 URLhttp://localhost:8080/api
数据格式JSON
认证方式JWT Token
字符编码UTF-8

请求格式

http
GET /api/ips HTTP/1.1
Host: localhost:8080
Authorization: Bearer <token>
Content-Type: application/json

响应格式

json
{
  "code": 200,
  "message": "操作成功",
  "data": {}
}

标准响应码

状态码说明
200请求成功
201创建成功
400请求参数错误
401未授权
403禁止访问
404资源不存在
500服务器内部错误

认证

获取验证码

http
GET /api/captcha

响应:

json
{
  "code": 200,
  "data": {
    "captcha_id": "captcha_abc123",
    "image": "data:image/png;base64,..."
  }
}

登录

http
POST /api/login
Content-Type: application/json

{
  "username": "admin",
  "password": "admin123",
  "captcha_id": "captcha_abc123",
  "captcha_code": "abcd"
}

响应:

json
{
  "code": 200,
  "message": "登录成功",
  "data": {
    "token": "eyJhbGciOiJIUzI1NiIs...",
    "user": {
      "id": 1,
      "username": "admin",
      "nickname": "管理员",
      "role": "admin"
    },
    "first_login": false
  }
}

使用 Token

在请求头中添加 Authorization:

http
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...

注销登录

http
POST /api/logout
Authorization: Bearer <token>

响应:

json
{
  "code": 200,
  "message": "注销成功"
}

获取用户资料

http
GET /api/profile
Authorization: Bearer <token>

用户注册

http
POST /api/users/register
Content-Type: application/json

{
  "username": "newuser",
  "password": "password123",
  "nickname": "新用户"
}

获取注册配置

http
GET /api/auth/register-config

响应:

json
{
  "code": 200,
  "data": {
    "allow_register": true,
    "default_role": "operator"
  }
}

灵活查询

系统采用灵活查询模式,支持动态过滤、排序和分页。

查询参数

参数类型说明
pageint页码,默认 1
page_sizeint每页数量,默认 20
orderstring排序字段
order_bystring排序方式:asc/desc
filtersJSON过滤条件

过滤条件格式

json
{
  "filters": {
    "field_name": {
      "operator": "value"
    }
  }
}

支持的运算符:

运算符说明
eq等于
neq不等于
gt大于
gte大于等于
lt小于
lte小于等于
like模糊匹配
in在列表中
not_in不在列表中
is_null为空
is_not_null不为空

查询示例

http
GET /api/ips?page=1&page_size=20&order=id&order_by=desc
GET /api/ips?filters={"ip":{"like":"192.168"}}

响应格式

json
{
  "code": 200,
  "message": "success",
  "data": {
    "list": [],
    "total": 100,
    "page": 1,
    "page_size": 20
  }
}

错误处理

错误响应格式

json
{
  "code": 400,
  "message": "请求参数错误",
  "error": {
    "code": "INVALID_PARAMETER",
    "details": {}
  }
}

错误码列表

错误码说明
INVALID_PARAMETER请求参数错误
UNAUTHORIZED未授权
FORBIDDEN禁止访问
NOT_FOUND资源不存在
ALREADY_EXISTS资源已存在
INTERNAL_ERROR内部服务器错误

数据校验错误码

错误码说明适用场景
NAME_EXISTS名称已存在单位、部门、项目、网络区域等的名称唯一性校验
CODE_EXISTS编码已存在单位、部门、项目、网络区域等的编码唯一性校验
ID_NOT_FOUNDID不存在更新时指定的资源ID不存在
INVALID_LEVEL行政级别无效单位级别不在1-5范围内
INVALID_PARENT上级单位无效层级关系校验失败
PARENT_NOT_FOUND上级单位不存在指定的上级单位ID不存在
PROVINCE_HAS_PARENT省级单位不能有上级1级单位设置了ParentID
MISSING_PARENT缺少上级单位N级单位(N>1)未指定上级

资源接口

IP 地址管理

方法路径说明
GET/api/ips获取 IP 列表(灵活查询)
GET/api/ips/count获取 IP 数量
GET/api/ips/options获取选项列表
POST/api/ips/allocate分配 IP
PATCH/api/ips灵活更新 IP
POST/api/ips/release释放 IP

子网管理

方法路径说明
GET/api/subnets获取子网列表(灵活查询)
GET/api/subnets/count获取子网数量
GET/api/subnets/options获取选项列表
POST/api/subnets创建子网
PATCH/api/subnets灵活更新子网
DELETE/api/subnets删除子网

网关管理

方法路径说明
GET/api/gateways获取网关列表(灵活查询)
GET/api/gateways/count获取网关数量
GET/api/gateways/options获取选项列表
POST/api/gateways创建网关
PATCH/api/gateways灵活更新网关
DELETE/api/gateways删除网关

网络区域管理

方法路径说明
GET/api/network_areas获取网络区域列表(灵活查询)
GET/api/network_areas/count获取网络区域数量
GET/api/network_areas/options获取选项列表
POST/api/network_areas创建网络区域
PATCH/api/network_areas灵活更新网络区域
DELETE/api/network_areas删除网络区域

单位管理

方法路径说明
GET/api/units获取单位列表(灵活查询)
GET/api/units/count获取单位数量
GET/api/units/options获取选项列表
POST/api/units创建单位
PATCH/api/units灵活更新单位
DELETE/api/units删除单位

部门管理

方法路径说明
GET/api/departments获取部门列表(灵活查询)
GET/api/departments/count获取部门数量
GET/api/departments/options获取选项列表
POST/api/departments创建部门
PATCH/api/departments灵活更新部门
DELETE/api/departments删除部门

项目管理

方法路径说明
GET/api/projects获取项目列表(灵活查询)
GET/api/projects/count获取项目数量
GET/api/projects/options获取选项列表
POST/api/projects创建项目
PATCH/api/projects灵活更新项目
DELETE/api/projects删除项目

使用人管理

方法路径说明
GET/api/user_contacts获取使用人列表(灵活查询)
GET/api/user_contacts/count获取使用人数量
GET/api/user_contacts/options获取选项列表
POST/api/user_contacts创建使用人
PATCH/api/user_contacts灵活更新使用人
DELETE/api/user_contacts批量删除使用人

用户组管理

方法路径说明
GET/api/user-groups获取用户组列表(灵活查询)
GET/api/user-groups/count获取用户组数量
GET/api/user-groups/options获取选项列表
POST/api/user-groups创建用户组(管理员)
PATCH/api/user-groups灵活更新用户组(管理员)
DELETE/api/user-groups删除用户组(管理员)

字段定义管理

方法路径说明
GET/api/field_definitions获取字段定义列表(灵活查询)
GET/api/field_definitions/count获取字段定义数量
GET/api/field_definitions/options获取选项列表
POST/api/field_definitions创建字段定义(管理员)
PATCH/api/field_definitions灵活更新字段定义(管理员)
DELETE/api/field_definitions删除字段定义(管理员)

IP 注册申请管理

方法路径说明
GET/api/registerIps获取注册申请列表(灵活查询)
GET/api/registerIps/count获取注册申请数量
POST/api/registerIps创建注册申请
PUT/api/registerIps灵活更新注册申请
DELETE/api/registerIps删除注册申请

用户管理

方法路径说明
GET/api/users获取用户列表
POST/api/users创建用户(管理员)
PATCH/api/users更新用户(管理员)
DELETE/api/users删除用户(管理员)

登录日志

方法路径说明
GET/api/login-logs获取登录日志列表
DELETE/api/login-logs删除登录日志(管理员)

智能导入导出

智能导出

http
POST /api/smart/export
Authorization: Bearer <token>
Content-Type: application/json

{
  "resource_types": ["network_areas", "units"],
  "include_dependencies": true,
  "filters": {}
}

响应:

json
{
  "code": 200,
  "message": "导出成功",
  "data": {
    "file_path": "/exports/xxx.xlsx",
    "file_name": "export_20240115.xlsx"
  }
}

导出全部

http
POST /api/smart/export/all

导入预览

http
POST /api/smart/import/preview
Content-Type: multipart/form-data

file: <Excel文件>

执行导入

http
POST /api/smart/import/execute
Content-Type: multipart/form-data

file: <Excel文件>

下载导入模板

http
GET /api/smart/import/template?type=network_areas

通知管理

方法路径说明
GET/api/notifications获取通知列表
GET/api/notifications/count获取通知数量
GET/api/notifications/unread-count获取未读数量
PATCH/api/notifications/mark-read标记已读
PATCH/api/notifications/mark-all-read标记全部已读
POST/api/notifications创建通知(管理员)
PATCH/api/notifications更新通知(管理员)
DELETE/api/notifications删除通知(管理员)

通知配置管理

方法路径说明
GET/api/notification-configs获取通知配置列表
GET/api/notification-configs/count获取配置数量
GET/api/notification-configs/options获取选项列表
POST/api/notification-configs创建配置(管理员)
PATCH/api/notification-configs更新配置(管理员)
DELETE/api/notification-configs删除配置(管理员)

邮箱配置管理

方法路径说明
GET/api/email-configs获取邮箱配置列表
GET/api/email-configs/count获取配置数量
GET/api/email-configs/options获取选项列表
POST/api/email-configs创建邮箱配置(管理员)
PATCH/api/email-configs更新邮箱配置(管理员)
PATCH/api/email-configs/set-default/:id设置默认配置(管理员)
POST/api/email-configs/test/:id测试连接(管理员)
POST/api/email-configs/send发送邮件(管理员)
DELETE/api/email-configs删除配置(管理员)

系统配置

方法路径说明
GET/api/config/registration获取注册配置
PUT/api/config/registration更新注册配置(管理员)

探针管理

探针客户端接口(无需认证)

方法路径说明
POST/probe/register注册探针客户端
POST/probe/heartbeat探针心跳
POST/probe/report上报扫描结果

探针管理接口(需要认证)

方法路径说明
GET/api/probe/clients获取探针客户端列表
GET/api/probe/clients/stats获取探针统计
GET/api/probe/clients/count获取探针数量
PATCH/api/probe/clients更新探针客户端
DELETE/api/probe/clients删除探针客户端
GET/api/probe/reports获取上报记录列表
GET/api/probe/reports/stats获取上报记录统计
GET/api/probe/reports/count获取上报记录数量
DELETE/api/probe/reports删除上报记录

备份管理

备份配置

方法路径说明
GET/api/backup/config获取备份配置
PUT/api/backup/config更新备份配置(管理员)

手动备份

方法路径说明
POST/api/backup/manual手动执行备份(管理员)

备份记录

方法路径说明
GET/api/backup/records获取备份记录列表
DELETE/api/backup/records/:id删除备份记录(管理员)

备份下载

方法路径说明
GET/api/backup/download/:id下载备份文件

备份统计

方法路径说明
GET/api/backup/stats获取备份统计信息

系统信息

获取系统信息

http
GET /api/system/info

响应:

json
{
  "code": 200,
  "data": {
    "app_name": "IPAM - IP地址管理系统",
    "version": "1.2.0",
    "description": "企业级IP地址管理系统",
    "author": "坐公交也用券",
    "license": "MIT",
    "website": "https://ipam.liumou.site/",
    "go_version": "go1.21",
    "os": "linux",
    "arch": "amd64",
    "start_time": "2024-01-15T10:00:00Z",
    "uptime": "24h0m0s",
    "server_host": "0.0.0.0",
    "server_port": 8080,
    "database_type": "mysql",
    "database_host": "localhost"
  }
}

数据库管理

获取数据库信息

http
GET /api/database/info

执行数据库操作(管理员)

http
POST /api/database/execute

示例代码

cURL

bash
# 登录
curl -X POST http://localhost:8080/api/login \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"admin123"}'

# 获取 IP 列表
curl -X GET "http://localhost:8080/api/ips?page=1&page_size=20" \
  -H "Authorization: Bearer <token>"

# 创建子网
curl -X POST http://localhost:8080/api/subnets \
  -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "办公网段",
    "cidr": "192.168.1.0/24",
    "gateway_id": 1,
    "network_area_id": 1
  }'

# 灵活更新子网
curl -X PATCH http://localhost:8080/api/subnets \
  -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -d '{
    "id": 1,
    "name": "新名称"
  }'

# 删除子网(批量)
curl -X DELETE http://localhost:8080/api/subnets \
  -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -d '{"ids": [1, 2, 3]}'

Python

python
import requests

BASE_URL = 'http://localhost:8080/api'

# 登录
response = requests.post(f'{BASE_URL}/login', json={
    'username': 'admin',
    'password': 'admin123'
})
token = response.json()['data']['token']
headers = {'Authorization': f'Bearer {token}'}

# 获取 IP 列表
response = requests.get(f'{BASE_URL}/ips', headers=headers, params={
    'page': 1,
    'page_size': 20
})
ip_list = response.json()['data']['list']

# 创建子网
response = requests.post(f'{BASE_URL}/subnets', headers=headers, json={
    'name': '办公网段',
    'cidr': '192.168.1.0/24',
    'gateway_id': 1,
    'network_area_id': 1
})

# 灵活更新
response = requests.patch(f'{BASE_URL}/subnets', headers=headers, json={
    'id': 1,
    'name': '新名称'
})

# 灵活查询示例
response = requests.get(f'{BASE_URL}/ips', headers=headers, params={
    'filters': '{"ip":{"like":"192.168"}}',
    'order': 'id',
    'order_by': 'desc'
})

JavaScript

javascript
const BASE_URL = 'http://localhost:8080/api';

const login = async () => {
  const response = await fetch(`${BASE_URL}/login`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ username: 'admin', password: 'admin123' })
  });
  const data = await response.json();
  return data.data.token;
};

const getIPs = async (token, params = {}) => {
  const query = new URLSearchParams(params).toString();
  const response = await fetch(`${BASE_URL}/ips?${query}`, {
    headers: { 'Authorization': `Bearer ${token}` }
  });
  return await response.json();
};

const createSubnet = async (token, subnet) => {
  const response = await fetch(`${BASE_URL}/subnets`, {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${token}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(subnet)
  });
  return await response.json();
};

const flexibleUpdate = async (token, resource, data) => {
  const response = await fetch(`${BASE_URL}/${resource}`, {
    method: 'PATCH',
    headers: {
      'Authorization': `Bearer ${token}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
  });
  return await response.json();
};

Swagger 文档

系统提供 Swagger UI 接口文档:

http://localhost:8080/swagger/index.html

Swagger 文档包含所有接口的详细说明、请求参数和响应示例。

基于 MIT 许可发布