Sign inSign up

palemoky/chinese-poetry-api

By palemoky

Updated about 2 months ago

高性能中国古诗词 API 服务

Image
1

10K+

palemoky/chinese-poetry-api repository overview

Chinese Poetry API

chinese-poetry
中国古诗词 API 服务

Docker Image Docker Image Size Go Report Card Go Version pre-commit License

基于 Go 语言的高性能中国古诗词 API 服务,支持 REST 和 GraphQL 接口,提供简体/繁体中文、爬虫练习场等功能。

✨ 特性

  • 🚀 高性能: Go 语言编写,支持并发处理,性能优化(简繁转换 ~300ns/op)
  • 📚 海量数据: 包含唐诗、宋词、元曲等近40万首诗词
  • 🔍 强大搜索: 支持全文搜索、标题/内容/作者分类搜索
  • 🌏 双语支持: 同一数据库同时存储简体和繁体中文,通过 ?lang= 参数切换
  • 🎯 多种接口: REST API 和 GraphQL 双接口支持
  • 🛡️ 限流保护: 内置 IP 限流,防止滥用
  • 🐳 容器化: Docker 镜像开箱即用,支持多架构(amd64/arm64)
  • 📊 智能分类: 按朝代、作者、诗词类型自动分类
  • 📝 结构化日志: 使用 zap 高性能日志库
  • 高质量代码: 完整的单元测试、性能测试、模糊测试

🚀 快速开始

使用 Docker(推荐)
docker run -d -p 1279:1279 palemoky/chinese-poetry-api:latest

完整配置参见 docker-compose.yml

使用 Makefile
make help          # 查看所有可用命令
make build         # 构建项目
make process-data  # 处理数据
make run-server    # 启动服务

📡 API 使用

多语言支持

所有接口支持 lang 参数切换简繁体:

参数值说明
zh-Hans简体中文(默认)
zh-Hant繁体中文
REST API
# 简体中文(默认)
curl "http://localhost:1279/api/v1/poems"

# 繁体中文
curl "http://localhost:1279/api/v1/poems?lang=zh-Hant"

# 搜索诗词
curl "http://localhost:1279/api/v1/poems/search?q=静夜思"

# 随机诗词
curl "http://localhost:1279/api/v1/poems/random"

# 作者列表
curl "http://localhost:1279/api/v1/authors?page=1&page_size=20"

# 朝代列表
curl "http://localhost:1279/api/v1/dynasties"
GraphQL API

端点: http://localhost:1279/graphql

# 繁体中文查询
query {
  poems(lang: ZH_HANT, pageSize: 10) {
    edges {
      node {
        title
        content
        author { name }
      }
    }
    totalCount
  }
}

# 搜索诗词
query {
  searchPoems(query: "静夜思", searchType: TITLE) {
    edges {
      node { title author { name } }
    }
  }
}

# 统计信息
query {
  statistics {
    totalPoems
    totalAuthors
    poemsByDynasty { dynasty { name } count }
  }
}

🔍 搜索功能

类型说明示例
all全文搜索(默认)?q=月
title标题搜索?q=静夜思&type=title
content内容搜索?q=床前明月光&type=content
author作者搜索?q=李白&type=author

📖 数据集

本项目基于 chinese-poetry 数据集,包含:

分类数量
五言绝句18895
七言绝句85032
五言律诗71400
七言律诗69028
乐府诗9315
五代词543
宋词21369
元曲10905
诗经305
楚辞65
论语20
四书五经14
其他96232

🏗️ 系统架构

graph TB
    subgraph Client["客户端"]
        REST["REST API<br/>?lang=zh-Hans/zh-Hant"]
        GQL["GraphQL<br/>lang: ZH_HANS/ZH_HANT"]
    end

    subgraph Server["API Server (Gin)"]
        Router["Router + Middleware<br/>Rate Limit / CORS"]
        Handlers["REST Handlers"]
        Resolvers["GraphQL Resolvers"]
        Logger["Structured Logging<br/>(zap)"]
    end

    subgraph Core["Core Layer"]
        Repo["Repository<br/>WithLang()"]
        Search["Search Engine<br/>LIKE Search"]
        Cache["Cache Layer"]
    end

    subgraph Database["SQLite Database"]
        Hans["*_zh_hans Tables<br/>简体中文"]
        Hant["*_zh_hant Tables<br/>繁体中文"]
    end

    REST --> Router --> Handlers --> Repo
    GQL --> Router --> Resolvers --> Repo
    Handlers --> Search
    Resolvers --> Search
    Repo --> Hans
    Repo --> Hant
    Server --> Logger

🙏 致谢

📮 联系方式

如有问题或建议,欢迎提交 IssuePull Request

Tag summary

Content type

Image

Digest

sha256:0de9bf0fd

Size

17.2 MB

Last updated

about 2 months ago

docker pull palemoky/chinese-poetry-api