Add Makefile, add announcement API, update FE

This commit is contained in:
BennyThink
2021-06-15 17:28:17 +08:00
parent 010e787edb
commit 3caced8035
5 changed files with 94 additions and 5 deletions

26
API.md
View File

@@ -314,4 +314,28 @@
"size": "15.0B"
}
}
```
```
# 公告
## 添加公告
* POST `/api/announcement`, json 字段 content
## 获取公告
* GET `/api/announcmement`接受URL参数 size、page
```json
{
"data": [
{
"username": "Benny",
"date": "2021-06-15 16:28:16",
"browser": "PostmanRuntime/7.28.0",
"content": "hello"
}
],
"count": 1
}
```

View File

@@ -14,12 +14,11 @@ RUN apk update && apk add --no-cache libressl jpeg-dev openjpeg-dev libimagequan
FROM node:alpine as nodebuilder
WORKDIR /YYeTsBot/YYeTsFE/
COPY YYeTsFE/package.json /YYeTsBot/YYeTsFE/
COPY YYeTsFE/yarn.lock /YYeTsBot/YYeTsFE/
RUN yarn
COPY YYeTsFE /YYeTsBot/YYeTsFE/
RUN yarn build
RUN yarn run release
FROM runner
@@ -30,7 +29,6 @@ COPY --from=pybuilder /usr/share/zoneinfo /usr/share/zoneinfo
RUN true
COPY --from=nodebuilder /YYeTsBot/YYeTsFE/build /YYeTsBot/yyetsweb
ENV TZ=Asia/Shanghai
WORKDIR /YYeTsBot/yyetsbot
CMD ["python", "yyetsbot.py"]

16
Makefile Normal file
View File

@@ -0,0 +1,16 @@
default:
make dev
dev:
rm -f YYeTsFE/.env
git checkout docker-compose.yml
git pull
git submodule update --remote
cp .env YYeTsFE/.env
docker build -t bennythink/yyetsbot .
cp ../docker-compose.yml ./docker-compose.yml
docker-compose up -d
clean:
docker rmi bennythink/yyetsbot:latest

Submodule YYeTsFE updated: deb313b74c...c9a111737d

View File

@@ -560,6 +560,56 @@ class CommentHandler(BaseHandler):
self.write(resp)
class AnnouncementHandler(CommentHandler):
@run_on_executor()
def get_announcement(self):
size = int(self.get_argument("size", "5"))
page = int(self.get_argument("page", "1"))
condition = {}
count = self.mongo.db["announcement"].count_documents(condition)
data = self.mongo.db["announcement"].find(condition, projection={"_id": False, "ip": False}) \
.sort("_id", pymongo.DESCENDING).limit(size).skip((page - 1) * size)
return {
"data": list(data),
"count": count,
}
@run_on_executor()
def add_announcement(self):
if not self.is_admin():
self.set_status(HTTPStatus.FORBIDDEN)
return {"message": "只有管理员可以设置公告"}
payload = json.loads(self.request.body)
content = payload["content"]
username = self.get_current_user()
real_ip = AntiCrawler(self).get_real_ip()
construct = {
"username": username.decode("u8"),
"ip": real_ip,
"date": DBDumpHandler.ts_date(None),
"browser": self.request.headers['user-agent'],
"content": content,
}
self.mongo.db["announcement"].insert_one(construct)
self.set_status(HTTPStatus.CREATED)
return {"message": "添加成功"}
@gen.coroutine
def get(self):
resp = yield self.get_announcement()
self.write(resp)
@gen.coroutine
def post(self):
resp = yield self.add_announcement()
self.write(resp)
class CaptchaHandler(BaseHandler):
@run_on_executor()
@@ -796,6 +846,7 @@ class RunServer:
(r'/api/grafana/query', GrafanaQueryHandler),
(r'/api/blacklist', BlacklistHandler),
(r'/api/db_dump', DBDumpHandler),
(r'/api/announcement', AnnouncementHandler),
(r'/', IndexHandler),
(r'/(.*\.html|.*\.js|.*\.css|.*\.png|.*\.jpg|.*\.ico|.*\.gif|.*\.woff2|.*\.gz|.*\.zip|.*\.svg|.*\.json)',
web.StaticFileHandler,