def create_app_swagger(config_name): conf = config[config_name] app_swg = connexion.App(__name__, port=conf.PORT, debug=conf.DEBUG, specification_dir='../swagger/') yaml_host = app_swg.add_api('./v1_0/employeemng.yaml', arguments={'title': 'api v1.0'}) app = app_swg.app CORS(app) app.config.from_object(conf) config[config_name].init_app(app) # from app.model import AuthUser # auth.user_class = AuthUser # 如果服務不需要註冊到kong,需修改host的設定 if app.config['KONG_AUTO_REGISTER']: from mwsdk import Kong kong = Kong() yaml_host.specification['host'] = '%s:%s' % (kong.ip, kong.port) else: from mwsdk import consul_agent_conf yaml_host.specification['host'] = '%s:%s' % (consul_agent_conf.bind_ip, app.config['PORT']) # 需要增加handler 这样docker 中才能看到log formatter = logging.Formatter( '%(asctime)s %(name)-12s %(levelname)-8s %(message)s') consoleHandler = logging.StreamHandler() consoleHandler.setFormatter(formatter) app.logger.addHandler(consoleHandler) app.logger.level = app.config['LOG_LEVEL'] db.init_app(app) rds.init_app(app) auth.init_app(app) p.init_app(app) return app_swg
def register_service(config_name, app): from config import config # 注册本服务到kong config = config[config_name] auto_register2consul = config.CONSUL_AUTO_REGISTER auto_register2kong = config.KONG_AUTO_REGISTER web_port = config.PORT from mwsdk import consul_agent_conf service_host = '%s:%s' % (consul_agent_conf.bind_ip, web_port) if auto_register2kong: from mwsdk import Kong kong = Kong() kong.reg_service('employeemng/v1.0', service_host, auth='jwt') app.logger.info('注册kong成功') #kong.reg_service('xxxx/v1.0/login_jwt', service_host, auth='') #kong.reg_service('xxxx/v1.0/logout_jwt', service_host,auth='jwt') #kong.reg_service('static', service_host, auth='', kong_uris='/auth/static') # 註冊到kong的服務到consul if auto_register2consul: check = { "id": "employeemng api", "name": "employeemng on port %s" % web_port, "http": "http://%s/employeemng/v1.0/health" % service_host, "interval": "20s", "timeout": "10s", "DeregisterCriticalServiceAfter": "5m" } from mwsdk import Kong, reg_service kong = Kong() reg_service('employeemng', address=kong.ip, port=kong.port, tags=['kong', 'jwt'], check=check) app.logger.info('注册consul成功')
# 註冊到kong的服務到consul app.logger.info('auto_register2consul:%s' % auto_register2consul) if auto_register2consul: check = { "id": "test api", "name": "test on port %s" % web_port, "http": "http://%s/test/v1.0/health" % service_host, "interval": "20s", "timeout": "10s", "DeregisterCriticalServiceAfter": "5m" } from mwsdk import Kong, reg_service kong = Kong() reg_service('test', address=kong.ip, port=kong.port, tags=['kong', 'jwt'], check=check) app.logger.info('注册consul成功') if __name__ == '__main__': # create_dev_db() app = create_app_swagger('default') register_service('default', app.app) app.run(host='0.0.0.0') if app.config.get('CONSUL_AUTO_REGISTER'): from mwsdk import Kong, dereg_service dereg_service('test', Kong().port)
#kong.reg_service('xxxx/v1.0/login_jwt', service_host, auth='') #kong.reg_service('xxxx/v1.0/logout_jwt', service_host,auth='jwt') #kong.reg_service('static', service_host, auth='', kong_uris='/auth/static') # 註冊到kong的服務到consul if auto_register2consul: check = { "id": "employeemng api", "name": "employeemng on port %s" % web_port, "http": "http://%s/employeemng/v1.0/health" % service_host, "interval": "20s", "timeout": "10s", "DeregisterCriticalServiceAfter": "5m" } from mwsdk import Kong, reg_service kong = Kong() reg_service('employeemng', address=kong.ip, port=kong.port, tags=['kong', 'jwt'], check=check) app.logger.info('注册consul成功') if __name__ == '__main__': app = create_app_swagger('default') register_service('default', app.app) app.run(host='0.0.0.0') if app.config.get('CONSUL_AUTO_REGISTER'): from mwsdk import Kong, dereg_service dereg_service('employeemng', Kong().port)