Example #1
0
 def registerUser(self, request, context):
     now = time.strftime("%Y-%m-%d %X")
     mobile = self.parameter["mobile"]
     password = self.parameter.get("password", mobile[:-6])
     username = self.parameter.get("username", "")
     company = self.parameter.get("company", "")
     uid = DealEncrypt.hash_md5_encrypt(mobile)
     _user = DemoUser.get_or_none(uid=uid)
     if _user:
         return ReturnData(CODE_202, {})
     insert_dict = {
         "uid": uid,
         "username": username,
         "mobile": mobile,
         "phone": mobile,
         "password": UserUtil.generate_password(password),
         "registration_time": now,
         "company": company,
     }
     DemoUser.insert(insert_dict).execute()
     user = DemoUser.get_or_none(uid=uid)
     if not user:
         return ReturnData(CODE_204, msg="注册失败")
     data = model_to_dict(user)
     data["registration_time"] = Common.format_datetime(
         data["registration_time"])
     data["last_login_time"] = Common.format_datetime(
         data["last_login_time"])
     return ReturnData(CODE_1, data)
Example #2
0
    def userProfile(self, request, context):
        uid = self.parameter["uid"]
        user = DemoUser.get_or_none(uid=uid)
        if not user:
            return ReturnData(CODE_204, {}, msg="用户不存在")
        data = model_to_dict(user)

        data["registration_time"] = Common.format_datetime(
            data["registration_time"])
        data["last_login_time"] = Common.format_datetime(
            data["last_login_time"])
        return ReturnData(CODE_1, data)
Example #3
0
def send_message(self, return_data):
    return_data.pop("data", "")
    env = self.settings.get("env")
    if env == "prod" and self.request.path not in Constant.NO_RECORD_URI:
        start_time = self.parameter["request_time"]
        return_time = datetime_now()
        cost_time = round((return_time - start_time).total_seconds() * 1000.0,
                          3)
        message_info, log_info = {}, {}
        collection = str_now("%Y%m")
        self.parameter["request_time"] = Common.format_datetime(
            self.parameter["request_time"])
        log_info["channel"] = self.settings.get("channel", "")
        log_info["headers"] = self.request.headers._dict
        log_info["routing"] = self.parameter.get("routing", "")
        log_info["path"] = self.request.path
        log_info["parameter"] = self.parameter
        log_info["return_data"] = return_data
        log_info["start_time"] = start_time.strftime("%Y-%m-%d %X.%f")
        log_info["return_time"] = return_time.strftime("%Y-%m-%d %X.%f")
        log_info["cost_time"] = cost_time
        log_info["trace"] = return_data["trace"]
        message_info["collection"] = collection
        message_info["log_info"] = log_info
        try:
            self.kafka_producer.log_send(value=message_info)
        except Exception as e:
            logging.warning(traceback.format_exc())
            logging.warning(f"send log to kafka error, {e}")
Example #4
0
 def _to_dict(self, display_fields, format_datetime=True):
     result = defaultdict()
     for fields in display_fields:
         value = getattr(self, fields)
         if format_datetime and isinstance(
                 value, (datetime.datetime, datetime.date)):
             value = Common.format_datetime(value)
         result.setdefault(fields, value)
     return result
Example #5
0
 def userLogin(self, request, context):
     mobile = self.parameter["mobile"]
     password = self.parameter["password"]
     user = DemoUser.get_or_none(DemoUser.mobile == mobile)
     if not user:
         return ReturnData(CODE_203, {})
     if user.status != CODE_1:
         return ReturnData(CODE_210, {})
     if not UserUtil.check_password(password, user.password):
         return ReturnData(CODE_204, {})
     data = model_to_dict(user)
     data["registration_time"] = Common.format_datetime(
         data["registration_time"])
     data["last_login_time"] = Common.format_datetime(
         data["last_login_time"])
     for k, v in data.items():
         if v == None:
             data[k] = ""
     return ReturnData(CODE_1, data)
Example #6
0
 def __init__(self):
     config = Common.get_config_value("mongodb")
     self.host = config["host"]
     self.port = config["port"]
     self.auth_db = config.get("auth_db", None)
     self.user = config.get("user", None)
     if config.get("password", None):
         self.password = DealEncrypt.crypto_decrypt(config["password"])
     else:
         self.password = None
     self.uri = config.get("uri", None)
     self.database = config.get("database", None)
     self.client = self.connect_mongodb()
 def upload_img(self, img_data, bucketName=None, suffix='jpg'):
     file_name = self.generate_uuid()
     file_name = ".".join([file_name, suffix])
     if not bucketName:
         bucketName = "pay_service/img/" + file_name
     else:
         bucketName = "".join([bucketName, os.sep, file_name])
     action_flag = HWObsClient().streamupload(bucketName, img_data)
     img_url = Common.get_config_value("cdn-service")["img_cloud_url"]
     img_url = img_url + '/' + bucketName
     if not action_flag:
         return 0, "上传失败"
     return 1, img_url
Example #8
0
 def connect_mysql():
     if not RetryConnectMysql._instance:
         config = Common.get_config_value("mysql")
         RetryConnectMysql._instance = RetryConnectMysql(
             database=config["database"],
             max_connections=int(config['max_connections']),
             stale_timeout=int(config['timeout']),
             timeout=int(config['timeout']),
             host=config['host'],
             user=config['user'],
             password=DealEncrypt.crypto_decrypt(config["password"]),
             port=int(config['port']))
     return RetryConnectMysql._instance
