Skip to content

Commit 63d5bf6

Browse files
committed
初次上传
0 parents  commit 63d5bf6

File tree

11 files changed

+822
-0
lines changed

11 files changed

+822
-0
lines changed

README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
## 一言·古诗词 API
2+
3+
<div style="text-align:center">
4+
<img src="https://api.gushi.ci/all.svg">
5+
</div>
6+
7+
8+
### 简介
9+
10+
古诗词·一言API 是一个可以随机返回一句古诗词名句的接口。具有以下特点:
11+
12+
* 快:使用 Vert.x Java 全异步框架开发,使用Redis数据库,确保毫秒级稳定响应。
13+
* 全:支持 svg / txt / json / png 调用,满足你在任何地方的调用需求
14+
* 准:可以根据你的喜好,在指定的分类中进行随机返回
15+
16+
本仓库是该项目的开源代码仓库,仅提供测试数据,其余数据请自行采集。
17+
18+
项目首页:[https://gushi.ci](https://gushi.ci)
19+
20+
21+
### 安装
22+
23+
1. 根据 Maven 安装依赖
24+
2. maven package 可打成 Jar 包
25+
3. 配置 redis,导入 dump.rdb 数据 (放到 redis 目录下,重启 redis,如果原来有这个文件请自行导出数据再替换,原有数据会全部清除)
26+
4. 运行:
27+
1. 服务器运行 `java -server -jar yiyan-1.0-fat.jar -conf conf.json`  conf.json从下面可以复制出来自己配
28+
2. IDE运行 启动类 `io.vertx.core.Launcher` 参数 `run ma.luan.yiyan.MainVerticle -conf src/main/resources/conf.json`
29+
30+
5. 如果要自行采集数据,请仿照原有 redis 中的 key 格式采集,log自动添加,help为可选,需要自己创建 text 和 img 两个 key。古诗词数据每个分类一个 set,set的内容是 json。图片使用base64加密储存。其他细节请自行查看源码。
31+
32+
### 使用
33+
34+
#### API举例
35+
36+
* [https://api.gushi.ci/all.json](https://api.gushi.ci/all.json)
37+
* [https://api.gushi.ci/all.svg](https://api.gushi.ci/all.svg)
38+
* [https://api.gushi.ci/shuqing/libie.png](https://api.gushi.ci/shuqing/libie.png)
39+
* [https://api.gushi.ci/rensheng.txt](https://api.gushi.ci/rensheng.txt)
40+
41+
#### API地址格式(仅支持https)
42+
43+
`https://api.gushi.ci/{一级分类}/{二级分类(可选)}.{返回格式(可选)}`
44+
45+
查看所有目前支持的分类:[https://api.gushi.ci/](https://api.gushi.ci/)
46+
47+
目前支持的后缀:.svg .txt .png .json 不加后缀默认返回 json
48+
49+
#### SVG 调用
50+
51+
```html
52+
<img src="https://api.gushi.ci/all.svg">
53+
```
54+
55+
SVG后缀是我们推荐的最优调用方案,可以在部分论坛、任何博客、小程序内无损直接调用,并且可以一定程度上控制样式。缺点是部分老旧浏览器不支持。
56+
57+
#### JSON 调用
58+
59+
```html
60+
<script>
61+
var xhr = new XMLHttpRequest();
62+
xhr.open('get', 'https://api.gushi.ci/all.json');
63+
xhr.onreadystatechange = function () {
64+
if (xhr.readyState === 4) {
65+
var data = JSON.parse(xhr.responseText);
66+
var gushici = document.getElementById('gushici');
67+
gushici.innerText = data.content;
68+
}
69+
};
70+
xhr.send();
71+
</script>
72+
```
73+
74+
JSON调用可以获取来源、作者、分类等信息,可以供你自定义拼接显示效果。
75+
76+
#### PNG 调用
77+
78+
```html
79+
<img src="https://api.gushi.ci/all.png">
80+
```
81+
82+
我们会提供透明的PNG文件。PNG方法兼容性最好,可以在几乎任何地方插入。并且支持所有浏览器。缺点是不能控制样式,另外,由于流量限制,我们只会给您传送较小的图片源文件。
83+
84+
#### TXT 调用
85+
86+
```html
87+
<script>
88+
var xhr = new XMLHttpRequest();
89+
xhr.open('get', 'https://api.gushi.ci/all.txt');
90+
xhr.onreadystatechange = function () {
91+
if (xhr.readyState === 4) {
92+
var data = JSON.parse(xhr.responseText);
93+
var gushici = document.getElementById('gushici');
94+
gushici.innerText = data.content;
95+
}
96+
};
97+
xhr.send();
98+
</script>
99+
```
100+
101+
TXT调用和JSON调用基本一致,可以节省一些流量。或者,你甚至可以使用 iframe 来调用我们的接口
102+
103+
#### 获取七天点击量数据
104+
[https://api.gushi.ci/log](https://api.gushi.ci/log)
105+
106+
107+
### 技术说明
108+
109+
1. MainVerticle 用来部署其他 Verticle
110+
2. ApiVerticle 为核心部分,负责处理请求
111+
3. DataService 和 LogService 负责提供查询服务
112+
4. ConvertUtil 负责转码
113+
5. Service 没有使用 Service Proxy,因此无需额外生成代码。
114+
115+
116+
117+
### 关于项目
118+
119+
这是我的个人项目,目的是为了弘扬中国传统文化。
120+
121+
名句数据由古诗文网收录整理,特此感谢。
122+
123+
若有任何建议,或者有工作实习的机会(本人大学在读),请联系 meetlhx#qq.com
124+
125+
若您对本人的其他作品或者文章感兴趣,请访问我的博客:[https://luan.ma](https://luan.ma/)

dump.rdb

1.75 MB
Binary file not shown.

pom.xml

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>ma.luan</groupId>
8+
<artifactId>yiyan</artifactId>
9+
<version>1.0</version>
10+
11+
<properties>
12+
<java.version>1.8</java.version>
13+
<vertx.version>3.5.3</vertx.version>
14+
</properties>
15+
16+
<build>
17+
<resources>
18+
<resource>
19+
<directory>src/main/resources</directory>
20+
</resource>
21+
</resources>
22+
23+
<plugins>
24+
<plugin>
25+
<groupId>org.apache.maven.plugins</groupId>
26+
<artifactId>maven-compiler-plugin</artifactId>
27+
<version>3.8.0</version>
28+
<configuration>
29+
<source>${java.version}</source>
30+
<target>${java.version}</target>
31+
</configuration>
32+
</plugin>
33+
34+
<plugin>
35+
<groupId>org.apache.maven.plugins</groupId>
36+
<artifactId>maven-shade-plugin</artifactId>
37+
<version>2.4.3</version>
38+
<executions>
39+
<execution>
40+
<phase>package</phase>
41+
<goals>
42+
<goal>shade</goal>
43+
</goals>
44+
<configuration>
45+
<transformers>
46+
<transformer
47+
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
48+
<manifestEntries>
49+
<Main-Class>io.vertx.core.Launcher</Main-Class>
50+
<Main-Verticle>ma.luan.yiyan.MainVerticle</Main-Verticle>
51+
</manifestEntries>
52+
</transformer>
53+
<transformer
54+
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
55+
<resource>META-INF/services/io.vertx.core.spi.VerticleFactory</resource>
56+
<resource>META-INF/services/io.vertx.core.spi.launcher.CommandFactory</resource>
57+
</transformer>
58+
</transformers>
59+
<artifactSet>
60+
</artifactSet>
61+
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar
62+
</outputFile>
63+
</configuration>
64+
</execution>
65+
</executions>
66+
</plugin>
67+
68+
<plugin>
69+
<groupId>org.codehaus.mojo</groupId>
70+
<artifactId>exec-maven-plugin</artifactId>
71+
<version>1.5.0</version>
72+
<configuration>
73+
<mainClass>io.vertx.core.Launcher</mainClass>
74+
<arguments>
75+
<argument>run</argument>
76+
<argument>ma.luan.yiyan.MainVerticle</argument>
77+
</arguments>
78+
</configuration>
79+
</plugin>
80+
81+
</plugins>
82+
</build>
83+
84+
<dependencies>
85+
86+
<dependency>
87+
<groupId>io.vertx</groupId>
88+
<artifactId>vertx-core</artifactId>
89+
<version>${vertx.version}</version>
90+
</dependency>
91+
92+
<dependency>
93+
<groupId>io.vertx</groupId>
94+
<artifactId>vertx-unit</artifactId>
95+
<version>${vertx.version}</version>
96+
</dependency>
97+
98+
<dependency>
99+
<groupId>io.vertx</groupId>
100+
<artifactId>vertx-web</artifactId>
101+
<version>${vertx.version}</version>
102+
</dependency>
103+
104+
<dependency>
105+
<groupId>io.vertx</groupId>
106+
<artifactId>vertx-redis-client</artifactId>
107+
<version>${vertx.version}</version>
108+
</dependency>
109+
110+
<dependency>
111+
<groupId>org.apache.logging.log4j</groupId>
112+
<artifactId>log4j-api</artifactId>
113+
<version>2.11.1</version>
114+
</dependency>
115+
<dependency>
116+
<groupId>org.apache.logging.log4j</groupId>
117+
<artifactId>log4j-core</artifactId>
118+
<version>2.11.1</version>
119+
</dependency>
120+
121+
122+
</dependencies>
123+
124+
</project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package ma.luan.yiyan;
2+
3+
import io.vertx.core.AbstractVerticle;
4+
5+
6+
import io.vertx.core.DeploymentOptions;
7+
import io.vertx.core.Future;
8+
import io.vertx.redis.RedisOptions;
9+
import ma.luan.yiyan.api.ApiVerticle;
10+
import ma.luan.yiyan.service.DataService;
11+
import ma.luan.yiyan.service.LogService;
12+
import org.apache.logging.log4j.LogManager;
13+
import org.apache.logging.log4j.Logger;
14+
15+
16+
/**
17+
* @author 乱码 https://luan.ma/
18+
*/
19+
public class MainVerticle extends AbstractVerticle {
20+
21+
private Logger log = LogManager.getLogger(this.getClass());
22+
23+
@Override
24+
public void start() {
25+
26+
// 读取配置文件
27+
RedisOptions redisOptions = new RedisOptions()
28+
.setHost(config().getJsonObject("redis").getString("host","127.0.0.1"))
29+
.setPort(config().getJsonObject("redis").getInteger("port",6379))
30+
.setSelect(config().getJsonObject("redis").getInteger("select",0));
31+
32+
// 顺序部署 Verticle
33+
Future.<Void>succeededFuture()
34+
.compose(v -> Future.<String>future(s -> vertx.deployVerticle(new ApiVerticle(),new DeploymentOptions().setConfig(config()), s)))
35+
.compose(v -> Future.<String>future(s -> vertx.deployVerticle(new DataService(redisOptions), new DeploymentOptions().setConfig(config()),s)))
36+
.compose(v -> Future.<String>future(s -> vertx.deployVerticle(new LogService(redisOptions), s)))
37+
.compose(v -> log.info("Vert.x started successfully"),
38+
Future.future().setHandler(ar -> {
39+
if (ar.failed()) {
40+
log.error("Vert.x failed to start", ar.cause());
41+
}
42+
}));
43+
}
44+
}

0 commit comments

Comments
 (0)