minimal-http-ruby 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e6ad76ce1c928006b7809051a82ac5af630103e2
4
- data.tar.gz: 673cfbbdd403bc840100a36174c755f35f2339cb
3
+ metadata.gz: 7e1af06230f27d9602d5958050d5970d7e424bdb
4
+ data.tar.gz: ee352828ef7ca87f4163532674f993d390bb2247
5
5
  SHA512:
6
- metadata.gz: 87bfb7b69275bde6e33ad3fdee12e0799ee47a0f68f9a7542b726d7799ab66d3c9ffe3d161f8316e9f41278fc72dedfdfbb14feccea7f1ddf153eed856b515c2
7
- data.tar.gz: 31a90eacb753a430e1780ad121999c20a1600b3e24b0030d2cb3a051fc0382f3f0bd4dae8cd6c0376c0a7cb86f5f2928b6da91b945536bf87e7a543b9573642a
6
+ metadata.gz: d70a82b930defb1738e0a7b47b11deaba5227524707770eac65ff289707c520ca3470d512089ccd3d44642fbd7d9a748874bcc6724352fcc55159f9513cce78e
7
+ data.tar.gz: 02f9b14c38bd16f14c9a9796c73ef4530caafd7778a38a4be00a18e70deb2f9ef436e736ce38df1bc7b29990c68d9908337b6d1d46111ceceb709104fa6eb43e
@@ -2,30 +2,25 @@
2
2
  # encode: UTF-8
3
3
 
4
4
  require "fileutils"
5
+ require "rubygems"
6
+ require 'minimal-http-ruby'
5
7
 
6
8
 
7
9
  puts "minimal-http-ruby Initializer"
8
- if File.directory? "./http"
9
- puts "Error: ./http Already Exists -- Cannot create!"
10
+
11
+ target=ARGV[0]||"./http"
12
+
13
+ if File.directory? target
14
+ puts "Error: #{target} Already Exists -- Cannot create!"
10
15
  exit(-1)
11
16
  end
12
- Dir.mkdir "./http"
13
- if File.directory? "./http"
14
- Dir.mkdir "./http/coffee"
15
- Dir.mkdir "./http/css"
16
- Dir.mkdir "./http/haml"
17
- Dir.mkdir "./http/json"
18
- File.open("./http/haml/index.haml", 'w') do |file|
19
- file.write <<END
20
- !!!
21
- %body
22
- %h1 Congratulations!
23
- %hr
24
- You have installed minimal-http-ruby
25
- %hr
26
- Next, please edit file: #{Dir.pwd}/http/haml to get started!
27
17
 
28
- END
29
- end
18
+ Dir.mkdir target
19
+ if File.directory? target
20
+ puts "Copying Files..."
21
+ path=File.join( Gem.loaded_specs['minimal-http-ruby'].full_gem_path, 'http/')
22
+ FileUtils.cp_r "#{path}/.", "#{target}"
30
23
  puts "Created OK!"
