mirror of
https://github.com/devproje/balance-application.git
synced 2024-10-20 15:11:21 +00:00
feat: middle save (dockerfile)
This commit is contained in:
parent
6f8fef42ac
commit
8e2518e4de
4 changed files with 80 additions and 74 deletions
6
Dockerfile
Normal file
6
Dockerfile
Normal file
|
@ -0,0 +1,6 @@
|
|||
FROM python:3-alpine3.20
|
||||
|
||||
WORKDIR /opt/server
|
||||
|
||||
COPY . .
|
||||
RUN pip install -r requirements.txt
|
24
app.py
24
app.py
|
@ -1,9 +1,29 @@
|
|||
import psycopg2
|
||||
from generate import on_load
|
||||
from fastapi import FastAPI, Response
|
||||
from routes.auth import router as auth
|
||||
from contextlib import asynccontextmanager
|
||||
from util.config import conn_param, db_url
|
||||
from routes.balance import router as balance
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
app = FastAPI()
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
conn = psycopg2.connect(conn_param)
|
||||
cur = conn.cursor()
|
||||
|
||||
try:
|
||||
print("loading database for: %s" % db_url())
|
||||
on_load(conn, cur)
|
||||
except:
|
||||
print("[warn] error occurred while creating table. aborted")
|
||||
finally:
|
||||
cur.close()
|
||||
conn.close()
|
||||
|
||||
yield
|
||||
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
|
@ -14,7 +34,7 @@ app.add_middleware(
|
|||
)
|
||||
|
||||
@app.get("/")
|
||||
def index(resp: Response):
|
||||
async def index(resp: Response):
|
||||
resp.headers.setdefault("Content-Type", "text")
|
||||
return "Hello, World!"
|
||||
|
||||
|
|
121
generate.py
121
generate.py
|
@ -1,8 +1,6 @@
|
|||
import psycopg2
|
||||
import random, string
|
||||
from getpass import getpass
|
||||
from util.auth_lib import hash
|
||||
from util.config import conn_param
|
||||
from service.auth_service import AuthData, AuthService
|
||||
|
||||
def gen_salt(length = 20):
|
||||
|
@ -18,77 +16,56 @@ def _gen_token():
|
|||
|
||||
sec.close()
|
||||
|
||||
def __main__():
|
||||
conn = psycopg2.connect(conn_param)
|
||||
cur = conn.cursor()
|
||||
def _new_account():
|
||||
name = input("input your display name: ")
|
||||
username = input("input your username: ")
|
||||
password = getpass("input your password: ")
|
||||
passchk = getpass("type password one more time: ")
|
||||
salt = gen_salt()
|
||||
|
||||
try:
|
||||
f = open("./load.txt", "r")
|
||||
_gen_token()
|
||||
if f.read().split("=")[1] == "false":
|
||||
raise ValueError("value not true")
|
||||
if password != passchk:
|
||||
return
|
||||
|
||||
print("server already initialized")
|
||||
f.close()
|
||||
except:
|
||||
cur.execute(
|
||||
"""
|
||||
create table if not exists account(
|
||||
name varchar(25),
|
||||
username varchar(25) not null,
|
||||
password varchar(100) not null,
|
||||
salt varchar(50),
|
||||
primary key(username)
|
||||
);
|
||||
"""
|
||||
)
|
||||
hashed_password = hash(password, salt)
|
||||
packed = AuthData(
|
||||
name=name,
|
||||
username=username,
|
||||
password=hashed_password,
|
||||
salt=salt
|
||||
)
|
||||
|
||||
cur.execute(
|
||||
"""
|
||||
create table if not exists balset(
|
||||
id serial primary key,
|
||||
uid varchar(25) not null,
|
||||
name varchar(50),
|
||||
date bigint,
|
||||
price bigint,
|
||||
buy boolean,
|
||||
memo varchar(300),
|
||||
constraint FK_Account_ID
|
||||
foreign key (uid)
|
||||
references account(username)
|
||||
on delete CASCADE
|
||||
);
|
||||
"""
|
||||
)
|
||||
service = AuthService()
|
||||
service.create(data=packed)
|
||||
|
||||
conn.commit()
|
||||
|
||||
cur.close()
|
||||
conn.close()
|
||||
|
||||
name = input("input your display name: ")
|
||||
username = input("input your username: ")
|
||||
password = getpass("input your password: ")
|
||||
passchk = getpass("type password one more time: ")
|
||||
salt = gen_salt()
|
||||
|
||||
if password != passchk:
|
||||
return
|
||||
|
||||
hashed_password = hash(password, salt)
|
||||
packed = AuthData(
|
||||
name=name,
|
||||
username=username,
|
||||
password=hashed_password,
|
||||
salt=salt
|
||||
)
|
||||
|
||||
service = AuthService()
|
||||
service.create(data=packed)
|
||||
|
||||
f = open("load.txt", "w")
|
||||
f.write("init=true")
|
||||
|
||||
f.close()
|
||||
|
||||
__main__()
|
||||
def on_load(conn, cur):
|
||||
_gen_token()
|
||||
cur.execute(
|
||||
"""
|
||||
create table account(
|
||||
name varchar(25),
|
||||
username varchar(25) not null,
|
||||
password varchar(100) not null,
|
||||
salt varchar(50),
|
||||
primary key(username)
|
||||
);
|
||||
"""
|
||||
)
|
||||
cur.execute(
|
||||
"""
|
||||
create table balset(
|
||||
id serial primary key,
|
||||
uid varchar(25) not null,
|
||||
name varchar(50),
|
||||
date bigint,
|
||||
price bigint,
|
||||
buy boolean,
|
||||
memo varchar(300),
|
||||
constraint FK_Account_ID
|
||||
foreign key (uid)
|
||||
references account(username)
|
||||
on delete CASCADE
|
||||
);
|
||||
"""
|
||||
)
|
||||
conn.commit()
|
||||
_new_account()
|
||||
|
|
|
@ -11,6 +11,9 @@ def _load_secret():
|
|||
|
||||
return tok
|
||||
|
||||
def db_url():
|
||||
return os.getenv("DB_URL")
|
||||
|
||||
conn_param = "host=%s port=%s dbname=%s user=%s password=%s" % (
|
||||
os.getenv("DB_URL"),
|
||||
os.getenv("DB_PORT"),
|
||||
|
|
Loading…
Reference in a new issue