Skip to content

Commit

Permalink
add --sql-server -ss suppport
Browse files Browse the repository at this point in the history
  • Loading branch information
tsbxmw committed Apr 9, 2019
1 parent cda8d5a commit 1e492e6
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 25 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@

> https://github.com/tsbxmw/haf
## now have version 0.0.3 to support mysql as an api server

## how to run

```
python3 -m hafweb run -ss=root:root@localhost:3306/haf_publish
```

## now support mysql as an api server

> apis
Expand Down
8 changes: 1 addition & 7 deletions hafweb/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,10 @@

RESOURCE_PATH = f"{os.path.dirname(__file__)}/resource"

DB_MYSQL_HOST = "192.168.41.208"
DB_MYSQL_PORT = 3306
DB_MYSQL_USER = "root"
DB_MYSQL_PASSWORD = "testzhan123"
DB_MYSQL_NAME = "haf_publish"

Base = declarative_base()

MAIN_VERSION = 0
SUB_VERSION = 0
FIX_VERSION = 5
FIX_VERSION = 6

VERSION = f"{MAIN_VERSION}.{SUB_VERSION}.{FIX_VERSION}"
20 changes: 10 additions & 10 deletions hafweb/controller/controller_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ def __init__(self):
@classmethod
def bind_all(cls):
apicase = ApiCase()
apicase.metadata.bind = engine
apicase.metadata.bind = engine_maker.engine
case_expect = ApiCaseExpect()
case_expect.metadata.bind = engine
case_expect.metadata.bind = engine_maker.engine
case_ids = ApiCaseIds()
case_ids.metadata.bind = engine
case_ids.metadata.bind = engine_maker.engine
case_request = ApiCaseRequest()
case_request.metadata.bind = engine
case_request.metadata.bind = engine_maker.engine
case_response = ApiCaseResponse()
case_response.metadata.bind = engine
case_response.metadata.bind = engine_maker.engine
case_sqlinfo = ApiCaseSqlinfo()
case_sqlinfo.metadata.bind = engine
case_sqlinfo.metadata.bind = engine_maker.engine
case_sqlinfo_checklsit = ApiCaseSqlinfoChecklist()
case_sqlinfo_checklsit.metadata.bind = engine
case_sqlinfo_checklsit.metadata.bind = engine_maker.engine
case_sqlinfo_config = ApiCaseSqlinfoConfig()
case_sqlinfo_config.metadata.bind = engine
case_sqlinfo_config.metadata.bind = engine_maker.engine
case_sqlinfo_script = ApiCaseSqlinfoScript()
case_sqlinfo_script.metadata.bind = engine
case_sqlinfo_script.metadata.bind = engine_maker.engine
case_detail = ApiDetail()
case_detail.metadata.bind = engine
case_detail.metadata.bind = engine_maker.engine

@classmethod
def get_case_all(cls):
Expand Down
6 changes: 3 additions & 3 deletions hafweb/controller/controller_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def __init__(self):
@classmethod
def bind_all(cls):
main = Main()
main.metadata.bind = engine
main.metadata.bind = engine_maker.engine
suite = Suite()
suite.metadata.bind = engine
suite.metadata.bind = engine_maker.engine
summary = Summary()
summary.metadata.bind = engine
summary.metadata.bind = engine_maker.engine

@classmethod
def get_main_all(cls):
Expand Down
20 changes: 16 additions & 4 deletions hafweb/controller/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,28 @@
from hafweb.config import *
import functools
from contextlib import contextmanager
from haf.common.sigleton import *

class EngineMaker(metaclass=SingletonType):
engine = None
maker = None

def __init__(self):
super().__init__()

def bind_sql_server(self, args):
self.engine = create_engine(
f"mysql+pymysql://{args.sql_server}")
self.maker = sessionmaker(bind=self.engine)


engine_maker = EngineMaker()

engine = create_engine(
f"mysql+pymysql://{DB_MYSQL_USER}:{DB_MYSQL_PASSWORD}@{DB_MYSQL_HOST}:{DB_MYSQL_PORT}/{DB_MYSQL_NAME}")
maker = sessionmaker(bind=engine)

@contextmanager
def session_close():
try:
session = maker()
session = engine_maker.maker()
yield session
finally:
session.close()
3 changes: 3 additions & 0 deletions hafweb/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ def main():
help="run web, using `python -m hafweb run` to run the web server ")
sub_run_arg_program.add_argument("--port", "-p", dest="port", type=int, default="8081",
help="the port of web server")
sub_run_arg_program.add_argument("--sql-server", "-ss", dest="sql_server", type=str, default="", required=True,
help="db sql server : user:pass@host:port/db")
args = arg_program.parse_args()
engine_maker.bind_sql_server(args)
Controller.bind_all()
ControllerApi.bind_all()
if hasattr(args, 'port'):
Expand Down

0 comments on commit 1e492e6

Please sign in to comment.