source: subversion/utils/little-osm/server.rb @ 1168

Last change on this file since 1168 was 1168, checked in by imi, 14 years ago

fixed little-osm. It is usable together with JOSM again.

  • Property svn:executable set to *
File size: 1.1 KB
Line 
1#!/usr/bin/ruby
2require 'socket'
3require 'uri'
4
5$stdout.sync = true
6
7puts "Start server on localhost:3000"
8server = TCPServer.new('127.0.0.1', 3000)
9loop do
10  session = server.accept
11  puts "connection accepted"
12  Thread.start(session) do |session|
13    uri = URI.parse session.gets.scan(/GET \/(.*) HTTP/)[0][0]
14    cmd = uri.path.split("/")[-1]
15    cmd ||= "test"
16    Thread.current["uri"] = uri
17    Thread.current["session"] = session
18    puts Thread.current.object_id.to_s + ": call command '#{cmd}'"
19    old_out, old_in = $stdout, $stdin
20    $stdout, $stdin = session, session
21    begin
22      catch :little_osm_done do
23        load "api/#{cmd}.rb"
24      end
25    rescue => x
26      session.print "HTTP/1.1 500/Exception executing script\r\n",
27        "Server: little-osm\r\n",
28        "Content-type: text/plain\r\n\r\n",
29        "Error while executing script #{cmd}.rb:\r\n",
30        "#{x}\r\n  ",
31        x.backtrace.join("\r\n  ")
32    ensure
33      $stdout, $stdin = old_out, old_in
34      puts Thread.current.object_id.to_s + ": call finished"
35      session.close
36    end
37  end
38end
Note: See TracBrowser for help on using the repository browser.