-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
70 lines (57 loc) · 1.57 KB
/
app.rb
File metadata and controls
70 lines (57 loc) · 1.57 KB
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
require 'sinatra'
require 'sinatra/cross_origin'
require 'securerandom'
require 'json'
require_relative 'lib/tasks'
enable :cross_origin
set :show_exceptions, false
use Rack::Auth::Basic, 'Restricted' do |user, pass|
user == ENV['LOGIN_USER'] && pass == ENV['LOGIN_PASS']
end
use Rack::Session::Cookie, key: 'hex', secret: ENV['SECRET']
use Rack::MethodOverride
options '*' do
response.headers['Allow'] = 'HEAD,GET,PUT,POST,DELETE,OPTIONS'
response.headers['Access-Control-Allow-Headers'] =
'X-Requested-With, X-HTTP-Method-Override, Content-Type, Cache-Control, Accept'
200
end
configure do
`git config --global user.name "GitBackend"` if `git config user.name`.empty?
`git config --global user.email "git-backend@example.com"` if `git config user.email`.empty?
end
before do
session[:hex] ||= SecureRandom.hex
end
error do
content_type :json
err = env['sinatra.error']
{ error: { type: err.class, message: err.message } }.to_json
end
post '/clone' do
content_type :json
files = Tasks.clone(session[:hex], ENV['REMOTE_URL'], ENV['REMOTE_BRANCH'])
{ files: files }.to_json
end
post '/push' do
Tasks.push(session[:hex], params['message'] || 'GitBackend changes...')
200
end
get '/ls' do
content_type :json
files = Tasks.ls(session[:hex])
{ files: files }.to_json
end
get '/read/*' do |path|
content_type :json
content = Tasks.read(session[:hex], path)
{ content: content }.to_json
end
post '/write/*' do |path|
Tasks.write(session[:hex], path, request.body.read)
200
end
post '/delete/*' do |path|
Tasks.delete(session[:hex], path)
200
end