def check(self, request, *args, **kwargs): """ 服务自检 :param request: :return: """ # 判断 eureka 注册中心是否启动 port_ret = check_tcp('127.0.0.1', '8001') if not port_ret: return JsonResponse( {'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'msg': 'Eureka Micro service Not started'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # 判断config 微服务是否启动 port_ret = check_tcp('127.0.0.1', '8002') if not port_ret: return JsonResponse( {'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'msg': 'Config Micro service Not started'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # 判断获取配置信息 res_dict = get_config() if not res_dict: return JsonResponse({'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'msg': 'Failed to get configuration'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # 判断mysql连接 # 获取mysql连接信息 host = res_dict['data']['mysql'].get('host') user = res_dict['data']['mysql'].get('username') password = res_dict['data']['mysql'].get('password') port = res_dict['data']['mysql'].get('port') # 判断mysql端口 if not check_tcp(host, port): return JsonResponse({'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'msg': 'MySQL port cannot connect'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # 查询表记录 sql = "show databases" print(sql) mysql_obj = ExecutionSql(host, user, password, port) select_ret = mysql_obj.select(sql) if not select_ret: return JsonResponse( {'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'msg': 'Failed to detect MySQL connection'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # 返回 http 200 return JsonResponse({'status': status.HTTP_200_OK, 'data': []}, status=status.HTTP_200_OK)
def login(self, request, *args, **kwargs): """ 登录 :param request: :param args: :param kwargs: :return: """ # 获取配置信息 res_dict = get_config() if not res_dict: return JsonResponse({'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'msg': 'Failed to get configuration'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # 获取表单数据 user_post = request.POST.get("username") pwd_post = request.POST.get("password") print("post",user_post, pwd_post) # 获取mysql连接信息 host = res_dict['data']['mysql'].get('host') user = res_dict['data']['mysql'].get('username') password = res_dict['data']['mysql'].get('password') port = res_dict['data']['mysql'].get('port') print("host",host,port) # 判断mysql端口 if not check_tcp(host,port): return JsonResponse({'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'msg': 'MySQL port cannot connect'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) print("查询表记录。。。。") # 查询表记录 sql = "select username,password from usercenter.users where username='******' and password=md5('%s')" % ( user_post, pwd_post) print(sql) mysql_obj = ExecutionSql(host, user, password, port) auth_ret = mysql_obj.select(sql) # 判断执行结果 if not auth_ret: # 返回 http 401 return JsonResponse({'status': status.HTTP_401_UNAUTHORIZED, 'msg': 'Authentication failure'}, status=status.HTTP_401_UNAUTHORIZED) # 更新登录时间 last_time = time.strftime('%Y-%m-%d %H:%M:%S') # UPDATE table_name SET field1=new-value1, field2=new-value2 sql = "update usercenter.users set last_time='%s' where username='******'" % (last_time, user_post) print(sql) update_ret = mysql_obj.update(sql) if not update_ret: return JsonResponse({'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'msg': 'Failed to update login time'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # 登录成功 # 返回 http 200 return JsonResponse({'status': status.HTTP_200_OK, 'data': []}, status=status.HTTP_200_OK)
def check(self, request, *args, **kwargs): """ 服务自检 :param request: :return: """ # 判断 eureka 注册中心是否启动 port_ret = check_tcp('svc-eureka.default.svc.cluster.local', '8001') if not port_ret: return JsonResponse( { 'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'msg': 'Eureka Micro service Not started' }, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # 判断获取配置信息 response = self.info(request) res = (response.content).decode('utf-8') res_dict = json.loads(res) # print("res_dict",res_dict) if not res_dict: return JsonResponse( { 'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'msg': 'Failed to get configuration' }, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # 判断mysql连接 # 获取mysql连接信息 host = res_dict['data']['mysql'].get('host') user = res_dict['data']['mysql'].get('username') password = res_dict['data']['mysql'].get('password') port = res_dict['data']['mysql'].get('port') # 查询表记录 sql = "show databases" print(sql) mysql_obj = ExecutionSql(host, user, password, port) select_ret = mysql_obj.select(sql) if not select_ret: return JsonResponse( { 'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'msg': 'Failed to detect MySQL connection' }, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # 返回 http 200 return JsonResponse({ 'status': status.HTTP_200_OK, 'data': [] }, status=status.HTTP_200_OK)
def check(request): """ 检测几个微服务端口是否正常 :param request: :return: """ response = server(request) # print(response.content) res = (response.content).decode('utf-8') server_dict = json.loads(res) if not isinstance(server_dict, dict): return JsonResponse( { 'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'msg': 'Failed to get microservice information' }, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # print("server_dict",server_dict) # 状态字典 ret_dict = {} # 遍历 for i in server_dict['data']: # 默认状态为0 if not ret_dict.get(i): ret_dict[i] = 0 # 检测tcp端口是否正常 #print("server_dict[i]",server_dict['data'][i]['ip']) port_ret = check_tcp(server_dict['data'][i]['ip'], server_dict['data'][i]['port']) if port_ret: # 更新状态 ret_dict[i] = 1 #print("ret_dict",ret_dict) # 返回 http 200 return JsonResponse({ 'status': status.HTTP_200_OK, 'data': ret_dict }, status=status.HTTP_200_OK)
def auth_login(self, request, *args, **kwargs): """ 用户认证 :param request: :param args: :param kwargs: :return: """ # print('http://127.0.0.1:8003/api-auth/login/') # return redirect('http://127.0.0.1:8003/api-auth/login/') user_post = request.POST.get("username") pwd_post = request.POST.get("password") data = { 'username': user_post, 'password': pwd_post, } auth_ip = "svc-auth.default.svc.cluster.local" auth_port = 8003 if not check_tcp(auth_ip, auth_port): return JsonResponse( { 'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'msg': 'Authentication microservice port unreachable' }, status=status.HTTP_500_INTERNAL_SERVER_ERROR) url = 'http://%s:%s/api-auth/login/' % (auth_ip, auth_port) response = requests.post(url, data=data, timeout=2) res = (response.content).decode('utf-8') res_dict = json.loads(res) if not isinstance(res_dict, dict): # print("错误,获取api: {} 数据失败".format(url), "red") return JsonResponse( { 'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'msg': 'Data returned by auth api is abnormal' }, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return JsonResponse(res_dict)
def info(self, request, *args, **kwargs): """ 用户信息 :param request: :param args: :param kwargs: :return: """ # 获取配置信息 res_dict = get_config() if not res_dict: return JsonResponse( { 'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'msg': 'Failed to get configuration' }, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # 获取表单数据 user_post = request.POST.get("username") # pwd_post = request.POST.get("password") # print(user_post, pwd_post) # 获取mysql连接信息 host = res_dict['data']['mysql'].get('host') user = res_dict['data']['mysql'].get('username') password = res_dict['data']['mysql'].get('password') port = res_dict['data']['mysql'].get('port') # 判断mysql端口 if not check_tcp(host, port): return JsonResponse( { 'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'msg': 'MySQL port cannot connect' }, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # 查询表记录 sql = "select * from usercenter.users where username='******'" % ( user_post) print(sql) mysql_obj = ExecutionSql(host, user, password, port) user_info = mysql_obj.select(sql) # 判断执行结果 if not user_info: # 返回 http 500 return JsonResponse( { 'status': status.HTTP_500_INTERNAL_SERVER_ERROR, 'msg': 'Failed to query user information' }, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # 返回 http 200 return JsonResponse({ 'status': status.HTTP_200_OK, 'data': [user_info] }, status=status.HTTP_200_OK)