Example #9
0
 def __init__(self):
     config_dict = Common.get_config_value('redis')
     self.kwags = {
         "host": config_dict["host"],
         "port": config_dict["port"],
         "db": config_dict["db"],
         "decode_responses": True
     }
     if config_dict.get("password"):
         self.kwags["password"] = DealEncrypt.crypto_decrypt(
             config_dict["password"])
     self.retry = int(config_dict["retry"])
     self.client = self.connect_redis()
Example #10
0
 def __init__(self,
              code=1,
              data=None,
              msg=None,
              decimal=False,
              trace="",
              request_id="",
              **kwargs):
     self.code = code
     self.message = msg if msg else EN_CODE[code]
     self.data = Common.format_decimal(data) if decimal else data
     self.kwargs = kwargs
     self.trace = trace
     self.request_id = request_id
 def make_producer(self):
     kafka_config = Common.yaml_config("kafka_cluster")
     connect_config = {}
     connect_config["key_serializer"] = lambda v: ujson.dumps(v).encode(
         'utf-8')
     connect_config["value_serializer"] = lambda v: ujson.dumps(v).encode(
         'utf-8')
     connect_config["max_block_ms"] = 15000
     if all([
             kafka_config["sasl_plain_username"],
             kafka_config["sasl_plain_password"]
     ]):
         connect_config.update(kafka_config)
     else:
         connect_config.update(
             bootstrap_servers=kafka_config["bootstrap_servers"])
     while True:
         producer = KafkaProducer(**connect_config)
         if not producer.bootstrap_connected():
             logging.debug("will retry connect kafka")
             continue
         logging.debug(f"connect kafka cluster "
                       f"{kafka_config['bootstrap_servers']} successful")
         return producer
Example #12
0
    def init_base(self, loop=None):
        if not loop:
            self.loop = asyncio.get_event_loop()
        if self._init:
            return
        config = Common.yaml_config()
        redis_config = config["redis"]
        mongo_config = config["mongo"]
        mysql_config = config["mysql"]

        async_client = AsyncClientSession()
        mysql_client = AsyncMySQLConnect.init_db(mysql_config)
        mysql_manager = AsyncManager(database=mysql_client)

        mongo_client = AsyncMongodbConnect(mongo_config).client
        redis_client = AsyncRedis(redis_config)
        self.loop.run_until_complete(redis_client.init_db())
        self.loop.run_until_complete(async_client.init_session())
        self.async_client = async_client
        self.redis = redis_client.client
        self.mongo = mongo_client
        self.mysql = mysql_manager
        self.kafka_producer = KafkaEntryPoint()
        ControllerBase._init = True
Example #13
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# @Time    : 2019/12/19 下午4:28
# @Author  : Hanley
# @File    : base.py
# @Desc    : 数据模型
import datetime
from collections import defaultdict

from peewee import *

from commons.common import Common
from utils.async_db  import AsyncMySQLConnect

mysql_config = Common.yaml_config("mysql")
mysql_client = AsyncMySQLConnect.init_db(mysql_config)


class _BaseModel(Model):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def _to_dict(self, display_fields, format_datetime=True):
        result = defaultdict()
        for fields in display_fields:
            value = getattr(self, fields)
            if format_datetime and isinstance(
                    value, (datetime.datetime, datetime.date)):
                value = Common.format_datetime(value)
Example #14
0
from __future__ import absolute_import

import os

from celery import platforms

from commons.common import Common

rbd_config = Common.yaml_config('redis')
host = rbd_config["host"]
port = rbd_config["port"]
if rbd_config.get("password"):
    password = rbd_config["password"]
    REDIS_URL = 'redis://:{2}@{0}:{1}'.format(host, port, password)
else:
    REDIS_URL = 'redis://{0}:{1}'.format(host, port)

mq_config = Common.yaml_config('rabbitmq')
BROKER_URL = 'pyamqp://{0}:{1}@{2}:{3}/{4}'.format(mq_config["user"],
                                                   mq_config["password"],
                                                   mq_config["host"],
                                                   mq_config["port"],
                                                   mq_config["vhost"])
# BROKER_URL = 'pyamqp://*****:*****@localhost:5672/test'
CELERY_RESULT_BACKEND = '{0}/{1}'.format(REDIS_URL, rbd_config["db"])

platforms.C_FORCE_ROOT = True
CELERY_ENABLE_UTC = True
CELERY_TIMEZONE = 'Asia/Shanghai'
CELERYD_FORCE_EXECV = True
CELERY_TASK_SERIALIZER = 'pickle'
Example #15
0
# @Time    : 2019/12/19 下午4:34
# @Author  : Hanley
# @File    : server.py
# @Desc    : 项目启动文件

from tornado import httpserver, ioloop
from tornado.options import define, options

define("port", default=9376, type=int)
define("env", default="local", type=str)
define("channel", default="demo", type=str)
options.logging = None
options.parse_command_line()
from commons.common import Common

Common.init_config_file(options.env)
from commons.constant import Constant
from commons.initlog import logging
from controller.base import ControllerBase
from scripts.init_tables import sync_uri
from urls.url import handlers_loads
from middelware.core import BaseApp


def make_settings(options) -> dict:
    controller = ControllerBase()
    return {
        "cookie_secret": "p4Qy1mcwQJiSOAytobquL3YDYuXDkkcYobmUWsaBuoo",
        'xsrf_cookies': False,
        'debug': False,
        "gzip": True,
Example #16
0
 def mysql(self) -> PooledMySQLDatabase:
     mysql_config = Common.yaml_config("mysql")
     return MysqlConnect.init_db(mysql_config)
Example #17
0
 def redis(self) -> Redis:
     redis_config = Common.yaml_config("redis")
     return RedisConnect(redis_config).client
Example #18
0
 def mongo(self) -> MongoClient:
     mongo_config = Common.yaml_config("mongo")
     return MongodbConnect(mongo_config).client