Skip to content

younthu/panda

Repository files navigation

Panda

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

Quick start

  1. Add gems
    1. Add gem 'panda', git: 'https://github.com/younthu/panda.git' to Gemfile
    2. Add gem 'annotate' to development group.
    3. Add mount Panda::Engine => "/panda" to routes.rb
  2. bundle install
  3. rails g config:install
    1. copy test/dummy/config/settings.yml to config/settings.yml
  4. rails g panda:install & rails g panda:initializer
  5. rails db:create && rails db:migrate
  6. start server: rails s
  7. start sidekiq(optional): sidekiq
  8. start rpush(optional): rpush start
  9. check app info: rails app:about

Setup

Api, rest api controller可以继承Panda::Api::Controller, 这个api包含了rest api登录的基本功能.

安装迁移文件:

rails panda:install:migrations

目录结构

  1. 遵循传统的Rails项目结构。
  2. 这是一个Rails Engine, 多了一个Dummy App,所以代码目录结构在细节上和传统Rails项目有区别。

Settings

./config/readme.md

Features

Rpush

  1. start rpush(optional): rpush start
    1. Both two commands below work in mac, but, not in linux.
    2. send notification to a device: rails rpush:p8push_to_device["你好","BF1F297A3B62D1517BB19D5D67BD0BC72D438F94F016DAD6E2B175AE01BFC38D"]
    3. send notification to device: rails rpush:push_to_device["你好","BF1F297A3B62D1517BB19D5D67BD0BC72D438F94F016DAD6E2B175AE01BFC38D"]
  2. 升级rpush:
    1. 升级gem包: bundle update rpush
    2. 重新生成migration files: rpush init

微信支付

权限管理

  1. Panda::AdminRole是给后台用户角色定义用的。目前没有对一般用户权限做支持,可以在后期根据需求来添加。

Swagger

Test

  1. run rails test: rails test
    1. 注意fixture加载的方式: panda_users(:one), 需要加namespace panda_
  2. run dummy app in the root folder: 'rails s'.
    1. 魔法在panda/bin/rails里面. APP_PATH = File.expand_path('../test/dummy/config/application', __dir__)
  3. postman scripts.
  4. 后台登录: http://localhost:3000/admin

Development

  1. 更新版本号: lib/panda/version
  2. 调试panda的时候可以引用本地panda, 这样可以直接改panda的代码调试: gem 'panda', path: '../panda'
  3. 更新Swagger Api Doc: rspec --format Rswag::Specs::SwaggerFormatter --order defined

Rails tasks

Overriding Models and Controller

Overriding Models and Controller

Contributing

Contribution directions go here.

License

The gem is available as open source under the terms of the MIT License.

