# main.py
# pip install sanic[ext]
# pip install sanic_session
import os,time
os.environ['TZ'] = 'Asia/Phnom_Penh'
time.tzset()
from sanic import Sanic
from config import Config
app = Sanic('Multinews')
app.static("/static", "./static")
app.config.update(Config.config)
from models.connectdb import Database
app.ctx.mydb = Database.mydb
from sanic_session import Session
Session(app)
from routes.front import index
from routes.front import login
# routes/front/login.py
from sanic import Sanic
from controllers.front.login import Login
app = Sanic.get_app('Multinews')
instance = Login()
@app.get("/login")
async def getItem(req):
return await instance.getItem(req)
@app.post("/login")
async def postItem(req):
return await instance.postItem(req)
# controllers/front/login.py
# pip install bcrypt
from sanic import Sanic
from sanic_ext import render
from copy import deepcopy
import bcrypt,uuid
class Login():
def __init__(self):
app = Sanic.get_app('Multinews')
self.config = deepcopy(app.config)
self.mydb = app.ctx.mydb
async def getItem(self,req):
self.config["pageTitle"] = 'ទំព័រចុះឈ្មោះចូលក្នុង'
self.config['route'] = '/login'
await self.createRootUser()
return await render("base.html", context={"data":self.config})
async def createRootUser(self):
password = b"xxxxxxxxxxxxxxx"
hashedPassword = bcrypt.hashpw(password, bcrypt.gensalt())
user = {
"id": uuid.uuid4().hex,
"email": "root@khmerweb.app",
"username": "Sokhavuth",
"password": hashedPassword,
"role":"Admin",
"thumb":"",
"info":"",
"video":"",
"date":""
}
self.mydb['users'].insert_one(user)