Created
September 11, 2018 11:52
-
-
Save arpitchauhan/b701ff08d677bba1f6244190889a19c0 to your computer and use it in GitHub Desktop.
Creating a Minimal Sinatra App For Hosting Test Files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BasicAuth | |
class << self | |
def enable_if_possible | |
return unless on? | |
Sinatra::Application.use(Rack::Auth::Basic) do |username, password| | |
valid_credentials?(username, password) | |
end | |
end | |
private | |
# Basic auth is on only if both username and password are set | |
def on? | |
username && password | |
end | |
def valid_credentials?(username_, password_) | |
raise 'Arguments missing' unless username_ && password_ | |
username_ == username && password == password_ | |
end | |
def username | |
ENV['BASIC_AUTH_USERNAME'] | |
end | |
def password | |
ENV['BASIC_AUTH_PASSWORD'] | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './server' | |
run Sinatra::Application | |
$stdout.sync = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source "https://rubygems.org" | |
ruby '2.5.1' | |
gem 'sinatra', '~> 2.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# server.rb | |
require 'sinatra' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require_relative 'basic_auth' | |
BasicAuth.enable_if_possible |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment