-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.rb
105 lines (86 loc) · 2.63 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# frozen_string_literal: true
# ruby-lang.org mailing list service/subscriber
#
# project home page: https://github.com/stomar/ruby-lang-mls
#
# Copyright (C) 2013-2024 Marcus Stollsteimer
#
# License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
require "sinatra/base"
require_relative "lib/mls"
require_relative "db/connection"
require_relative "db/models" if DB
ENV["TZ"] = "UTC"
SENDER_EMAIL = ENV.fetch("SENDER_EMAIL", nil)
SMTP_USER = ENV.fetch("SMTP_USER", nil)
SMTP_PASSWORD = ENV.fetch("SMTP_PASSWORD", nil)
SMTP_ADDRESS = ENV["SMTP_SERVER"] || ""
SMTP_PORT = ENV["SMTP_PORT"] || "587"
NO_CONFIRM = ENV["NO_CONFIRM"] == "true"
NO_LOGS = ENV["NO_LOGS"] == "true"
# The application class.
class App < Sinatra::Base
set :environment, :production
configure do
mailer = MLS::Mailer.new(
sender_email: SENDER_EMAIL,
smtp_user: SMTP_USER,
smtp_password: SMTP_PASSWORD,
smtp_address: SMTP_ADDRESS,
smtp_port: SMTP_PORT
)
set :mailer, mailer
set :logger, MLS::Logger.new(no_logs: NO_LOGS)
set :stats, MLS::StatsHandler.new
messages = {
success: {
header: "Confirmation",
text: "Your request has been accepted. " \
"To complete your request, please follow the instructions " \
"in the email you should receive shortly."
},
invalid: {
header: "Invalid request",
text: "Your request is invalid. " \
"Please make sure that you filled out all fields."
},
error: {
header: "Error",
text: "Sorry, an error occurred during processing of your request."
}
}
set :messages, messages
set :status_codes, { success: 200, invalid: 400, error: 500 }
end
def escape(text)
Rack::Utils.escape_html(text)
end
get "/" do
erb :index
end
post "/submit" do
@ml_request = MLS::Request.new(params)
if @ml_request.valid?
begin
settings.mailer.mail(@ml_request.mail_options)
status = :success
settings.logger.log(@ml_request.list, @ml_request.action)
settings.stats.increment(@ml_request.list, @ml_request.action)
rescue StandardError => e
status = :error
settings.logger.log_error(@ml_request.list, @ml_request.action, e)
end
else
status = :invalid
settings.logger.log_invalid(@ml_request.list, @ml_request.action)
end
@header = settings.messages[status][:header]
@message = settings.messages[status][:text]
if NO_CONFIRM
redirect back
else
status settings.status_codes[status]
erb :confirmation
end
end
end