# json-db **Repository Path**: nachao/json-db ## Basic Information - **Project Name**: json-db - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-10-09 - **Last Updated**: 2025-11-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # JSON DB - 极简JSON数据库 一个基于Go的极简JSON数据库应用,通过HTTP接口实现JSON数据的存储和查询。 ## 功能特性 - **GET** 请求:查询并返回完整的JSON数据 - **POST** 请求:保存或更新JSON数据 - **DELETE** 请求:删除数据文件 - **自动验证**:scopename只允许英文字母、数字、下划线和连字符,最大长度50字符 - **数据持久化**:所有数据存储在 `./data` 目录下的JSON文件中 ## 快速开始 ### 1. 启动服务 ```bash go run main.go ``` 服务默认运行在 `http://localhost:9090` ### 2. 使用示例 #### 保存数据(POST) ```bash # 保存用户数据 curl -X POST http://localhost:9090/users \ -H "Content-Type: application/json" \ -d '{ "name": "张三", "age": 25, "email": "zhangsan@example.com" }' # 保存配置数据 curl -X POST http://localhost:9090/app_config \ -H "Content-Type: application/json" \ -d '{ "theme": "dark", "language": "zh-CN", "features": ["auth", "analytics"] }' ``` #### 查询数据(GET) ```bash # 查询用户数据 curl http://localhost:9090/users # 查询配置数据 curl http://localhost:9090/app_config ``` #### 删除数据(DELETE) ```bash # 删除用户数据 curl -X DELETE http://localhost:9090/users # 删除配置数据 curl -X DELETE http://localhost:9090/app_config ``` #### 查看API说明 ```bash curl http://localhost:9090/ ``` #### 查看产品介绍页面 在浏览器中访问:`http://localhost:9090/index.html` 这是一个炫酷的未来科幻风格介绍页面,包含完整的产品说明和使用文档。 ## 项目结构 ``` json-db/ ├── main.go # 主程序文件 ├── go.mod # Go模块配置 ├── index.html # 产品介绍页面(未来科幻风格) ├── data/ # 数据存储目录(自动创建) │ ├── users.json │ └── app_config.json └── README.md # 项目说明 ``` ## 配置说明 可以在 `main.go` 中修改以下常量: ```go const ( dataDir = "./data" // 数据存储目录 maxScopeNameLength = 50 // scopename最大长度 port = 9090 // 服务端口 ) ``` ## 命名规则 scopename必须符合以下规则: - 只允许英文字母(大小写) - 数字 - 下划线(_) - 连字符(-) - 长度:1-50个字符 有效示例:`users`, `app_config`, `user-profile`, `data2024` 无效示例:`用户数据`, `user/data`, `config.json`, `very-long-name-that-exceeds-fifty-characters-limit` ## 编译运行 ### 开发模式 ```bash go run main.go ``` ### 编译可执行文件 ```bash # Windows go build -o json-db.exe # Linux/Mac go build -o json-db # 使用 Makefile(推荐) make build # 本地平台 make build-linux # Linux平台(用于部署) ``` ### 运行可执行文件 ```bash # Windows ./json-db.exe # Linux/Mac ./json-db # 使用 Makefile make run ``` ### 自定义端口 程序会自动从 `config.conf` 文件读取端口配置: ```conf # config.conf SERVER_PORT=6102 ``` 如果没有 config.conf 文件,程序会使用默认端口 9090。 ## 服务器部署 ### 使用 ssh-deploy 快速部署 详细部署指南请参考 [DEPLOY.md](DEPLOY.md) ```bash # 1. 构建 Linux 版本 make build-linux # 2. 部署(需要配置好 config.conf) make deploy # 3. 在服务器上启动(会自动从 config.conf 读取端口) ssh user@your-server cd /opt/json-db ./server-start.sh ``` ### 常见部署问题 - **端口配置**: 程序会从 `config.conf` 文件读取 `SERVER_PORT` 配置 - **配置文件**: 确保 `UPLOAD_FILES` 包含 `config.conf,index.html` - **防火墙**: 记得开放 config.conf 中配置的端口 ## 注意事项 1. **数据安全**:本应用为极简实现,未包含身份验证和授权机制,不建议在生产环境直接使用 2. **并发安全**:当前实现未处理并发写入冲突,高并发场景下可能存在数据覆盖问题 3. **数据备份**:`data/` 目录包含所有数据,请定期备份 4. **文件权限**:确保应用有 `data/` 目录的读写权限 5. **端口配置**:程序从 `config.conf` 文件读取 `SERVER_PORT` 配置,默认 9090 ## 扩展建议 如需在生产环境使用,建议添加: - 身份验证(API Key、JWT等) - 并发控制(文件锁或数据库) - 数据备份机制 - 请求日志记录 - 数据加密存储 - HTTPS支持