This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathserverapi.py
138 lines (105 loc) · 3.62 KB
/
serverapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import uvicorn
import fastapi
import pymongo
import json
import hashlib
import datetime
import os
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from fastapi import FastAPI, Depends, HTTPException
from pydantic import BaseModel, Field
#Set Path
path_config = "bin/config.json"
path_idcard = "bin/api/idcard"
path_slipbank = "bin/api/slipbank"
with open(path_config,"r",encoding="utf8") as conf :
d_config = json.loads(conf.read())
#Create APP
app = FastAPI(
docs_url=None,
redoc_url=None,
secret_key="Fwbh2n4jrQQAqw8PVMv5dQgN4UQTRTwUkecsnQ8g7sLGstqxa6KSfMDvzkB5UbWe"
)
#Set Class
class requestsdataidcard(BaseModel) :
uuid : str = Field(...,min_length=1)
class requestsdataslipbank(BaseModel) :
uuid : str = Field(...,min_length=1)
uuid_book : str = Field(...,min_length=1)
@app.post("/idcardimage", include_in_schema=False)
def idcardimage(data: requestsdataidcard=None) :
if not data is None :
c = __checkreq(uuid=data.uuid)
if not c is None :
if os.path.isfile(f"{path_idcard}/{hashlib.md5(c['_id'].__str__().encode()).hexdigest()}.jpg") :
return FileResponse(f"{path_idcard}/{hashlib.md5(c['_id'].__str__().encode()).hexdigest()}.jpg")
raise HTTPException(status_code=404,detail={"code" : "ERR_NOTFOUND"})
raise HTTPException(status_code=401,detail={"code" : "ERR_AUTHFAIL"})
@app.post("/slipbankimage", include_in_schema=False)
def slipbankimage(data: requestsdataslipbank=None) :
if not data is None :
c = __checkreq(uuid=data.uuid)
if not c is None :
if os.path.isfile(f"{path_slipbank}/{data.uuid_book}.jpg") :
return FileResponse(f"{path_slipbank}/{data.uuid_book}.jpg")
raise HTTPException(status_code=404,detail={"code" : "ERR_NOTFOUND"})
raise HTTPException(status_code=401,detail={"code" : "ERR_AUTHFAIL"})
@app.post("/checkidcard", include_in_schema=False)
def checkidcard(data: requestsdataidcard=None) :
if not data is None :
c = __checkreq(uuid=data.uuid)
if not c is None :
if os.path.isfile(f"{path_idcard}/{hashlib.md5(c['_id'].__str__().encode()).hexdigest()}.jpg") :
return {"code" : True}
return {"code" : False}
raise HTTPException(status_code=401,detail={"code" : "ERR_AUTHFAIL"})
@app.post("/getuuid", include_in_schema=False)
def getid(data: requestsdataidcard=None) :
if not data is None :
c = __checkreq(uuid=data.uuid)
if not c is None :
return {"code" : "%s" % hashlib.md5(c['_id'].__str__().encode()).hexdigest()}
#Function
def __connectdb() :
try :
conn = pymongo.MongoClient(__getconnectmongodb(),serverSelectionTimeoutMS=5000)
conn.server_info()
return conn
except Exception as e :
raise HTTPException(status_code=500,detail={"code" : "ERR_CONNECTFAILED"})
def __getconfigmongodb() :
global path_config
with open(path_config,"r",encoding="utf8") as conf :
d = json.loads(conf.read())
res = {
"db" : d["mongodb"]["database"]
}
return res
def __getconnectmongodb() :
global path_config
with open(path_config,"r",encoding="utf8") as conf :
d = json.loads(conf.read())
if d["mongodb"]["srv"] is True :
res = "mongodb+srv://%s:%s@%s/%s" % (
d["mongodb"]["username"],
d["mongodb"]["password"],
d["mongodb"]["host"],
d["mongodb"]["database_auth"]
)
else :
res = "mongodb://%s:%s@%s:%s/%s" % (
d["mongodb"]["username"],
d["mongodb"]["password"],
d["mongodb"]["host"],
d["mongodb"]["port"],
d["mongodb"]["database_auth"]
)
return res
def __checkreq(uuid) :
conn = __connectdb()
return conn[__getconfigmongodb()["db"]]["user_members"].find_one({
"uuid" : "%s" % uuid
})
if __name__ == "__main__" :
uvicorn.run(app,host="127.0.0.1",port=8085)