This repository has been archived by the owner on May 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.rb
executable file
·112 lines (89 loc) · 2.31 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
100
101
102
103
104
105
106
107
108
109
110
111
112
#! /usr/bin/env jruby
# frozen_string_literal: true
require 'rubygems'
require 'bundler/setup'
require 'dotenv'
require 'sinatra'
require 'haml'
require 'sass/plugin/rack'
# require 'rack/sassc'
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'
}.freeze
require 'rack-timeout'
use Rack::Timeout, service_timeout: 20
use Sass::Plugin::Rack
Sass::Plugin.options.merge(
css_location: './docs/css/',
template_location: 'docs/css',
style: :compressed
)
# use Rack::SassC, {
# check: ENV['RACK_ENV'] != 'production',
# public_location: 'publi/css',
# syntax: :scss,
# css_dirname: :css,
# scss_dirname: :scss,
# create_map_file: true,
# }
# Sass::Plugin.options.merge({
# :css_location => './docs/css/',
# :template_location => 'docs/css',
# :style => :compressed,
# })
set :haml, format: :html5, escape_attrs: false
set :static, true
set :public_folder, Proc.new { File.join(root, "docs") }
helpers do
def github
@github ||= Github.new(ENV['GITHUB_ID'], ENV['GITHUB_SECRET'])
end
def format
params['format']
end
def set_content_type(format)
# rubocop:disable Style/GuardClause
if (content_type_value = CONTENT_TYPE_MAPPING[format])
content_type content_type_value
end
# rubocop:enable Style/GuardClause
end
end
# ---------------------------------------------------
# before do
# redirect 'http://vizard.io' if request.host.include?('heroku')
# end
# ---------------------------------------------------
after do
body Base64.encode64(body.first) if request.env['HTTP_ACCEPT'] =~ /base64/
end
# ---------------------------------------------------
post '/render.:format' do
set_content_type(format)
cache_control :public
headers 'Access-Control-Allow-Origin' => '*'
# 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 '/ping' do
'OK'
end
get %r{(/edit)?/.*} do
haml :'index-old'
end