Class: Nuri::Service

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/naas/naas.rb

Constant Summary collapse

HOME =
File.expand_path('~/.nuri')
WebDir =
File.expand_path('.', File.dirname(__FILE__))
WebFiles =
['index.html', 'index.js', 'index.css', 'jquery.js', 'd3.js']
PIDFile =
'/tmp/nuri_service.pid'
Log =
Logger.new(HOME + "/nuri.log")
Port =
8383
@@enabled =
false

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ Service

Returns a new instance of Service.



84
85
86
87
# File 'lib/naas/naas.rb', line 84

def initialize(server)
	@server = server
	@cache = {}
end

Class Method Details

.pidObject



80
81
82
# File 'lib/naas/naas.rb', line 80

def self.pid
	(File.exist?(PIDFile) ? File.read(PIDFile).to_i : 1)
end

.startObject



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
# File 'lib/naas/naas.rb', line 24

def self.start
	fail "Nuri service is already running with PID #{pid}" if status == :running

	Process.daemon
	Log.info 'starting'
	@@enabled = true

	Signal.trap('HUP') {
		@@enabled = false
		@@server.shutdown if defined?(@@server) and !@@server.nil?
	}

	until not @@enabled do
		begin
			File.open(PIDFile, 'w') { |f| f.write($$.to_s) }

			config = {
				:Port => Port,
				:ServerType => WEBrick::SimpleServer,
				:DirectoryIndex => 'index.html',
				:Logger => Log
			}
			@@server = WEBrick::HTTPServer.new(config)
			@@server.mount "/", Nuri::Service # WEBrick::HTTPServlet::FileHandler, WebDir
			@@server.start

			sleep 1
		rescue Exception => e
			Log.error "#{e}"
			Log.debug e.backtrace.join("\n")
			sleep 10
		end
	end

	Log.info 'stopped'
end

.statusObject



61
62
63
64
65
66
67
68
# File 'lib/naas/naas.rb', line 61

def self.status
	begin
		Process.kill 0, pid
		return :running
	rescue
	end
	:stopped
end

.stopObject



70
71
72
73
74
75
76
77
78
# File 'lib/naas/naas.rb', line 70

def self.stop
	if status == :running
		Log.info 'stopping'
		Process.kill 'HUP', pid
		sleep 5
		system "kill -9 #{pid}" if status == :running
	end
	true
end

Instance Method Details

#do_GET(request, response) ⇒ Object



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
# File 'lib/naas/naas.rb', line 89

def do_GET(request, response)
	status = 404
	content_type = body = ''

	if not trusted?(request)
		status = 403
	else
		case request.path
		when '/'
			status = 200
			content_type = 'text/html'
			@cache[WebFiles[0]] = File.read(WebDir + '/' + WebFiles[0]) if !@cache.has_key?(WebFiles[0])
			body = @cache[WebFiles[0]]
		when '/modules/'
			status, content_type, body = get_modules
		when /^\/.+/
			req = request.path[1, request.path.length-1]
			WebFiles.each do |file|
				if file == req
					status = 200
					case File.extname(file)
					when '.css'
						content_type = 'text/css'
					when '.js'
						content_type = 'application/javascript'
					else
						content_type = 'text/plain'
					end
					@cache[file] = File.read(WebDir + '/' + file) if !@cache.has_key?(file)
					body = @cache[file]
					break
				end
			end
		end
	end

	response.status = status
	response['Content-Type'] = content_type
	response.body = body
end

#get_modulesObject



135
136
137
138
139
# File 'lib/naas/naas.rb', line 135

def get_modules
	module_dir = WebDir + '/../../modules'
	modules = Dir.entries(module_dir).select { |entry| File.directory?(File.expand_path(module_dir + '/' + entry)) and !(entry == '.' or entry == '..') }
	[200, 'application/json', JSON.generate({'modules' => modules})]
end

#trusted?(request) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
133
# File 'lib/naas/naas.rb', line 130

def trusted?(request)
	# TODO
	true
end