Class: Gitdocs::Server
- Inherits:
-
Object
- Object
- Gitdocs::Server
- Defined in:
- lib/gitdocs/server.rb
Instance Method Summary collapse
-
#initialize(manager, *gitdocs) ⇒ Server
constructor
A new instance of Server.
- #start(port = 8888) ⇒ Object
Constructor Details
#initialize(manager, *gitdocs) ⇒ Server
Returns a new instance of Server.
12 13 14 15 |
# File 'lib/gitdocs/server.rb', line 12 def initialize(manager, *gitdocs) @manager = manager @gitdocs = gitdocs end |
Instance Method Details
#start(port = 8888) ⇒ Object
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 151 152 153 |
# File 'lib/gitdocs/server.rb', line 17 def start(port = 8888) gds = @gitdocs manager = @manager Tilt.prefer ::MarkdownTemplate, 'md' Tilt.prefer ::MarkdownTemplate, 'mkd' Tilt.prefer ::MarkdownTemplate, 'markdown' Thin::Logging.debug = @manager.debug Thin::Server.start('127.0.0.1', port) do use Rack::Static, :urls => ['/css', '/js', '/img', '/doc'], :root => File.("../public", __FILE__) use Rack::MethodOverride run Renee { if request.path_info == '/' if manager.config.shares.size == 1 redirect! "/0" else render! "home", :layout => 'app', :locals => {:conf => manager.config, :nav_state => "home" } end else path 'settings' do get.render! 'settings', :layout => 'app', :locals => {:conf => manager.config, :nav_state => "settings" } post do shares = manager.config.shares manager.config.global.update_attributes(request.POST['config']) request.POST['share'].each do |idx, share| if remote_branch = share.delete('remote_branch') share['remote_name'], share['branch_name'] = remote_branch.split('/', 2) end # Update paths if share['path'] && !share['path'].empty? shares[Integer(idx)].update_attributes(share) end end EM.add_timer(0.1) { manager.restart } redirect! '/settings' end end path('search').get do render! "search", :layout => 'app', :locals => {:conf => manager.config, :results => manager.search(request.GET['q']), :nav_state => nil} end path('shares') do post do Configuration::Share.create redirect! '/settings' end var(:int) do |id| delete do share = manager.config.shares.find { |s| s.id == id } halt 404 if share.nil? share.destroy redirect! '/settings' end end end var :int do |idx| gd = gds[idx] halt 404 if gd.nil? file_path = URI.unescape(request.path_info) file_ext = File.extname(file_path) = File.(".#{file_path}", gd.root) = File.(".gitmessage~", gd.root) halt 400 unless [/^#{Regexp.quote(gd.root)}/] parent = File.dirname(file_path) parent = '' if parent == '/' parent = nil if parent == '.' locals = {:idx => idx, :parent => parent, :root => gd.root, :file_path => , :nav_state => nil } mime = File.mime_type?(File.open()) if File.file?() mode = request.params['mode'] if mode == 'meta' # Meta halt 200, { 'Content-Type' => 'application/json' }, [gd.(file_path).to_json] elsif mode == 'save' # Saving File.open(, 'w') { |f| f.print request.params['data'] } File.open(, 'w') { |f| f.print request.params['message'] } unless request.params['message'] == '' redirect! "/" + idx.to_s + file_path elsif mode == 'upload' # Uploading halt 404 unless file = request.params['file'] tempfile, filename = file[:tempfile], file[:filename] FileUtils.mv(tempfile.path, File.(filename, )) redirect! "/" + idx.to_s + file_path + "/" + filename elsif !File.exist?() && !request.params['dir'] # edit for non-existent file FileUtils.mkdir_p(File.dirname()) FileUtils.touch() redirect! "/" + idx.to_s + file_path + "?mode=edit" elsif !File.exist?() && request.params['dir'] # create directory FileUtils.mkdir_p() redirect! "/" + idx.to_s + file_path elsif File.directory?() # list directory contents = gd.dir_files() rendered_readme = nil if readme = Dir[File.("README.{md,txt}", )].first rendered_readme = '<h3>' + File.basename(readme) + '</h3><div class="tilt">' + render(readme) + '</div>' end render! "dir", :layout => 'app', :locals => locals.merge(:contents => contents, :rendered_readme => rendered_readme) elsif mode == "revisions" # list revisions revisions = gd.file_revisions(file_path) render! "revisions", :layout => 'app', :locals => locals.merge(:revisions => revisions) elsif mode == "revert" # revert file if revision = request.params['revision'] File.open(, 'w') { |f| f.print "Reverting '#{file_path}' to #{revision}" } gd.file_revert(file_path, revision) end redirect! "/" + idx.to_s + file_path elsif mode == 'delete' # delete file FileUtils.rm() redirect! "/" + idx.to_s + parent elsif mode == 'edit' && (mime.match(%r{text/}) || mime.match(%r{x-empty})) # edit file contents = File.read() render! "edit", :layout => 'app', :locals => locals.merge(:contents => contents) elsif mode != 'raw' # render file revision = request.params['revision'] = gd.file_revision_at(file_path, revision) if revision begin # attempting to render file contents = '<div class="tilt">' + render() + '</div>' rescue RuntimeError => e # not tilt supported contents = if mime.match(%r{text/}) '<pre class="CodeRay">' + CodeRay.scan_file().encode(:html) + '</pre>' else %|<embed class="inline-file" src="/#{idx}#{request.path_info}?mode=raw"></embed>| end end render! "file", :layout => 'app', :locals => locals.merge(:contents => contents, :pygments_css => Pygments.css('.highlight')) else # other file run! Rack::File.new(gd.root) end end end }.setup { views_path File.("../views", __FILE__) } end end |