5

免奔溃核酸码系统2-单体应用代码实现

 2 years ago
source link: http://blog.ilibrary.me/2022/05/03/%E5%85%8D%E5%A5%94%E6%BA%83%E6%A0%B8%E9%85%B8%E7%A0%81%E7%B3%BB%E7%BB%9F2-%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
欢迎转载,请支持原创,保留原文链接:blog.ilibrary.me

上一篇文章我们对核酸检测系统做了需求分析和技术选型,并完成了架构设计。

本文实现免奔溃核酸码系统, 先用rails把单体应用实现. 为了快速实现功能,会用一个开源的rails engine panda来实现.

panda是一个业务代码聚合引擎,Rails Engine. 所有功能开箱即用。常用的用户系统,缓存和任务系统, 消息推送, 短信验证码, 管理后台, etc. 欢迎大家提交pr。

基础框架搭建

  1. 生成rails project: rails new crash-free-hesuan
  2. 添加panda engine到Gemfile: gem 'panda', git: 'https://github.com/younthu/panda'
    1. 这个engine自带用户管理系统,注册、登陆、登出API
  3. 安装gem包: bundle install
  4. seeds.rb里面加入:
     Panda::AdminUser.create! email: '[email protected]', password: 'password'
     Panda::User.create! email: '[email protected]', mobile: '13712345678', password: '12345678'
    
  5. 初始化数据库: rails db:create db:migrate db:seed
  6. mount pand, 在routes.rb加入下面语句: ` mount Panda::Engine => “/panda”`
  7. 起服务器: rails s
  8. 可以进入管理后台看现有的功能http://localhost:3000/admin,
  9. 至此, 系统的用户系统搭建完毕
    1. 创建用户: /api/v1/users/create
    2. 登陆用户: ` /api/v1/users/sign_in`
    3. 获取个人信息: /api/v1/users/my_info

建模和API实现

  1. 用户建模可以用panda里面自带的,不用重新设计。
  2. 核酸码建模
    1. 核酸码里面主要包含身份证,姓名,核酸码生成日期和一个hash, hash由前面三个字段+一个key(这个key由服务器生成, 固定)通过md5生成. 这4样信息编码到二维码里面, 医护人员验码的时候可以扫描二维码离线核验了。
    2. 生成核酸码模型: rails g model code name id_number
    3. 二维码API, 这个API返回用户的name, id_number, 一个时间标签和一个hash值.
    4. 创建app/controllers/codes_controller.rb, 填入如下内容:
        class CodesController < Panda::Api::BaseController
            def show
                user = current_user
                time = DateTime.now
                md5 = Digest::MD5.new
                hash = md5.update(user.name + user.id_number + time)
                render json: {name: user.name, id_number: user.id_number, time: time, hash: hash}
            end
        end
      
  3. 核酸检测分组上传建模:
    1. 每个分组包含小区信息,分组时间,分组包含哪10个居民,分组的试管编号, 测试结果
    2. 为了降低服务器压力,分组不在服务器预生成,由医护端先生成本地分组数据,等检测信息收集完毕以后一次性上传分组测试信息.
    3. 生成核酸检测分组模型: rails g model group test_id community tested_at:datetime ids:jsonb status
    4. 分组上传API, 该API接受参数test_id,community, tested_at, ids, 分别表示试管id, 哪个小区, 什么时候测试的,该组里面所有接受检测的人的身份证id.
      1. 该API的数据不直接入库,先进入kafka, 然后入库.
    5. 创建app/controllers/groups_controller.rb, 填入下面内容:
        class GroupsController < Panda::Api::BaseController
        def create
            user = current_user
      
            # TODO: 把下面的内容用kafka来削峰填谷
            code = Code.create!(test_id: params[:test_id], community: params[:community], tested_at: params[:tested_at], ids: params[:ids])
      
            render json: code
        end
        end
      
  4. 添加理由到routes.rb:
     scope :api do
         resource :code, only: [:show]
         resource :group, only: [:create]
     end
    
  5. 没了. 所有代码上传到crash-free-hesuan

接下来会做压力测试,然后上K8S系统。

欢迎加微信群讨论,扫下面的二维码拉入群, 请备注’核酸码’: wechat


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK