Commit 7df91c1b by liyijie

init project

parents
File added
# New Rails API Project Template
rails new blog -m http://example.com/template.rb
\ No newline at end of file
File added
REDIS_URL: redis://127.0.0.1:6379
SECRET_KEY_BASE =
DATABASE_ADAPTER = postgresql
DATABASE_POOL = 5
DATABASE_HOST = localhost
DATABASE_USERNAME = postgresql
DATABASE_PASSWORD =
DATABASE_NAME = app_name_to_replace
REDIS_URL: redis://redis:6379
DATABASE_HOST = postgresql
DATABASE_USERNAME = postgresql
DATABASE_PASSWORD = test
DATABASE_NAME = app_name_to_replace
REDIS_URL: redis://127.0.0.1:6379
SECRET_KEY_BASE =
DATABASE_HOST = localhost
DATABASE_USERNAME = root
DATABASE_PASSWORD =
DATABASE_NAME = app_name_to_replace
image: dsh0416/tallty-env:latest
cache:
paths:
- .bundle
services:
- mysql:8.0.2
- redis:latest
before_script:
- bundle install --path .bundle --retry 3
test:
variables:
RACK_ENV: test
REDIS_ENV: ci
MYSQL_DATABASE: mysql_test_database
MYSQL_ROOT_PASSWORD: test
script:
- bundle exec rake db:drop
- bundle exec rake db:create
- bundle exec rake db:migrate
- bundle exec rake spec
- bundle exec rake rubocop
require:
- rubocop-rails
- rubocop-performance
AllCops:
Exclude:
- 'bin/**/*'
- 'db/**/*'
DisabledByDefault: true
TargetRubyVersion: 2.4
REDIS_URL: redis://127.0.0.1:6379
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require_relative 'config/application'
Rails.application.load_tasks
task :rubocop do
sh 'rubocop'
end
task :scp do
sh 'scp ./swagger/v1/app_name_to_replace.json web@docs.tallty.com:/home/web/swagger-ui/dist/api/'
end
task :annotate do
sh 'annotate -i'
sh 'annotate -i --with-comment'
end
class ApplicationController < ActionController::API
def authenticated_auth
if respond_to?(:current_auth) && current_auth
current_auth
else
'Unknown'
end
end
rescue_from ActiveRecord::RecordNotFound do |e|
render json: { error: e.message }, status: 404
end
rescue_from ActiveRecord::RecordInvalid do |e|
render json: { error: e.message }, status: 422
end
end
Audited.current_user_method = :authenticated_auth
# Be sure to restart your server when you modify this file.
# Avoid CORS issues when API is called from the frontend app.
# Handle Cross-Origin Resource Sharing (CORS) in order to accept cross-origin AJAX requests.
# Read more: https://github.com/cyu/rack-cors
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*',
headers: :any,
methods: [:get, :post, :put, :patch, :delete, :options, :head]
end
end
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: <%= ENV["DATABASE_ADAPTER"] %>
pool: <%= ENV["DATABASE_POOL"] %>
encoding: <%= ENV["DATABASE_ENCODING"] %>
username: <%= ENV["DATABASE_USERNAME"] %>
password: <%= ENV["DATABASE_PASSWORD"] %>
host: <%= ENV["DATABASE_HOST"] %>
development:
<<: *default
database: <%= ENV["DATABASE_NAME"] %>
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: <%= ENV["DATABASE_NAME"] %>
production:
<<: *default
database: <%= ENV["DATABASE_NAME"] %>
set :stages, %w(test production)
set :default_stage, 'test'
require 'mina/rails'
require 'mina/git'
require 'mina/multistage'
# require 'mina/rbenv' # for rbenv support. (https://rbenv.org)
require 'mina/rvm' # for rvm support. (https://rvm.io)
require 'mina/bundler'
require 'mina/puma'
# require 'mina_sidekiq/tasks'
# Basic settings:
# domain - The hostname to SSH to.
# deploy_to - Path to deploy into.
# repository - Git repo to clone from. (needed by mina/git)
# branch - Branch name to deploy. (needed by mina/git)
set :application_name, 'app_name'
set :repository, 'https://github.com/app_name.git'
set :branch, 'master'
set :rails_env, 'production'
# Optional settings:
# set :user, 'foobar' # Username in the server to SSH to.
# set :port, '30000' # SSH port number.
# set :forward_agent, true # SSH forward_agent.
# Shared dirs and files will be symlinked into the app-folder by the 'deploy:link_shared_paths' step.
# Some plugins already add folders to shared_dirs like `mina/rails` add `public/assets`, `vendor/bundle` and many more
# run `mina -d` to see all folders and files already included in `shared_dirs` and `shared_files`
# set :shared_dirs, fetch(:shared_dirs, []).push('public/assets')
# set :shared_files, fetch(:shared_files, []).push('config/database.yml', 'config/secrets.yml')
set :shared_dirs, fetch(:shared_dirs, []).push('tmp/pids', 'tmp/sockets')
set :shared_files, fetch(:shared_files, []).push('.env.production.local')
# sidekiq
# set :sidekiq_pid, 'tmp/pids/sidekiq.pid'
# This task is the environment that is loaded for all remote run commands, such as
# `mina deploy` or `mina rake`.
task :remote_environment do
# If you're using rbenv, use this to load the rbenv environment.
# Be sure to commit your .ruby-version or .rbenv-version to your repository.
# invoke :'rbenv:load'
# For those using RVM, use this to load an RVM version@gemset.
invoke :'rvm:use', 'ruby-2.4.2'
end
# Put any custom commands you need to run at setup
# All paths in `shared_dirs` and `shared_paths` will be created on their own.
task :setup do
# command %{rbenv install 2.3.0 --skip-existing}
in_path(fetch(:shared_path)) do
command %{touch .env.production.local}
command %{mkdir -p tmp/pids}
command %{mkdir -p pids/sockets}
end
end
desc "Deploys the current version to the server."
task :deploy do
# uncomment this line to make sure you pushed your local branch to the remote origin
# invoke :'git:ensure_pushed'
deploy do
# Put things that will set up an empty directory into a fully set-up
# instance of your project.
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
invoke :'bundle:install'
command %{rails db:migrate RAILS_ENV=production}
# invoke :'rails[db:migrate]'
invoke :'deploy:cleanup'
on :launch do
# in_path(fetch(:current_path)) do
# command %{mkdir -p tmp/}
# command %{touch tmp/restart.txt}
# end
invoke :load
invoke :'puma:stop'
invoke :'puma:start'
# invoke :'sidekiq:restart'
end
end
# you can use `run :local` to run tasks on local machine before of after the deploy scripts
# run(:local){ say 'done' }
end
task :load do
workers = fetch(:workers, 2)
threads = fetch(:threads, 20)
command %{export WEB_CONCURRENCY=#{workers}}
command %{export RAILS_MAX_THREADS=#{threads}}
end
set :domain, 'www.example.com'
set :deploy_to, '/path'
set :user, 'user'
set :workers, 2
\ No newline at end of file
set :domain, 'www.example.com'
set :deploy_to, '/path'
set :user, 'user'
set :workers, 2
\ No newline at end of file
Ohm.redis = Redic.new(ENV['REDIS_URL'])
$redis = Redis.new(url: ENV['REDIS_URL'])
# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.
#
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 20 }
threads threads_count, threads_count
# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
port ENV.fetch("PORT") { 3000 }
# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "development" }
# Specifies the `pidfile` that Puma will use.
pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }
# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked webserver processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
workers ENV.fetch("WEB_CONCURRENCY") { 2 }
# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory.
#
# preload_app!
# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart
# RSpec
# spec/support/factory_bot.rb
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
require 'rspec/rails/swagger'
require 'rails_helper'
RSpec.configure do |config|
# Specify a root directory where the generated Swagger files will be saved.
config.swagger_root = Rails.root.to_s + '/swagger'
# Define one or more Swagger documents and global metadata for each.
config.swagger_docs = {
'v1/app_name_to_replace.json' => {
swagger: '2.0',
info: {
title: 'API V1',
version: 'v1'
},
schemes: ["http"],
host: 'app_name_to_replace.tallty.com',
}
}
end
def source_paths
Array(super) +
[File.expand_path(File.dirname(__FILE__))]
end
# Add needed gem to Gemfile
gsub_file 'Gemfile', 'https://rubygems.org', 'https://gems.ruby-china.com'
inject_into_file 'Gemfile', after: "source 'https://gems.ruby-china.com'" do <<-GEM_COMMENT
# A Ruby gem to load environment variables from `.env`.
# https://github.com/bkeepers/dotenv
# If you use gems that require environment variables to be set before they are loaded, then list dotenv-rails in the Gemfile before those other gems and require dotenv/rails-now.
gem 'dotenv-rails', require: 'dotenv/rails-now'
GEM_COMMENT
end
# gem 'sqlite3', '~> 1.3.6'
gem 'pg'
inject_into_file 'Gemfile', before: "gem 'pg" do <<-COMMENT
# Use PostgresSql as the database for deploy
COMMENT
end
# gem 'jbuilder'
# inject_into_file 'Gemfile', before: "gem 'jbuilder'" do <<-COMMENT
# # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# COMMENT
# end
#
# gem 'rack-cors'
# inject_into_file 'Gemfile', before: "gem 'rack-cors" do <<-COMMENT
# # Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
# COMMENT
# end
#
# gem 'redis'
gem 'uuid64'
inject_into_file 'Gemfile', before: "gem 'uuid64'" do <<-COMMENT
# This is a library generating unique id in short pattern. https://rubygems.org/gems/uuid64
# https://github.com/heckpsi-lab/uuid64
COMMENT
end
gem 'responders'
inject_into_file 'Gemfile', before: "gem 'responders'" do <<-COMMENT
# A set of Rails responders to dry up your application http://blog.plataformatec.com.br/
# https://github.com/plataformatec/responders
# rails g responders:install
COMMENT
end
gsub_file 'app/controllers/application_controller.rb', 'respond_to :html', 'respond_to :json'
gem 'will_paginate'
inject_into_file 'Gemfile', before: "gem 'will_paginate'" do <<-COMMENT
# Pagination library for Rails, Sinatra, Merb, DataMapper, and more http://github.com/mislav/will_paginat…
# https://github.com/mislav/will_paginate
COMMENT
end
gem 'aasm'
inject_into_file 'Gemfile', before: "gem 'aasm" do <<-COMMENT
# AASM - State machines for Ruby classes (plain Ruby, ActiveRecord, Mongoid)
# https://github.com/aasm/aasm
# rails generate aasm NAME [COLUMN_NAME]
COMMENT
end
# A Ruby static code analyzer, based on the community Ruby style guide. http://rubocop.readthedocs.io
# https://github.com/bbatsov/rubocop
gem 'rubocop', require: false
gem 'rubocop-rails'
gem 'rubocop-performance'
gem 'simple_controller', git: 'https://git.tallty.com/open-source/simple_controller.git'
gem 'acts_as_pasting', git: 'http://git.tallty.com/open-source/acts_as_pasting.git'
gem 'tallty_import_export', git: 'https://git.tallty.com/open-source/tallty_import_export.git'
gem 'acts_as_paranoid'
gem "audited"
gem 'pry-byebug'
# deploy
gem 'mina', require: false
gem 'mina-multistage', require: false
gem 'sprockets'
gem_group :development, :test do
gem 'rspec-rails'
gem 'shoulda-matchers'
gem 'factory_bot_rails'
gem 'rspec-rails-swagger', git: "https://git.tallty.com/open-source/rspec-rails-swagger.git"
gem 'simplecov', require: false
gem 'annotate'
gem "pry-rails"
end
# .ignore files
inject_into_file '.gitignore', after: ".byebug_history" do <<-COMMENT
# Ignore swagger files
/swagger/*
# Ignore simplecov coverage files
/coverage/*
# Ignore puma running files
/.pids/*
!/pids/.keep
# Other ignore files
dump.rdb
*.pem
# Ignore public folder
/public/*
# Ignore local files
*.local
.DS_Store
COMMENT
end
after_bundle do
run "spring stop"
# rspec-rails gem install
generate 'rspec:install'
# rspec-rails-swagger gem install
generate 'rspec:swagger_install'
inject_into_file 'Gemfile', before: "gem 'rspec-rails-swagger" do <<-COMMENT
# Generate Swagger 2.0 docs for Rails apps using RSpec request specs. Test results can be captured as response examples.
# https://github.com/drewish/rspec-rails-swagger
# rails generate rspec:swagger_install
# rails generate rspec:swagger PostsController
# bundle exec rake swagger
COMMENT
end
# Audited (formerly acts_as_audited) is an ORM extension that logs all changes to your Rails models.
generate 'audited:install'
# shoulda-matchers gem configuration
copy_file 'src/spec/support/shoulda_matches.rb', 'spec/support/shoulda_matches.rb'
# factory_bot_rails gem configuration
gsub_file 'spec/rails_helper.rb',
"# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }",
"Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }"
copy_file 'src/spec/support/factory_bot.rb', 'spec/support/factory_bot.rb'
# simplecov configuration
inject_into_file 'spec/rails_helper.rb', before: /^RSpec.configure/ do <<-COMMENT
require 'simplecov'
SimpleCov.start
COMMENT
end
# rubocop configuration
copy_file 'src/.rubocop.yml', '.rubocop.yml'
copy_file 'src/.env', '.env'
copy_file 'src/.env.test', '.env.test'
copy_file 'src/.env.test.local', '.env.test.local'
copy_file 'src/.rubocop.yml', '.rubocop.yml'
remove_file 'Rakefile'
copy_file 'src/Rakefile', 'Rakefile'
# ci configuration
copy_file 'src/.gitlab-ci.yml', '.gitlab-ci.yml'
gsub_file '.gitlab-ci.yml', 'mysql_test_database', "#{app_name}_test_db"
# puma configuration
remove_file 'config/puma.rb'
copy_file 'src/config/puma.rb', 'config/puma.rb'
remove_file 'config/database.yml'
copy_file 'src/config/database.yml', 'config/database.yml'
copy_file 'src/config/ohm.rb', 'config/initializers/ohm.rb'
remove_file 'config/initializers/cors.rb'
copy_file 'src/config/cors.rb', 'config/initializers/cors.rb'
remove_file 'spec/swagger_helper.rb'
copy_file 'src/spec/swagger_helper.rb', 'spec/swagger_helper.rb'
remove_file 'app/controllers/application_controller.rb'
copy_file 'src/app/controllers/application_controller.rb', 'app/controllers/application_controller.rb'
copy_file 'src/config/audited.rb', 'config/initializers/audited.rb'
gsub_file 'config/puma.rb', 'app_name_to_replace', app_name
gsub_file 'spec/swagger_helper.rb', 'app_name_to_replace', app_name
gsub_file 'Rakefile', 'app_name_to_replace', app_name
gsub_file '.env', 'app_name_to_replace', app_name
gsub_file '.env.test', 'app_name_to_replace', app_name
gsub_file '.env.test.local', 'app_name_to_replace', app_name
# deploy
copy_file 'src/config/deploy.rb', 'config/deploy.rb'
directory 'src/config/deploy', 'config/deploy'
# run 'mkdir .pids'
# run 'touch .pids/.keep'
run 'bundle binstubs rspec-core'
run 'bundle binstubs bundler --force'
git :init
git add: '.'
git commit: "-a -m 'Initial Rails API Project'"
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment