安裝Flask跟套件
pip install flask
pip install flask-restful
雖然使用sqlite時應該不會有連線問題,不過在這邊還是簡單寫一個測試執行SQL的Route,如果執行成功顯示Database ONLINE
將伺服器掛載在http的port 8080上,在/dbstatus以GET方式接收請求
# Server.py
import util.dbcc as dbcc
app = flask.Flask(__name__)
api = Api(app)
@app.route("/dbstatus", methods=['GET'])
def HelloWorld():
if(dbcc.quy_dbonline(conn)):
return "Database ONLINE"
else:
return "Database OFFLINE"
if __name__ == '__main__':
env = ConfigParser()
env.read('env.ini')
try:
conn = db.connect(env['SQL']['sqlite_URL'], check_same_thread=False)
print(f"load database from {env['SQL']['sqlite_URL']} successfully")
app.run(port = 8080, debug=True)
except Exception as err:
print(err)
後續將以Server.py作為整個專案的中控主程式,對接API