Skip to content

Commit c41507f

Browse files
committed
Allow an easy local-productionish setup
This uses the development environment, but starts up rails in production mode. It allows us to test features which we expect to work differently in production mode, such as the profiling tools.
1 parent 78e6f77 commit c41507f

File tree

5 files changed

+27
-13
lines changed

5 files changed

+27
-13
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ dev: ## Start dev stack (foreground)
88
dev-detach: ## Start dev stack in background
99
$(COMPOSE) up -d --build
1010

11+
dev-prod-detach: ## Start dev stack but run Rails in production mode (uses dev compose & env)
12+
RAILS_ENV=production NODE_ENV=production RAILS_SERVE_STATIC_FILES=1 RAILS_LOG_TO_STDOUT=1 FORCE_SSL=false $(COMPOSE) up -d --build
13+
1114
down: ## Stop dev stack
1215
$(COMPOSE) down
1316

app/controllers/sessions_controller.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,25 @@ def new
33
end
44

55
def create
6-
email = EmailNormalizer.normalize(params[:email])
7-
user = Alias.by_email(email).includes(:user).first&.user
6+
email = params[:email].to_s
7+
normalized_email = begin
8+
EmailNormalizer.normalize(email)
9+
rescue StandardError
10+
nil
11+
end
12+
user = normalized_email && Alias.by_email(normalized_email).includes(:user).first&.user
13+
verified_alias = user&.aliases&.where&.not(verified_at: nil)&.exists?
814

9-
if user&.authenticate(params[:password]) && user.aliases.where.not(verified_at: nil).exists?
15+
if user.nil? || !user.authenticate(params[:password])
16+
flash.now[:alert] = 'Invalid email or password'
17+
render :new, status: :unauthorized
18+
elsif !verified_alias
19+
flash.now[:alert] = 'Please verify your email before signing in.'
20+
render :new, status: :forbidden
21+
else
1022
reset_session
1123
session[:user_id] = user.id
1224
redirect_to root_path, notice: 'Signed in successfully'
13-
else
14-
flash.now[:alert] = 'Invalid email or password'
15-
render :new, status: :unauthorized
1625
end
1726
end
1827

@@ -21,4 +30,3 @@ def destroy
2130
redirect_to root_path, notice: 'Signed out'
2231
end
2332
end
24-

app/views/sessions/new.html.slim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
h1 Sign in
22

3-
= form_with url: session_path, method: :post do |f|
3+
= form_with url: session_path, method: :post, local: true do |f|
44
.field
55
= label_tag :email
66
= email_field_tag :email

config/environments/production.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
# Store uploaded files on the local file system (see config/storage.yml for options).
2525
config.active_storage.service = :local
2626

27+
force_ssl_enabled = ActiveModel::Type::Boolean.new.cast(ENV.fetch("FORCE_SSL", "true"))
28+
2729
# Assume all access to the app is happening through a SSL-terminating reverse proxy.
28-
config.assume_ssl = true
30+
config.assume_ssl = force_ssl_enabled
2931

3032
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
31-
config.force_ssl = true
33+
config.force_ssl = force_ssl_enabled
3234

3335
# Skip http-to-https redirect for the default health check endpoint.
3436
# config.ssl_options = { redirect: { exclude: ->(request) { request.path == "/up" } } }

docker-compose.dev.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ services:
2828
env_file:
2929
- .env.development
3030
environment:
31-
RAILS_ENV: development
32-
NODE_ENV: development
31+
RAILS_ENV: ${RAILS_ENV:-development}
32+
NODE_ENV: ${NODE_ENV:-development}
3333
DATABASE_URL: ${DATABASE_URL:-postgresql://${POSTGRES_USER:-hackorum}:${POSTGRES_PASSWORD:-hackorum}@db:5432/${POSTGRES_DB:-hackorum_development}}
3434
PGHOST: db
3535
PGPORT: 5432
3636
PGUSER: ${POSTGRES_USER:-hackorum}
3737
PGPASSWORD: ${POSTGRES_PASSWORD:-hackorum}
3838
PGDATABASE: ${POSTGRES_DB:-hackorum_development}
39+
FORCE_SSL: ${FORCE_SSL:-false}
3940
volumes:
4041
- .:/app
4142
- bundle:/usr/local/bundle
@@ -56,7 +57,7 @@ services:
5657
- .env.development
5758
command: ["bundle", "exec", "ruby", "script/imap_idle.rb"]
5859
environment:
59-
RAILS_ENV: development
60+
RAILS_ENV: ${RAILS_ENV:-development}
6061
DATABASE_URL: ${DATABASE_URL:-postgresql://${POSTGRES_USER:-hackorum}:${POSTGRES_PASSWORD:-hackorum}@db:5432/${POSTGRES_DB:-hackorum_development}}
6162
PGHOST: db
6263
PGPORT: 5432

0 commit comments

Comments
 (0)