diff --git a/app/api/mobile/api.rb b/app/api/mobile/api.rb index 918c5d73c..4add19248 100644 --- a/app/api/mobile/api.rb +++ b/app/api/mobile/api.rb @@ -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 diff --git a/app/api/mobile/apis/auth.rb b/app/api/mobile/apis/auth.rb index acd5fe1e2..8281c4aa9 100644 --- a/app/api/mobile/apis/auth.rb +++ b/app/api/mobile/apis/auth.rb @@ -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 diff --git a/app/api/mobile/apis/courses.rb b/app/api/mobile/apis/courses.rb index 9409fddd7..2dca9c220 100644 --- a/app/api/mobile/apis/courses.rb +++ b/app/api/mobile/apis/courses.rb @@ -1,5 +1,7 @@ module Mobile + module Apis + module Entities class Course < Grape::Entity @@ -67,5 +69,6 @@ module Mobile end end + end end diff --git a/app/api/mobile/apis/users.rb b/app/api/mobile/apis/users.rb index 489b3e419..e4544a09e 100644 --- a/app/api/mobile/apis/users.rb +++ b/app/api/mobile/apis/users.rb @@ -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 diff --git a/config/initializers/autoreload_grape.rb b/config/initializers/autoreload_grape.rb new file mode 100644 index 000000000..ceffb9425 --- /dev/null +++ b/config/initializers/autoreload_grape.rb @@ -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 + +