diff --git a/sql_app/main.py b/sql_app/main.py index e0230b2..3a939c1 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -1,7 +1,9 @@ -from fastapi import Depends, FastAPI, HTTPException, status +from fastapi import Depends, FastAPI, HTTPException, status, Request from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm, OAuth2AuthorizationCodeBearer from fastapi.security.api_key import APIKey from fastapi.responses import PlainTextResponse +from fastapi.templating import Jinja2Templates +from fastapi.staticfiles import StaticFiles from sqlalchemy.orm import Session from . import crud, models, schemas, auth_helper, init_db @@ -16,11 +18,16 @@ models.Base.metadata.create_all(bind=engine) oauth = OAuth2PasswordBearer(tokenUrl="tkn") app = FastAPI(title="IoT Building System") +app.mount("/sql_app/static", StaticFiles(directory="sql_app/static"), name="static") +templates = Jinja2Templates(directory="sql_app/templates") # Split into endpoints modules #app.include_router(users.router,prefix="/users", tags=["User"]) init_db.init() +@app.get("/") +def home(request: Request): + return templates.TemplateResponse("home.html", context={"request": request}) def get_current_user(token: str = Depends(oauth), db: Session = Depends(get_db)): credentials_exception = HTTPException( diff --git a/sql_app/static/main.css b/sql_app/static/main.css new file mode 100644 index 0000000..35b5553 --- /dev/null +++ b/sql_app/static/main.css @@ -0,0 +1,20 @@ +/* custom css */ + +html { + position: relative; + min-height: 100%; +} + +body { + padding-top: 2rem; + margin-bottom: 60px; +} + +.footer { + position: absolute; + bottom: 0; + width: 100%; + height: 50px; + line-height: 50px; + } + \ No newline at end of file diff --git a/sql_app/static/main.js b/sql_app/static/main.js new file mode 100644 index 0000000..496587a --- /dev/null +++ b/sql_app/static/main.js @@ -0,0 +1,36 @@ + +(function () { + console.log("Sanity Check!"); +})() + +var token = null; +var logged_in = false; + +function handleLogInClick() { + + var username = document.getElementById("username_box").value; + var password = document.getElementById("password_box").value; + console.log("Username ", username); + + fetch('/users/tkn', { + method: 'POST', + headers: { + 'Content-type': 'application/x-www-form-urlencoded', + 'accept': 'application/json' + }, + body: `grant_type=&username=${username}&password=${password}&scope=&client_id=&client_secret=` + }) + .then(response => { + if (!response) { + throw new Error("HTTP error " + response.status); + } + return response.json(); + }) + .then(json => { + console.log(json); + token = json['access_token']; + console.log(token); + if (token) { logged_in = true; } + }) + +} diff --git a/sql_app/templates/_base.html b/sql_app/templates/_base.html new file mode 100644 index 0000000..76251b5 --- /dev/null +++ b/sql_app/templates/_base.html @@ -0,0 +1,32 @@ + + + +
+ +Login
+ID | +Status | +Result | +
---|