Skip to content
This repository was archived by the owner on May 4, 2022. It is now read-only.

Commit f283658

Browse files
committed
BF add github auth helper and server start helper
1 parent 148a7fd commit f283658

7 files changed

Lines changed: 103 additions & 10 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
config/application.yml

README.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,35 @@ Install the gems:
3434
bundle
3535
```
3636

37-
Then set the application settings as environment variables:
37+
Then set up your the application config and database settings
3838

3939
```bash
40-
export GITHUB_CLIENT_ID="<from GH>"
41-
export GITHUB_CLIENT_SECRET="<from GH>"
40+
./serve.sh setup`
4241
```
4342

43+
Update the GITHUB keys in your `config/application.yml`
44+
45+
4446
Finally start the web server using thin:
4547

4648
```bash
47-
bundle exec thin start # start the server on 0.0.0.0:3000
49+
./serve.sh start
50+
```
51+
52+
If you want to daemonize your dev server
53+
54+
```bash
55+
./serve.sh start -d
56+
./serve.sh stop
4857
```
4958

50-
### Deploying to heroku
59+
### Deploying to heroku (production)
5160

5261
```bash
5362
heroku addons:add heroku-postgresql:dev
5463
heroku config:set GITHUB_CLIENT_ID="<from GH>" GITHUB_CLIENT_SECRET="<from GH>"
64+
git push heroku master
65+
heroku run rake db:migrate
5566
```
5667

5768
## License

app.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@
44
require 'sinatra/activerecord'
55
require './models/code'
66
require './models/user'
7+
require './models/github_auth'
78

89
module PpwmMatcher
910
class App < Sinatra::Base
1011
enable :sessions
1112

12-
set :github_options, {
13-
:scopes => "user:email",
14-
:secret => ENV['GITHUB_CLIENT_SECRET'],
15-
:client_id => ENV['GITHUB_CLIENT_ID'],
16-
}
13+
set :github_options, PpwmMatcher::GithubAuth.options
1714
LOGGER = Logger.new(STDOUT)
1815

1916
register Sinatra::Auth::Github

config/application.example.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
development:
2+
GITHUB_CLIENT_ID: ""
3+
GITHUB_CLIENT_SECRET: ""
4+
production:
5+
GITHUB_CLIENT_ID: ""
6+
GITHUB_CLIENT_SECRET: ""

init.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require 'yaml'
2+
db_config_filename = 'config/database.yml'
3+
db_name = YAML.load(File.read("./#{db_config_filename}"))['development']['database']
4+
puts "Initializing and migrating postgresql db #{db_name}"
5+
`createdb #{db_name} && rake db:migrate`

models/github_auth.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module PpwmMatcher
2+
module GithubAuth
3+
4+
module_function
5+
6+
def options
7+
{
8+
:scopes => "user:email",
9+
:secret => secret,
10+
:client_id => client_id,
11+
}
12+
end
13+
14+
15+
def secret
16+
dev_config[secret_key] || ENV[secret_key]
17+
end
18+
def secret_key
19+
'GITHUB_CLIENT_SECRET'
20+
end
21+
def client_id
22+
dev_config[client_id_key] || ENV[client_id_key]
23+
end
24+
def client_id_key
25+
'GITHUB_CLIENT_ID'
26+
end
27+
def dev_config
28+
config.fetch('development', {})
29+
end
30+
def prod_config
31+
config.fetch('production', {})
32+
end
33+
def config
34+
@config ||= if File.exists?(app_config_filename)
35+
YAML.load(File.read(app_config_filename))
36+
else
37+
{}
38+
end
39+
end
40+
def app_config_filename
41+
'config/application.yml'
42+
end
43+
end
44+
end

serve.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
# ./serve.sh setup
3+
# ./serve.sh start
4+
if [ ! -n "$1" ]
5+
then
6+
echo "Usage: ./serve.sh [start|stop|restart|setup]"
7+
else
8+
case "$1" in
9+
10+
setup)
11+
test -s 'config/application.yml'
12+
if [ $? -eq 1 ]
13+
then
14+
echo -e 'copying over example application.yml'
15+
cp config/application.example.yml config/application.yml
16+
fi
17+
ruby init.rb
18+
;;
19+
20+
start)
21+
bundle exec thin start -p 9393 $2
22+
;;
23+
24+
stop)
25+
bundle exec thin stop
26+
;;
27+
28+
esac
29+
fi

0 commit comments

Comments
 (0)