cxhy 6 місяців тому
батько
коміт
f87198efe5
3 змінених файлів з 30 додано та 3 видалено
  1. 3 1
      .gitignore
  2. 9 1
      main.py
  3. 18 1
      model.py

+ 3 - 1
.gitignore

@@ -8,4 +8,6 @@
 /yixue.log
 /.idea/dataSources.xml
 /.idea/sqldialects.xml
-/.idea/vcs.xml
+/.idea/vcs.xml
+
+*.pyc

+ 9 - 1
main.py

@@ -220,7 +220,8 @@ async def getSiZhuInfo(request: SiZhuInfoRequest):
 @app.post("/api/customLogin")
 async def customLogin(request: CustomLogin):
     logging.info("login")
-    dt = CustomUser.select().where(CustomUser.user == request.user, CustomUser.psd == request.psd).first()
+    dt = CustomUser.select().where(CustomUser.user == request.user,
+                                   CustomUser.psd == request.psd).first()
 
     if dt is not None:
         return {"msg": "ok", "name": dt.name, "sexy": dt.sexy}
@@ -312,3 +313,10 @@ async def test(request: Request):
                 content["db"] = "broke , reconnect"
             logging.info("reconnect database")
     return JSONResponse(content=content, headers=headers)
+
+
+@app.get("/api/test2")
+async def test2():
+    users = UserInfo.select().where(UserInfo.customer != None)
+    users = [__build_user_object(dt) for dt in users]
+    return {"message": "Hello World", "users": users}

+ 18 - 1
model.py

@@ -1,3 +1,4 @@
+import logging
 from peewee import *
 from playhouse.pool import PooledMySQLDatabase
 
@@ -10,13 +11,29 @@ database = PooledMySQLDatabase('yixue',
                                stale_timeout=300, )
 
 
+def get_db():
+    db = PooledMySQLDatabase('yixue',
+                             user='yixue',
+                             password='Lai123',
+                             host='test.db.cxhy.cn',
+                             port=3306,
+                             max_connections=32,
+                             stale_timeout=300,
+                             wait_timeout=300, )
+    logging.getLogger().setLevel(logging.INFO)
+    logging.info("get db")
+    print("get db 2222")
+
+    return db
+
+
 class UnknownField(object):
     def __init__(self, *_, **__): pass
 
 
 class BaseModel(Model):
     class Meta:
-        database = database
+        database = get_db()
 
 
 class AreaInfo(BaseModel):