Panda是一个业务代码聚合引擎,Rails Engine. 所有功能开箱即用。常用的用户系统,缓存和任务系统, 消息推送, 短信验证码, 管理后台, etc.
- Add gems
- Add
gem 'panda', git: 'https://github.com/younthu/panda.git'
to Gemfile - Add
gem 'annotate'
to development group. - Add
mount Panda::Engine => "/panda"
toroutes.rb
- Add
bundle install
rails g config:install
- copy
test/dummy/config/settings.yml
toconfig/settings.yml
- copy
rails g panda:install & rails g panda:initializer
rails db:create && rails db:migrate
- start server:
rails s
- start sidekiq(optional):
sidekiq
- start rpush(optional):
rpush start
- check app info:
rails app:about
Api, rest api controller可以继承Panda::Api::Controller, 这个api包含了rest api登录的基本功能.
安装迁移文件:
rails panda:install:migrations
- 遵循传统的Rails项目结构。
- 这是一个Rails Engine, 多了一个Dummy App,所以代码目录结构在细节上和传统Rails项目有区别。
- start rpush(optional):
rpush start
- Both two commands below work in mac, but, not in linux.
- send notification to a device:
rails rpush:p8push_to_device["你好","BF1F297A3B62D1517BB19D5D67BD0BC72D438F94F016DAD6E2B175AE01BFC38D"]
- send notification to device:
rails rpush:push_to_device["你好","BF1F297A3B62D1517BB19D5D67BD0BC72D438F94F016DAD6E2B175AE01BFC38D"]
- 升级rpush:
- 升级gem包:
bundle update rpush
- 重新生成migration files:
rpush init
- 升级gem包:
- Panda::AdminRole是给后台用户角色定义用的。目前没有对一般用户权限做支持,可以在后期根据需求来添加。
- run rails test:
rails test
- 注意fixture加载的方式:
panda_users(:one)
, 需要加namespacepanda_
- 注意fixture加载的方式:
- run dummy app in the root folder: 'rails s'.
- 魔法在
panda/bin/rails
里面.APP_PATH = File.expand_path('../test/dummy/config/application', __dir__)
- 魔法在
- postman scripts.
- 后台登录:
http://localhost:3000/admin
- 更新版本号:
lib/panda/version
- 调试panda的时候可以引用本地panda, 这样可以直接改panda的代码调试:
gem 'panda', path: '../panda'
- 更新Swagger Api Doc:
rspec --format Rswag::Specs::SwaggerFormatter --order defined
Overriding Models and Controller
Contribution directions go here.
The gem is available as open source under the terms of the MIT License.
- User
- User model
- Rest api for Avatar upload
- Identity model
- Profile model
- 账户注销
- 会员等级
- captcha
- 新用户邀请
- Login with devise
- 手机登录
- 微信登录
- 通过关注微信公众号来绑定账号
- 邮箱登录
- 账号密码登录
- 扫码登陆
- 苹果登录
- Feature Toggle
- 聊天
- 站内信
- 基于Secure_token的token管理和身份认证.
- 目前
secure_token
和devise
不能同时在controller
里面用,因为有命名冲突,current_user
和authenticate_user!
都冲突了。目前的解决办法是通过Panda.token_method == :secure_token
来决定是用secure_token还是devise
- 目前登录时两种token都会返回.
devise token
在auth_token
下面,secure_token
在secure_token
字段 - 切换token认证方式需要去panda.rb里面设置token_method, 用postman测试过myinfo api, secure_token是工作的.
- 目前
- Login with wechat
- User
- 后台用户权限管理
- Roles
- Permissions
- 角色配置页面, 分管理员和用户
- 权限配置页面, 分管理员和用
- wx_pay, WechatMiniService from Tekapic.
- config gem with settings files.
- 优惠券
- 常用可通用的功能写到subfolder里面以gem的形式加载.
- swagger
- Kaminari pagination
- Localization
- ElasticSearch
- Redis
- ELK Log
- Sidekiq
- Puma auto restart. 解决ruby内存泄漏到问题
- CORS
- 默认关闭CORS
[//]: # ( 2.
rails g panda:install
会生成config/cors/initilaizer.rb
)
- 默认关闭CORS
[//]: # ( 2.
- OAuth
- JWT
- RPush
- Log rotation
- ActiveAdmin
- 后台
- 朋友圈
1. [ ] 照片
1. [ ] 文字
- 点赞
- 评论
- 搜索
- 订阅
- Panda installer
- 添加自动加载路径
routes/*.rb
到config/application.rb
里面去 - [ ]
- 添加自动加载路径
- 基于数据库的配置
- config做基于文件的静态配置
- rails-settings-cached做基于数据库的动态配置管理,给运维人员用.
- 订单系统
- 支付系统
- 微信支付
- 支付宝支付
- 苹果支付
- spree商城
- sentry rails
- newrelic 免费版
- devise authentication in go
- MQTT
- Websocket
- Message Queue, kafka, 削峰填谷
- APIs in go
- nginx configuration
- APIs in python
- devise authentication in python
- 工作流引擎
- Rails Settings Cached
- 禁用panda的dashboard, settings里面禁用
- App版本控制
- 类似Redmine的插件系统
- 积分系统
- 虚拟货币系统
- 抽奖系统
- 调试
- 系统探针?
- 远程日志?
- 数据库数据记录自诊断
- 测试
- UT
- Postman
- Jmeter
- Docker
- Docker文件
- Docker-compose文件
- Docker-compose for dummy
- K8S Helm Chart
- 运维
- 拷贝ansible scripts from yoga
- 拷贝ansible scripts with panda:install
- 技术文档,gitbook.
- 用户基本的API request频率限制, 防止攻击.
- Docker-compose template generator
- Panda settings generator(config rb and settings yaml) 3. [ ] ansible scripts 4. [ ]
- Seeds generator
- Generator网页,点击直接生成目标内容的界面。
- API/rspec Generator from Gu
- 说明见
lib/generators/panda/readme.md
- 说明见
- 小程序generator
- React Native app generator
常用来讲,性能问题不可避免。性能问题需要通过微服务架构来解决,规划如下:
- rails做快速业务开发和数据库建模,常用的业务逻辑和代码就落到panda里面去。
- Rails在某些情况下会存在API并发上的问题,对某些性能敏感的API,用go或者java去处理. 也就是某些模块一个API会有两套实现,一套是Rails的,成熟以后用go或者java再实现一遍。
- 两套API挂不同的namespace下面,用nginx做路由转发。
- 两套API必然会遇到用户认证的问题,前期可以两边各自实现,到后期可以考虑用kong之类的网关工具做用户认证。
- 以后可以用这套模板代码批量做系统。
- 短期内(1到2年)只需要集中精力在第1点就可以了。
- 多用callbacks, 方便做定制化。
- Rails Engine和Rails Plugin的区别.
- 加载本地gem:
gem 'core', path: '../core'
- Rails is the biggest engine.
- Thredded, Thredded is a Rails 4.2+ forum/messageboard engine. Its goal is to be as simple and feature rich as possible.
- Spree,Spree is a complete open source e-commerce solution built with Ruby on Rails.
- Devise,Flexible authentication solution for Rails with Warden.
- Start with rails engine
- A Guide to Rails Engines in the Wild: Real World Examples of Rails Engines in Action
- Rails 引擎初探
-
默认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
-
ActiveAdmin routes放engine namespace下面会导致资源加载的问题,解决办法是把routes放
Rails.application.routes.draw do
下面, 具体细节查看routes/api.rb
. -
panda.gemspec里面添加
spec.add_development_dependency
会导致依赖项在目标项目中不会被安装。用spec.add_dependency
则不会有这个问题。 -
自定义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
- 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
- panda engine做了自动加载engine migration files的配置, 所以不需要手动
-
sqlite可以存jsonb, 查看yoga.user
-
每改一行engine里面的代码,dummy的routes就会奔溃,得重启
rails s
-
rails在debug模式下会渲染exception in html.
- 解决办法: 在basecontroller.rb里面加
rescue_from ActiveRecord::RecordInvalid
, - 打印exception in json:
config.debug_exception_response_format = :api
- https://www.wyeworks.com/blog/2016/01/12/improvements-to-error-responses-in-rails-5-api-mode/
- 解决办法: 在basecontroller.rb里面加
继承Panda::Api:BaseController以后就会默认使用带分页功能的layout, 有几种办法可以避免用panda带的layout
- 在API里面通过layout指令来指定模版:
layout 'panda/layouts/application'
, 具体参考Panda::Api::BaseController
. - 在controller里指定不使用layout:
layout false
, 具体参考Panda::Api::BaseController
. - render的时候不使用layout:
render :index, layout: false