Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
修复 配置文件实际目录不位于运行目录的Bug。
Browse files Browse the repository at this point in the history
  • Loading branch information
yeying-xingchen committed Apr 5, 2024
1 parent b795cf4 commit f12a0f8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 24 deletions.
2 changes: 1 addition & 1 deletion example/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"host": "0.0.0.0",
"port": 8080,
"path": "discourse"
"path": "text"
}
File renamed without changes.
17 changes: 9 additions & 8 deletions todaydiscourse/__init__.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
from flask import Flask, request, jsonify
from todaydiscourse import log, core, config
import os

app = Flask(__name__)

@app.route('/', methods=['GET'])
def index():
log.warning(f"请求IP: {request.remote_addr} 请求内容: 错误!调用方式错误!")
log.info(f"请求IP: {request.remote_addr} 请求内容: 错误!调用方式错误!")
return "欢迎来到 TodayDiscourse 今日话语!", 200

@app.route('/text/', methods=['GET'])
def text_endpoint():
log.warning(f"请求IP: {request.remote_addr} 请求内容: 文本")
result = core.get_discourse()
log.info(f"请求IP: {request.remote_addr} 请求内容: 文本")
result = core.get_discourse(os.getcwd())
text = result.get('content', 0)
return text, 200, {'Content-Type': 'text/plain'}

@app.route('/json/', methods=['GET'])
def json_endpoint():
log.warning(f"请求IP: {request.remote_addr} 请求内容: JSON")
response_data = core.get_discourse()
log.info(f"请求IP: {request.remote_addr} 请求内容: JSON")
response_data = core.get_discourse(os.getcwd())
return jsonify(response_data), 200

def start():
log.info("欢迎使用 TodayDiscourse 今日话语")
log.info("开发团队: XingchenOpenSource 星辰开源")
log.info("项目地址: https://github.com/XingchenOpenSource/TodayDiscourse")
log.info("官方文档: https://xingchenopensource.github.io/apis/todaydiscourse/")
config.get_config()
server_port = config.get_config_port()
server_host = config.get_config_host()
config.get_config(os.getcwd())
server_port = config.get_config_port(os.getcwd())
server_host = config.get_config_host(os.getcwd())
log.info(f"🎉恭喜您!今日话语已在 http://localhost:{server_port} 上启动,请参阅官方文档以查看如何调用。")
app.run(host=server_host, port=server_port, threaded=True)

Expand Down
27 changes: 16 additions & 11 deletions todaydiscourse/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import json
from todaydiscourse import log

default_data = {
"host": "0.0.0.0",
"port": 8080,
"path": "discourse"
}
default_data = '''{\n
"host": "0.0.0.0",\n
"port": 8080,\n
"path": "discourse"\n
}'''

def get_config():
json_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'settings.json')
def get_config(path):
json_file_path = path+'/'+'settings.json'
if not os.path.exists(json_file_path):
with open(json_file_path, 'w') as json_file:
json.dump(default_data, json_file)
Expand All @@ -29,18 +29,23 @@ def get_config():
log.warning("配置文件中缺少'host'字段,使用默认值。")
data['host'] = default_data['host']

json_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'settings.json')
def get_config_port():
if 'path' not in data:
log.warning("配置文件中缺少'path'字段,使用默认值。")
data['path'] = default_data['path']
def get_config_port(path):
json_file_path = path+'/'+'settings.json'
with open(json_file_path, 'r') as json_file:
data = json.load(json_file)
return data.get('port', 0)

def get_config_host():
def get_config_host(path):
json_file_path = path+'/'+'settings.json'
with open(json_file_path, 'r') as json_file:
data = json.load(json_file)
return data.get('host', 0)

def get_discourse_path():
def get_discourse_path(path):
json_file_path = path+'/'+'settings.json'
with open(json_file_path, 'r') as json_file:
data = json.load(json_file)
return data.get('path', 0)
6 changes: 2 additions & 4 deletions todaydiscourse/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import json
import random

current_file_path = os.path.abspath(__file__)
current_directory = os.path.dirname(current_file_path)
directory = config.get_discourse_path()

def get_discourse():
def get_discourse(path):
directory = config.get_discourse_path(path)
json_files = [file for file in os.listdir(directory) if file.endswith('.json')]
discourse_file = random.choice(json_files)
with open(os.path.join(directory, discourse_file), 'r', encoding='utf-8') as file:
Expand Down

0 comments on commit f12a0f8

Please sign in to comment.