diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..612eccc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ +FROM python:3-alpine3.20 + +WORKDIR /opt/server + +COPY . . +RUN pip install -r requirements.txt diff --git a/app.py b/app.py index 1bd4397..246eed5 100644 --- a/app.py +++ b/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!" diff --git a/generate.py b/generate.py index b35ccfd..6c164a9 100644 --- a/generate.py +++ b/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") - - 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 + if password != passchk: + return - hashed_password = hash(password, salt) - packed = AuthData( - name=name, - username=username, - password=hashed_password, - salt=salt - ) + hashed_password = hash(password, salt) + packed = AuthData( + name=name, + username=username, + password=hashed_password, + salt=salt + ) - service = AuthService() - service.create(data=packed) + 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() diff --git a/util/config.py b/util/config.py index 19f0e5f..ecd25ee 100644 --- a/util/config.py +++ b/util/config.py @@ -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"),