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 fastapi import FastAPI, Response
|
||||||
from routes.auth import router as auth
|
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 routes.balance import router as balance
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
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(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
|
@ -14,7 +34,7 @@ app.add_middleware(
|
||||||
)
|
)
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
def index(resp: Response):
|
async def index(resp: Response):
|
||||||
resp.headers.setdefault("Content-Type", "text")
|
resp.headers.setdefault("Content-Type", "text")
|
||||||
return "Hello, World!"
|
return "Hello, World!"
|
||||||
|
|
||||||
|
|
121
generate.py
121
generate.py
|
@ -1,8 +1,6 @@
|
||||||
import psycopg2
|
|
||||||
import random, string
|
import random, string
|
||||||
from getpass import getpass
|
from getpass import getpass
|
||||||
from util.auth_lib import hash
|
from util.auth_lib import hash
|
||||||
from util.config import conn_param
|
|
||||||
from service.auth_service import AuthData, AuthService
|
from service.auth_service import AuthData, AuthService
|
||||||
|
|
||||||
def gen_salt(length = 20):
|
def gen_salt(length = 20):
|
||||||
|
@ -18,77 +16,56 @@ def _gen_token():
|
||||||
|
|
||||||
sec.close()
|
sec.close()
|
||||||
|
|
||||||
def __main__():
|
def _new_account():
|
||||||
conn = psycopg2.connect(conn_param)
|
name = input("input your display name: ")
|
||||||
cur = conn.cursor()
|
username = input("input your username: ")
|
||||||
|
password = getpass("input your password: ")
|
||||||
|
passchk = getpass("type password one more time: ")
|
||||||
|
salt = gen_salt()
|
||||||
|
|
||||||
try:
|
if password != passchk:
|
||||||
f = open("./load.txt", "r")
|
return
|
||||||
_gen_token()
|
|
||||||
if f.read().split("=")[1] == "false":
|
|
||||||
raise ValueError("value not true")
|
|
||||||
|
|
||||||
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)
|
|
||||||
);
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
|
|
||||||
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
|
|
||||||
);
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
|
|
||||||
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)
|
hashed_password = hash(password, salt)
|
||||||
packed = AuthData(
|
packed = AuthData(
|
||||||
name=name,
|
name=name,
|
||||||
username=username,
|
username=username,
|
||||||
password=hashed_password,
|
password=hashed_password,
|
||||||
salt=salt
|
salt=salt
|
||||||
)
|
)
|
||||||
|
|
||||||
service = AuthService()
|
service = AuthService()
|
||||||
service.create(data=packed)
|
service.create(data=packed)
|
||||||
|
|
||||||
f = open("load.txt", "w")
|
def on_load(conn, cur):
|
||||||
f.write("init=true")
|
_gen_token()
|
||||||
|
cur.execute(
|
||||||
f.close()
|
"""
|
||||||
|
create table account(
|
||||||
__main__()
|
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
|
return tok
|
||||||
|
|
||||||
|
def db_url():
|
||||||
|
return os.getenv("DB_URL")
|
||||||
|
|
||||||
conn_param = "host=%s port=%s dbname=%s user=%s password=%s" % (
|
conn_param = "host=%s port=%s dbname=%s user=%s password=%s" % (
|
||||||
os.getenv("DB_URL"),
|
os.getenv("DB_URL"),
|
||||||
os.getenv("DB_PORT"),
|
os.getenv("DB_PORT"),
|
||||||
|
|
Loading…
Reference in a new issue