forked from wurmlab/afra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
150 lines (126 loc) · 2.95 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
require 'logger'
require 'sequel'
require 'sinatra/base'
require 'puma/server'
module App
class Routes < Sinatra::Base
class << self
## OVERRIDE
def middleware
@middleware
end
def inherited(klass)
super
use klass
end
## /OVERRIDE
end
enable :logging
disable :show_exceptions
end
class Server < Puma::Server
attr_reader :binds, :daemonize
def initialize(binds, daemonize: false)
@binds = binds
@daemonize = daemonize
super nil
end
def serve(app)
self.app = app
binder.parse binds, events
if daemonize
events.log "* Daemonizing ..."
Process.daemon(true)
end
run.join
rescue Interrupt
# swallow it
end
end
extend self
def init_config(**config)
defaults = {
db_uri: 'postgres://localhost/afra',
binds: ['tcp://0.0.0.0:9292']
}
@config = defaults.update config
rescue Errno::ENOENT
puts "Couldn't find file: #{config}."
end
def init_db
@db = Sequel.connect config[:db_uri], loggers: Logger.new($stderr)
@db.extension :pg_json
@db.extension :pg_array
Sequel.extension :pg_json_ops
Sequel.datetime_class = DateTime
end
def init_server
@server = Server.new(config[:binds], daemonize: config[:daemonize])
end
attr_reader :config, :db, :server
def load_models
init_db
Sequel::Model.db = db
Sequel::Model.plugin :json_serializer
Dir['models/*.rb'].each do |model|
require_relative model
end
rescue Sequel::DatabaseConnectionError
puts "Couldn't connect to database."
exit
end
def load_routes
Dir['routes/*.rb'].sort.each do |route|
require_relative route
end
end
def load_services
load_models
Dir['services/*.rb'].each do |service|
require_relative service
end
end
def migrate(version: nil, **options)
init_config **options
init_db
Sequel.extension :migration
db.extension :constraint_validations
Sequel::Migrator.apply(db, File.expand_path('migrations'), version)
rescue Sequel::DatabaseConnectionError
puts "Couldn't connect to database."
exit
end
def auto_check_tasks(**options)
puts "Auto check ..."
init_config **options
load_models
Task.all.select do |t|
next unless t.submissions.count >= 3 # TODO: I think it should be == 3
if t.submissions.uniq.length == 1
t.contributions.each do |contribution|
contribution.status = 'accepted'
contribution.save
end
else
t.contributions.each do |contribution|
contribution.status = 'accepted'
contribution.save
end
end
end
end
def irb(**options)
init_config **options
load_models
load_services
require 'irb'
IRB.start
end
def serve(**options)
init_config **options
load_models
load_routes
init_server
server.serve(Routes)
end
end