forked from rngtng/vizard.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb
99 lines (76 loc) · 1.8 KB
/
server.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
# encoding: utf-8
require 'rubygems'
require 'bundler/setup'
require 'dotenv'
require 'sinatra'
require 'haml'
require 'sass/plugin/rack'
require 'newrelic_rpm'
require 'base64'
require 'lib/github'
require 'lib/plantuml_renderer'
require 'lib/cache_helper'
Dotenv.load(ENV['ENV'] || '.env')
CONTENT_TYPE_MAPPING = {
'png' => 'image/png',
'txt' => 'text/plain',
'utxt' => 'text/plain',
}
require "rack-timeout"
Rack::Timeout.timeout = 20
use Rack::Timeout
use Sass::Plugin::Rack
newrelic_ignore '/ping'
Sass::Plugin.options.merge({
:css_location => './public/css/',
:template_location => 'public/css',
:style => :compressed,
})
set :haml, :format => :html5, :escape_attrs => false
helpers do
def github
@github ||= Github.new(ENV['GITHUB_ID'], ENV['GITHUB_SECRET'])
end
def format
params["format"]
end
def set_content_type(format)
if content_type_value = CONTENT_TYPE_MAPPING[format]
content_type content_type_value
end
end
end
# ---------------------------------------------------
before do
if request.host.include?('heroku')
redirect 'http://vizard.io'
end
end
# ---------------------------------------------------
after do
if request.env["HTTP_ACCEPT"] =~ /base64/
body Base64.encode64(body.first)
end
end
# ---------------------------------------------------
post '/render.:format' do
set_content_type(format)
cache_control :public
# cache(request.env["QUERY_STRING"], format) do
PlantumlRenderer.render(request.body.string, format)
# end
end
# ---------------------------------------------------
get '/login' do
if code = params["code"]
haml :login, :locals => { :access_token => github.get_access_token(code) }
else
redirect github.auth_url
end
end
get %r{(edit)?} do
haml :index
end
get '/ping' do
'OK'
end