require 'sinatra/base' module Adminix::Setup class Routes < Sinatra::Base get '/' do render('root') end get '/login' do render('login') end post '/login' do result = Services.create_session(params[:session]) case result[0] when :ok redirect "/create-service?t=#{result[1]}" when :error errors = result[1] render('login', session: params[:session], errors: errors) end end get '/sign-up' do render('sign_up') end post '/sign-up' do result = Services.create_user(params[:user]) case result[0] when :ok redirect "/create-service?t=#{result[1]}" when :error errors = result[1] render('sign_up', user: params[:user], errors: errors) end end get '/create-service' do if params[:t] case (result = Services.get_existing_services(params[:t]))[0] when :ok render('create_service', token: params[:t], service_exists: result[1] != 0) when :error render('error') end else redirect '/sign-up' end end post '/create-service' do if params[:t] case (result = Services.create_service(params[:t], params[:service]))[0] when :ok redirect "/complete?t=#{params[:t]}&id=#{result[1]['id']}" when :error render('error') end else redirect '/sign-up' end end get '/connect-service' do if params[:t] case (result = Services.get_existing_services(params[:t]))[0] when :ok render('connect_service', token: params[:t], services: result[1]) when :error render('error') end else redirect '/sign-up' end end post '/connect-service' do redirect "/complete?t=#{params[:t]}&id=#{params[:service][:id]}" end get '/complete' do if params[:t] && params[:id] id_is_valid = Services.verify_service_id(params[:t], params[:id]) secret_key = Services.get_secret_key(params[:t]) if id_is_valid && !secret_key.nil? Services.import_settings(secret_key, params[:id]) render('complete') else render('error') end else render('error') end end private def render(view_name, assigns={}) Views.send("#{view_name}_view", assigns) end set :bind, '0.0.0.0' set :port, Adminix::Config.instance.setup_server_port run! end end