This repository has been archived by the owner on Sep 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.rb
111 lines (84 loc) · 2.32 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
require 'sinatra'
require 'sequel'
require 'sequel/extensions/migration'
require 'sprockets'
require 'haml'
require 'maruku'
require 'sass'
require 'compass'
require 'bootstrap-sass'
require 'yui/compressor'
class Doi2Oa < Sinatra::Base
configure do
set :root, File.dirname(__FILE__)
set :haml, format: :html5, layout: :layout
set :scss, style: :compact, debug_info: false
set :markdown, layout: :layout, layout_engine: :haml
set :assets_path, '/assets'
environment = Sprockets::Environment.new
%w{images javascripts stylesheets}.each do |type|
environment.append_path File.join(Compass::Frameworks['bootstrap'].path, 'vendor', 'assets', type)
environment.append_path File.join('assets', type)
end
environment.js_compressor = YUI::JavaScriptCompressor.new
environment.css_compressor = YUI::CssCompressor.new
environment.context_class.class_eval do
def asset_path(path, options = {})
File.join(Doi2Oa.assets_path, path)
end
end
set :sprockets, environment
end
configure :development, :production do
DB = Sequel.connect(ENV['DATABASE_URL'] \
|| "sqlite://tmp/#{settings.environment}.sqlite")
end
configure :test do
DB = Sequel.sqlite
end
configure do
Sequel::Migrator.apply(DB, 'db/migrations')
Sequel::Model.db = DB
require_relative 'models/doi_mapping'
require_relative 'models/repository'
Compass.add_project_configuration(File.join(root, 'config', 'compass.rb'))
end
helpers do
alias_method :h, :escape_html
def menu_class(link_path)
request.path_info == link_path ? {class: 'active'} : {}
end
def ga_id
ENV['GA_ID'] || ''
end
end
get '/' do
markdown :index
end
get '/about' do
markdown :about
end
get '/repositories' do
@repositories = Repository.all
haml :repositories
end
get %r{/resolve/?(?<doi_capture>.+)?} do
unless doi = params[:doi] || params[:doi_capture]
error 400
end
if dest = DoiMapping.resolve(doi)
return dest
end
error 404
end
get %r{/redirect/?(?<doi_capture>.+)?} do
unless doi = params[:doi] || params[:doi_capture]
error 400
end
if dest = DoiMapping.resolve(doi)
redirect dest
end
error 404
end
run! if app_file == $0
end