Roadmap

  1. User
    1. User model
    2. Rest api for Avatar upload
    3. Identity model
    4. Profile model
    5. 账户注销
    6. 会员等级
    7. captcha
    8. 新用户邀请
  2. Login with devise
    1. 手机登录
    2. 微信登录
    3. 通过关注微信公众号来绑定账号
    4. 邮箱登录
    5. 账号密码登录
    6. 扫码登陆
    7. 苹果登录
  3. Feature Toggle
  4. 聊天
  5. 站内信
  6. 基于Secure_token的token管理和身份认证.
    1. 目前secure_tokendevise不能同时在controller里面用,因为有命名冲突,current_userauthenticate_user!都冲突了。目前的解决办法是通过Panda.token_method == :secure_token来决定是用secure_token还是devise
    2. 目前登录时两种token都会返回. devise tokenauth_token下面, secure_tokensecure_token字段
    3. 切换token认证方式需要去panda.rb里面设置token_method, 用postman测试过myinfo api, secure_token是工作的.
  7. Login with wechat
  8. User
  9. 后台用户权限管理
    1. Roles
    2. Permissions
    3. 角色配置页面, 分管理员和用户
    4. 权限配置页面, 分管理员和用
  10. wx_pay, WechatMiniService from Tekapic.
  11. config gem with settings files.
  12. 优惠券
  13. 常用可通用的功能写到subfolder里面以gem的形式加载.
  14. swagger
  15. Kaminari pagination
  16. Localization
  17. ElasticSearch
  18. Redis
  19. ELK Log
  20. Sidekiq
  21. Puma auto restart. 解决ruby内存泄漏到问题
  22. CORS
    1. 默认关闭CORS [//]: # ( 2. rails g panda:install 会生成config/cors/initilaizer.rb)
  23. OAuth
  24. JWT
  25. RPush
  26. Log rotation
  27. ActiveAdmin
    1. 后台
  28. 朋友圈 1. [ ] 照片 1. [ ] 文字
    1. 点赞
    2. 评论
    3. 搜索
    4. 订阅
  29. Panda installer
    1. 添加自动加载路径routes/*.rbconfig/application.rb里面去
    2. [ ]
  30. 基于数据库的配置
    1. config做基于文件的静态配置
    2. rails-settings-cached做基于数据库的动态配置管理,给运维人员用.
  31. 订单系统
  32. 支付系统
    1. 微信支付
    2. 支付宝支付
    3. 苹果支付
  33. spree商城
  34. sentry rails
  35. newrelic 免费版
  36. devise authentication in go
  37. MQTT
  38. Websocket
  39. Message Queue, kafka, 削峰填谷
  40. APIs in go
  41. nginx configuration
  42. APIs in python
  43. devise authentication in python
  44. 工作流引擎
  45. Rails Settings Cached
  46. 禁用panda的dashboard, settings里面禁用
  47. App版本控制
  48. 类似Redmine的插件系统
  49. 积分系统
  50. 虚拟货币系统
  51. 抽奖系统
  52. 调试
    1. 系统探针?
    2. 远程日志?
    3. 数据库数据记录自诊断
  53. 测试
    1. UT
    2. Postman
    3. Jmeter
  54. Docker
    1. Docker文件
    2. Docker-compose文件
    3. Docker-compose for dummy
    4. K8S Helm Chart
  55. 运维
    1. 拷贝ansible scripts from yoga
    2. 拷贝ansible scripts with panda:install
  56. 技术文档,gitbook.
  57. 用户基本的API request频率限制, 防止攻击.

Rails generators

  1. Docker-compose template generator
  2. Panda settings generator(config rb and settings yaml) 3. [ ] ansible scripts 4. [ ]
  3. Seeds generator
  4. Generator网页,点击直接生成目标内容的界面。
  5. API/rspec Generator from Gu
    1. 说明见lib/generators/panda/readme.md
  6. 小程序generator
  7. React Native app generator

性能问题

常用来讲,性能问题不可避免。性能问题需要通过微服务架构来解决,规划如下:

  1. rails做快速业务开发和数据库建模,常用的业务逻辑和代码就落到panda里面去。
  2. Rails在某些情况下会存在API并发上的问题,对某些性能敏感的API,用go或者java去处理. 也就是某些模块一个API会有两套实现,一套是Rails的,成熟以后用go或者java再实现一遍。
  3. 两套API挂不同的namespace下面,用nginx做路由转发。
  4. 两套API必然会遇到用户认证的问题,前期可以两边各自实现,到后期可以考虑用kong之类的网关工具做用户认证。
  5. 以后可以用这套模板代码批量做系统。
  6. 短期内(1到2年)只需要集中精力在第1点就可以了。

设计原则

  1. 多用callbacks, 方便做定制化。

Notes

  1. Rails Engine和Rails Plugin的区别.
  2. 加载本地gem: gem 'core', path: '../core'

Famous engine

  1. Rails is the biggest engine.
  2. Thredded, Thredded is a Rails 4.2+ forum/messageboard engine. Its goal is to be as simple and feature rich as possible.
  3. Spree,Spree is a complete open source e-commerce solution built with Ruby on Rails.
  4. Devise,Flexible authentication solution for Rails with Warden.

Famous Rails app(open sourced)

  1. Discourse

Useful gems

Useful links

  1. Start with rails engine
  2. A Guide to Rails Engines in the Wild: Real World Examples of Rails Engines in Action
  3. Rails 引擎初探

notes

  1. 默认Engine不会加载gemspec里面的内容,导致uninitialized constant DeviseTokenAuth错误. 解决办法:

    # 把下面的内容加入到lib/panda.rb顶部, https://stackoverflow.com/questions/5159607/rails-engine-gems-dependencies-how-to-load-them-into-the-application
    # requires all dependencies
     Gem.loaded_specs['yourengine'].dependencies.each do |d|
     require d.name
     end
  2. ActiveAdmin routes放engine namespace下面会导致资源加载的问题,解决办法是把routes放Rails.application.routes.draw do下面, 具体细节查看routes/api.rb.

  3. panda.gemspec里面添加spec.add_development_dependency会导致依赖项在目标项目中不会被安装。用spec.add_dependency则不会有这个问题。

  4. 自定义migration files加载路径:

    # ./test/test_helper.rb
    ActiveRecord::Migrator.migrations_paths = [File.expand_path("../test/dummy/db/migrate", __dir__)]

    Rails Engine会根据变量ENGINE_ROOT自动添加migrate path:

    #activerecord/lib/active_record/railtie.rb:42,
         task :load_config do
           if defined?(ENGINE_ROOT) && engine = Rails::Engine.find(ENGINE_ROOT)
             if engine.paths["db/migrate"].existent
               ActiveRecord::Tasks::DatabaseTasks.migrations_paths += engine.paths["db/migrate"].to_a
             end
           end
         end
    1. panda engine做了自动加载engine migration files的配置, 所以不需要手动rails panda:install:migrations:
      # lib/panda/engine.rb
      config.paths["db/migrate"].expanded.each do |expanded_path|
          app.config.paths["db/migrate"] << expanded_path
       end
  5. sqlite可以存jsonb, 查看yoga.user

  6. 每改一行engine里面的代码,dummy的routes就会奔溃,得重启rails s

  7. rails在debug模式下会渲染exception in html.

    1. 解决办法: 在basecontroller.rb里面加rescue_from ActiveRecord::RecordInvalid,
    2. 打印exception in json: config.debug_exception_response_format = :api
    3. https://www.wyeworks.com/blog/2016/01/12/improvements-to-error-responses-in-rails-5-api-mode/

更改json的渲染layout

继承Panda::Api:BaseController以后就会默认使用带分页功能的layout, 有几种办法可以避免用panda带的layout

  1. 在API里面通过layout指令来指定模版: layout 'panda/layouts/application', 具体参考 Panda::Api::BaseController.
  2. 在controller里指定不使用layout: layout false, 具体参考 Panda::Api::BaseController.
  3. render的时候不使用layout: render :index, layout: false

mount activeadmin inside engine

  1. Define a resource inside an engine
  2. Example Engines