24
+ puts "Next, please edit file: #{target}/haml/index.haml to get started!"
25
+ system "tree #{target}"
31
26
  end
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'minimal-http-ruby'
4
+ require 'optparse'
5
+
6
+ options = {}
7
+
8
+ OptionParser.new do |opts|
9
+ opts.banner = "Usage: #{$PROGRAM_NAME} [options]"
10
+
11
+ opts.on("-v", "--[no-]verbose", "Run verbosely; creates protocol log on console (false)") do |v|
12
+ options[:verbose] = v
13
+ end
14
+
15
+ options[:http_port]=8088
16
+ opts.on("-p", "--http_port port", "Local port to listen (8088)") do |v|
17
+ options[:http_port] = v.to_i
18
+ end
19
+ options[:http_path]="./http/"
20
+ opts.on("-d", "--http_path path", "Location of http files to serve (./http/)") do |v|
21
+ options[:http_path] = v
22
+ end
23
+ end.parse!
24
+
25
+ puts "ok"
26
+ pp options
27
+ minimal_http_server options
28
+
29
+ loop do #or whatever you need to do
30
+ sleep 1
31
+ end
@@ -0,0 +1,43 @@
1
+
2
+ now=0
3
+ xx=123
4
+
5
+ sse_data = (data) ->
6
+ console.log "sse:",data
7
+ $(".data").html(data.now)
8
+
9
+ ajax_data = (data) ->
10
+ console.log "ajax:",data
11
+ $(".adata").html(data.now)
12
+
13
+
14
+ @ajax = (obj) ->
15
+ console.log "doin ajax"
16
+ $.ajax
17
+ url: "/demo.json"
18
+ type: "GET"
19
+ dataType: "json",
20
+ contentType: "application/json; charset=utf-8",
21
+ success: (data) ->
22
+ ajax_data(data)
23
+ setTimeout (->
24
+ ajax()
25
+ return
26
+ ), 3000
27
+ return
28
+ error: (xhr, ajaxOptions, thrownError) ->
29
+ alert thrownError
30
+ return
31
+
32
+
33
+ $ ->
34
+ console.log "Script Starts..."
35
+ ajax()
36
+ stream = new EventSource("/sse_demo.json")
37
+ stream.addEventListener "message", (event) ->
38
+ sse_data($.parseJSON(event.data))
39
+ return
40
+
41
+
42
+
43
+
@@ -0,0 +1,3 @@
1
+ body {
2
+ background-color: #b0c4de;
3
+ }
@@ -1,4 +1,27 @@
1
+ !!!
2
+ %head
3
+ %title minimal-http-ruby
4
+ %meta{:charset => "utf-8"}/
5
+ %link{:href => "/css/style.css", :rel => "stylesheet", :type => "text/css"}
6
+ %script{:src => "https://code.jquery.com/jquery-2.1.1.min.js"}
7
+ %script{:src => "/script.js"}
1
8
 
2
- okei
9
+ %body
10
+ %h1 Congratulations!
11
+ %hr
12
+ You have installed minimal-http-ruby
13
+ %hr
14
+ Next, please edit file: haml/index.haml to get started!
15
+ %br
16
+ And for css: css/style.css to get started!
17
+ %br
18
+ And for coffeescript: coffee/script.coffee to get started!
19
+ %hr
20
+ Epoch Clock from minimal-http-ruby with AJAX:
21
+ %span.adata
22
+ [this should be updated with AJAX]
23
+ %hr
24
+ Epoch Clock from minimal-http-ruby with SSE:
25
+ %span.data
26
+ [this should be updated with SSE]
3
27
 
4
- #{123}
@@ -1,9 +1,8 @@
1
1
  # encode: UTF-8
2
2
 
3
3
  def json_demo request,args,session,event
4
- puts "OKxxxxxxxxxxx"
5
4
  data={
6
5
  now:Time.now.to_i,
7
6
  }
8
7
  return ["text/json",data]
9
- end
8
+ end
@@ -30,6 +30,9 @@ def minimal_http_server options={}
30
30
  else
31
31
  $http_dir = File.join( Gem.loaded_specs['minimal-http-ruby'].full_gem_path, 'http/')
32
32
  end
33
+ if not File.directory? $http_dir
34
+ puts "Warning: Http-path does not exist? -- Please create and populate!\nYou can use utility 'minimal-http-init.rb'"
35
+ end
33
36
  puts "Serving pages from home directory: '#{$http_dir}'"
34
37
  statuses={ "200" => "OK", "404" => "Not Found", "500" => "Internal Server Error"}
35
38
  Thread.new(options[:http_port],options[:app_name]) do |http_port,http_app|
@@ -44,7 +47,7 @@ def minimal_http_server options={}
44
47
  client_ip= client.peeraddr[2]
45
48
  raw=client.gets
46
49
  if not raw
