init commit

This commit is contained in:
2023-08-10 12:27:18 +08:00
commit 56f93165ba
21 changed files with 457 additions and 0 deletions

44
app/__init__.py Normal file
View File

@@ -0,0 +1,44 @@
import os
# from dotenv import load_dotenv
from apiflask import APIFlask
from chassis.flask_nameko import FlaskPooledClusterRpcProxy
from app.config.config import Config
from app.util.common import basedir
rpc = FlaskPooledClusterRpcProxy()
def register_blueprints(apiflask_app):
from app.api.v1 import create_v1
apiflask_app.register_blueprint(create_v1(), url_prefix='/v1')
def load_app_config(app):
"""
加载环境变量和配置类到app config
"""
# 读取 .env
# load_dotenv(os.path.join(basedir, '.apiflask.env'))
# 读取配置类
app.config.from_object('app.config.config.Config')
def load_rpc_client(apiflask_app):
apiflask_app.config.update(dict(
NAMEKO_AMQP_URI=str(Config.RABBITMQ_URI)
))
rpc.init_app(apiflask_app)
def create_app():
# http wsgi server 托管启动需指定读取环境配置
# load_dotenv(os.path.join(basedir, '.apiflaskenv'))
app = APIFlask(__name__)
load_app_config(app)
register_blueprints(app)
# load_rpc_client(app)
return app

0
app/api/__init__.py Normal file
View File

16
app/api/v1/__init__.py Normal file
View File

@@ -0,0 +1,16 @@
from apiflask import APIBlueprint
from app.api.v1.api import api
def create_v1():
bp_v1 = APIBlueprint('v1', __name__)
bp_v1.register_blueprint(api, url_prefix='/api')
return bp_v1

80
app/api/v1/api.py Normal file
View File

@@ -0,0 +1,80 @@
import socket
import platform
import psutil
from apiflask import APIBlueprint
api = APIBlueprint('api', __name__)
@api.get('/hello')
def hello():
result_one = f'我是您的专属接口提供服务器: {socket.gethostname()}'
result_two = f'我的机器参数如下'
result_three = f'操作系统:{platform.system()}CPU 核数:{psutil.cpu_count()},目前 CPU 占用率: {psutil.cpu_percent()}'
text = """
<style type="text/css">
* {
padding: 0;
margin: 0;
}
div {
padding: 4px 48px;
}
a {
color: black;
cursor: pointer;
text-decoration: none
}
a:hover {
text-decoration: None;
}
body {
background: #fff;
font-family:
"Century Gothic", "Microsoft yahei";
color: #333;
font-size: 18px;
}
h1 {
font-size: 100px;
font-weight: normal;
margin-bottom: 12px;
}
p {
line-height: 1.6em;
font-size: 42px
}
</style>
<div style="padding: 24px 48px;">
<p>
<a href="" target="_Blank">您好PZX 大人!</a>
<br />
<span style="font-size:30px">
<a href=""> """ + result_one + """</a>
</span>
<br />
<span style="font-size:25px">
<a href=""> """ + result_two + """</a>
</span>
<br />
<span style="font-size:20px">
<a href=""> """ + result_three + """</a>
<br />
<a href=""> """ + result_three + """</a>
<br />
<a href=""> """ + f'总内存 {psutil.virtual_memory().total / 1024 / 1024},使用中内存:{round(psutil.virtual_memory().used / 1024 / 1024, 2)}' + """</a>
<br />
<a href=""> """ + f'磁盘总空间 {round(psutil.disk_usage("/").total / 1024 / 1024 / 1024, 2)},磁盘使用情况:{round(psutil.disk_usage("/").used / 1024 / 1024 / 1024, 2)}' + """</a>
</span>
</p>
</div>
"""
return text

View File

View File

51
app/api/v1/schema/api.py Normal file
View File

@@ -0,0 +1,51 @@
from apiflask import Schema
from apiflask.fields import Integer, String, Boolean, Nested, List
class LoginIn(Schema):
username = String(required=True)
password = String(required=True)
tenantId = String(required=True)
uuid = String()
code = String()
class LoginNestedOut(Schema):
token = String()
class LoginOut(Schema):
code = Integer()
msg = String()
data = Nested(LoginNestedOut)
class CaptchaImageNestedOut(Schema):
captchaEnabled = Boolean()
img = String()
uuid = String()
class CaptchaImageOut(Schema):
code = Integer()
msg = String()
data = Nested(CaptchaImageNestedOut)
class TenantOut(Schema):
companyName = String()
domain = String()
tenantId = String()
class TenantsNestedOut(Schema):
tenantEnabled = Boolean()
voList = List(Nested(TenantOut))
class TenantsOut(Schema):
code = Integer()
msg = String()
data = Nested(TenantsNestedOut)

0
app/config/__init__.py Normal file
View File

11
app/config/config.py Normal file
View File

@@ -0,0 +1,11 @@
import os
from chassis.config import Config as _Config
class Config(_Config):
"""
配置
"""
RABBITMQ_URI = os.getenv('RABBITMQ_URI')

0
app/util/__init__.py Normal file
View File

15
app/util/common.py Normal file
View File

@@ -0,0 +1,15 @@
import os
from itertools import groupby
from operator import itemgetter
def split_group(dict_list, key):
dict_list.sort(key=itemgetter(key))
tmps = groupby(dict_list, itemgetter(key))
result = []
for key, group in tmps:
result.append({key: list(group)})
return result
basedir = os.getcwd()