auto reload api folder in development mode.

This commit is contained in:
guange 2014-12-09 15:16:47 +08:00
parent ee3d998873
commit 41d9af1ab9
5 changed files with 58 additions and 25 deletions

View File

@ -1,11 +1,5 @@
module Mobile
$LOAD_PATH << File.expand_path('..',__FILE__)
autoload :Auth, 'apis/auth'
autoload :Users, 'apis/users'
autoload :Courses, 'apis/courses'
class API < Grape::API
version 'v1', using: :path
format :json
@ -30,9 +24,9 @@ module Mobile
end
end
mount Auth
mount Users
mount Courses
mount Apis::Auth
mount Apis::Users
mount Apis::Courses
end
end

View File

@ -1,20 +1,22 @@
module Mobile
class Auth < Grape::API
resource :auth do
desc "Creates and returns access_token if valid login"
params do
requires :login, type: String, desc: 'Username or email'
requires :password, type: String, desc: 'Password'
end
post :login do
user,last_logon = ::User.try_to_login(params[:login], params[:password])
if user
::ApiKey.delete_all(user_id: user.id)
key = ::ApiKey.create!(user_id: user.id)
data = {token: key.access_token}.merge(user.as_json).merge(user.extensions.as_json)
{status: 0, data: data}
else
{status: 1, message: 'Unauthorized.'}
module Apis
class Auth < Grape::API
resource :auth do
desc "Creates and returns access_token if valid login"
params do
requires :login, type: String, desc: 'Username or email'
requires :password, type: String, desc: 'Password'
end
post :login do
user,last_logon = ::User.try_to_login(params[:login], params[:password])
if user
::ApiKey.delete_all(user_id: user.id)
key = ::ApiKey.create!(user_id: user.id)
data = {token: key.access_token}.merge(user.as_json).merge(user.extensions.as_json)
{status: 0, data: data}
else
{status: 1, message: 'Unauthorized.'}
end
end
end
end

View File

@ -1,5 +1,7 @@
module Mobile
module Apis
module Entities
class Course < Grape::Entity
@ -67,5 +69,6 @@ module Mobile
end
end
end
end

View File

@ -1,4 +1,5 @@
module Mobile
module Apis
class Users < Grape::API
resource :users do
desc "get all users"
@ -23,8 +24,15 @@ module Mobile
{status: 1, message: e.message}
end
end
desc "test upload"
post "upload" do
puts "haha11"
end
end
end
end
end

View File

@ -0,0 +1,26 @@
if Rails.env.development?
lib_ruby_files = Dir.glob(File.join("app/api/**", "*.rb"))
lib_reloader ||= ActiveSupport::FileUpdateChecker.new(lib_ruby_files) do
# lib_ruby_files.each do |lib_file|
# puts "start require #{lib_file}"
# require_dependency(lib_file)
# end
#
# binding.pry
# if Object.const_defined?(:Mobile)
# Object.send(:remove_const, :Mobile)
# end
#
# $".delete_if {|s| s.include?('api/mobile') }
# require File.join(Rails.root,"app/api/mobile/api.rb")
Rails.application.reload_routes!
end
ActionDispatch::Callbacks.to_prepare do
lib_reloader.execute_if_updated
end
end