47
- puts "#{client_ip}:#{client_port} #{Time.now.iso8601} ?? nil request?"
50
+ puts "#{client_ip}:#{client_port} #{Time.now.iso8601} ?? nil request?" if options[:verbose]
48
51
  client.close
49
52
  next
50
53
  end
@@ -84,7 +87,7 @@ def minimal_http_server options={}
84
87
  response=cache[fn]
85
88
  end
86
89
  elsif req[/^\/(.+)\.json$/] and File.file?(fn="#{$http_dir}json#{req.gsub('.json','.rb')}")
87
- req[/\/(.+).json$/]
90
+ req[/\/(.+).json$/]
88
91
  act=$1
89
92
  t=File.mtime(fn)
90
93
  if not prev_t[fn] or prev_t[fn]<t
@@ -130,14 +133,13 @@ def minimal_http_server options={}
130
133
  client.print "Content-Length: #{response.bytesize}\r\n"
131
134
  client.print "Connection: close\r\n"
132
135
  client.print "\r\n"
133
- client.print response
136
+ client.print response
134
137
  else
135
138
  client.print "Expires: -1\r\n"
136
139
  client.print "\r\n"
137
140
  begin
138
141
  my_session=client.peeraddr[1]
139
142
  if not @http_sessions[my_session]
140
- #puts "**************** new port #{my_session}"
141
143
  @http_sessions[my_session]={client_port:client.peeraddr[1],client_ip:client.peeraddr[2] , log_position:0 }
142
144
  end
143
145
  my_event=0
@@ -150,7 +152,7 @@ def minimal_http_server options={}
150
152
  puts "#{e.backtrace[0..2]}"
151
153
  pp e.backtrace
152
154
  response=[{act: :error, msg:"Error executing JSON",alert: "Syntax error '#{e}' in '#{fn}'"}].to_json
153
- end
155
+ end
154
156
  if not response or response==[] or response=={}
155
157
  else
156
158
  client.print "retry: 1000\n"
@@ -159,13 +161,15 @@ def minimal_http_server options={}
159
161
  sleep 1
160
162
  break if my_event>100
161
163
  end
164
+ rescue Errno::EPIPE
165
+ #quite normal ;)
162
166
  rescue => e
163
167
  puts "stream #{client} died #{e}"
164
168
  pp e.backtrace
165
169
  end
166
170
  end
167
171
  dur=sprintf "%.2f",(Time.now.to_f-@start.to_f)
168
- puts "#{client_ip}:#{client_port} #{Time.now.iso8601} \"#{method} #{req}\" #{status} #{response.bytesize} \"#{type}\" #{dur}"
172
+ puts "#{client_ip}:#{client_port} #{Time.now.iso8601} \"#{method} #{req}\" #{status} #{response.bytesize} \"#{type}\" #{dur}" if options[:verbose]
169
173
  client.close
170
174
  rescue Exception =>e
171
175
  response="Error '#{e}'"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minimal-http-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ari Siitonen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-09 00:00:00.000000000 Z
11
+ date: 2014-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: haml
@@ -55,15 +55,16 @@ description: 'Minimal Http Server Class with Ryby: Haml & Coffeescript & SSE & A
55
55
  email: jalopuuverstas@gmail.com
56
56
  executables:
57
57
  - minimal-http-init.rb
58
+ - minimal-http.rb
58
59
  extensions: []
59
60
  extra_rdoc_files: []
60
61
  files:
61
62
  - bin/minimal-http-init.rb
63
+ - bin/minimal-http.rb
62
64
  - examples/server.rb
63
- - http/coffee/test.coffee
64
- - http/css/test.css
65
+ - http/coffee/script.coffee
66
+ - http/css/style.css
65
67
  - http/haml/index.haml
66
- - http/index.html
67
68
  - http/json/demo.rb
68
69
  - http/json/sse_demo.rb
69
70
  - lib/minimal-http-ruby.rb
@@ -1,113 +0,0 @@
1
-
2
- now=0
3
- xx=123
4
-
5
- update_status = (data) ->
6
- console.log data.jes
7
- console.log data.clients
8
- #console.log data.logpos
9
- now=data.now
10
- html="<table><tr><th>uri</th><th>state</th><th>stamp</th><th>last_send</th><th>counter_send</th><th>last_recv</th><th>counter_recv</th></tr>"
11
- for k,v of data.clients
12
- html+="<tr>"
13
- html+="<td>#{k}</td>"
14
- html+="<td>#{v.state}</td>"
15
- html+="<td>#{now-v.stamp}</td>"
16
-
17
-
18
-
19
- if v.last_send
20
- html+="<td>#{now-v.last_send}</td>"
21
- else
22
- html+="<td></td>"
23
- html+="<td>#{v.counter_send}</td>"
24
-
25
- if v.last_recv
26
- html+="<td>#{now-v.last_recv}</td>"
27
- else
28
- html+="<td></td>"
29
- html+="<td>#{v.counter_recv}</td>"
30
-
31
- html+="</tr>"
32
- html+="</table>"
33
- #console.log html
34
- $(".clients").html(html)
35
-
36
- html="<table><tr><th>gw_id</th><th>uri</th><th>source</th><th>stamp</th><th>last_use</th><th>last_ping</th><th>last_send</th><th>counter_send</th><th>last_recv</th><th>counter_recv</th></tr>"
37
- for k,v of data.gateways
38
- k=parseInt(k,10)
39
- col="white"
40
- if data.active_gw_id== k
41
- col="#d0ffd0"
42
- html+="<tr bgcolor='#{col}'>"
43
- html+="<td>#{k}</td>"
44
- html+="<td>#{v.uri}</td>"
45
- html+="<td>#{v.source}</td>"
46
- html+="<td>#{now-v.stamp}</td>"
47
- if v.last_use
48
- html+="<td>#{now-v.last_use}</td>"
49
- else
50
- html+="<td></td>"
51
- if v.last_ping
52
- html+="<td>#{now-v.last_ping}</td>"
53
- else
54
- html+="<td></td>"
55
-
56
- if v.last_send
57
- html+="<td>#{now-v.last_send}</td>"
58
- else
59
- html+="<td></td>"
60
- html+="<td>#{v.counter_send}</td>"
61
-
62
- if v.last_recv
63
- html+="<td>#{now-v.last_recv}</td>"
64
- else
65
- html+="<td></td>"
66
- html+="<td>#{v.counter_recv}</td>"
67
- html+="</tr>"
68
- if data.jes[1]==0
69
- $(".log").html("")
70
- for l in data.loglines
71
- #console.log l
72
- $(".log").prepend(l.text+"\n")
73
- html+="</table>"
74
- #console.log html
75
- $(".data").html(html)
76
- $(".info").html("State: #{data.state}, gw: #{data.active_gw_id}, app: #{data.options.app_name}, ")
77
-
78
-
79
- @ajax = (obj) ->
80
- console.log "doin ajax"
81
- form=$(obj).closest("form")
82
- key=form.attr('id')
83
- q=$( form ).serialize()
84
- console.log q
85
- $.ajax
86
- url: "/action.json?#{q}"
87
- type: "GET"
88
- processData: false
89
- contentType: false
90
- success: (data) ->
91
- console.log "ajax returns: ", data
92
-
93
- return
94
- error: (xhr, ajaxOptions, thrownError) ->
95
- alert thrownError
96
- return
97
-
98
-
99
- $ ->
100
- console.log "tadaa"
101
- #update_gw()
102
- #setInterval(->
103
- # update_gw()
104
- # return
105
- #, 200000)
106
- stream = new EventSource("/gateways.json")
107
- stream.addEventListener "message", (event) ->
108
- update_status($.parseJSON(event.data))
109
- return
110
-
111
-
112
-
113
-
File without changes
@@ -1 +0,0 @@
1
- oukei