sorta-http 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +43 -0
- data/LICENSE.txt +21 -0
- data/README.md +38 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/examples/simple.rb +34 -0
- data/lib/sorta/http.rb +17 -0
- data/lib/sorta/http/logger.rb +27 -0
- data/lib/sorta/http/server.rb +50 -0
- data/lib/sorta/http/version.rb +7 -0
- data/lib/sorta/http/web/action.rb +33 -0
- data/lib/sorta/http/web/app.rb +61 -0
- data/lib/sorta/http/web/params_validator.rb +37 -0
- data/lib/sorta/http/web/router.rb +44 -0
- data/lib/sorta/http/web/template/app.rb +36 -0
- data/lib/sorta/http/web/template/router.rb +40 -0
- data/lib/sorta/http/worker.rb +88 -0
- data/sorta-http.gemspec +29 -0
- data/test.rb +38 -0
- metadata +70 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f864c42253b9c913a75ad7c9208931b0827d4c94cae8664e44e232755cc3967f
|
4
|
+
data.tar.gz: 72b8c23f6043822d7d4554b3c3b133974264cd13b5d0118997d44486acc4b45e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5af95cf516c9d83af32676271f0ab12673fe25f06288fced9a0e49f9ceff3a7723718a62ffea5d1e51c4e99f894474664a8cda0ce0ae091585b65241b8cda3da
|
7
|
+
data.tar.gz: 7faa923792fb58f15f70e6f043eeae7f7817f9284310f767bdf7a56da460d958a42195b37b69895106626a369a23a4732825c38115cda96f5129eb38cfdb9bbf
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
GIT
|
2
|
+
remote: https://github.com/Odebe/radix
|
3
|
+
revision: 168ab744c9970a21b048d3e0062bc49f947e244c
|
4
|
+
specs:
|
5
|
+
radix (0.4.1)
|
6
|
+
|
7
|
+
PATH
|
8
|
+
remote: .
|
9
|
+
specs:
|
10
|
+
sorta-http (0.1.0)
|
11
|
+
|
12
|
+
GEM
|
13
|
+
remote: https://rubygems.org/
|
14
|
+
specs:
|
15
|
+
diff-lcs (1.4.4)
|
16
|
+
rack (2.2.3)
|
17
|
+
rake (12.3.3)
|
18
|
+
rspec (3.10.0)
|
19
|
+
rspec-core (~> 3.10.0)
|
20
|
+
rspec-expectations (~> 3.10.0)
|
21
|
+
rspec-mocks (~> 3.10.0)
|
22
|
+
rspec-core (3.10.1)
|
23
|
+
rspec-support (~> 3.10.0)
|
24
|
+
rspec-expectations (3.10.1)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.10.0)
|
27
|
+
rspec-mocks (3.10.2)
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
+
rspec-support (~> 3.10.0)
|
30
|
+
rspec-support (3.10.2)
|
31
|
+
|
32
|
+
PLATFORMS
|
33
|
+
x86_64-linux
|
34
|
+
|
35
|
+
DEPENDENCIES
|
36
|
+
rack
|
37
|
+
radix!
|
38
|
+
rake (~> 12.0)
|
39
|
+
rspec (~> 3.0)
|
40
|
+
sorta-http!
|
41
|
+
|
42
|
+
BUNDLED WITH
|
43
|
+
2.2.15
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Mihail Odebe
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# sorta-http
|
2
|
+
|
3
|
+
Naive HTTP server on ruby 3.0's ractors with toy framework.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'sorta-http', git: 'https://github.com/Odebe/sorta-http'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
[comment]: <> (Or install it yourself as:)
|
18
|
+
|
19
|
+
[comment]: <> ( $ gem install racktor)
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
See examples
|
24
|
+
|
25
|
+
## Development
|
26
|
+
|
27
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
28
|
+
|
29
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/racktor.
|
34
|
+
|
35
|
+
|
36
|
+
## License
|
37
|
+
|
38
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "racktor"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/examples/simple.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative '../lib/sorta/http.rb'
|
2
|
+
|
3
|
+
HOST = 'localhost'
|
4
|
+
PORT = 8080
|
5
|
+
WORKERS_COUNT = 1
|
6
|
+
|
7
|
+
hello_schema = Sorta::Http::Web::ParamsValidator.build do
|
8
|
+
param :name, type: String
|
9
|
+
end
|
10
|
+
|
11
|
+
calc_schema = Sorta::Http::Web::ParamsValidator.build do
|
12
|
+
param :num1, type: Integer
|
13
|
+
param :num2, type: Integer
|
14
|
+
end
|
15
|
+
|
16
|
+
app = Sorta::Http::Web::Template::App.build do
|
17
|
+
routes do
|
18
|
+
get '/', to: Actions::Root
|
19
|
+
get '/hello/:name', to: Actions::Hello, schema: hello_schema
|
20
|
+
|
21
|
+
namespace :calc do
|
22
|
+
namespace :minus do
|
23
|
+
get '/:num1/:num2', to: Actions::Calc::Minus, schema: calc_schema
|
24
|
+
end
|
25
|
+
|
26
|
+
namespace :plus do
|
27
|
+
get '/:num1/:num2', to: Actions::Calc::Plus, schema: calc_schema
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
server = Sorta::Http::Server.new(app,host: HOST, port: PORT, workers: WORKERS_COUNT, logger: Sorta::Http::Logger.new)
|
34
|
+
server.run
|
data/lib/sorta/http.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'rack'
|
3
|
+
require 'radix'
|
4
|
+
|
5
|
+
require_relative "./http/version.rb"
|
6
|
+
require_relative "./http/logger.rb"
|
7
|
+
require_relative "./http/server.rb"
|
8
|
+
require_relative "./http/worker.rb"
|
9
|
+
|
10
|
+
require_relative "./http/web/app.rb"
|
11
|
+
|
12
|
+
module Sorta
|
13
|
+
module Http
|
14
|
+
class Error < StandardError; end
|
15
|
+
# Your code goes here...
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
module Sorta
|
4
|
+
module Http
|
5
|
+
# https://mensfeld.pl/2020/09/building-a-ractor-based-logger-that-will-work-with-non-ractor-compatible-code/
|
6
|
+
class Logger < Ractor
|
7
|
+
def self.new
|
8
|
+
super do
|
9
|
+
logger = ::Logger.new($stdout)
|
10
|
+
|
11
|
+
loop do
|
12
|
+
data = recv
|
13
|
+
logger.public_send(data[0], *data[1])
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
[:error, :info, :debug].each do |level|
|
19
|
+
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
20
|
+
def #{level}(*args)
|
21
|
+
self << [:#{level}, *args]
|
22
|
+
end
|
23
|
+
RUBY
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'socket'
|
4
|
+
require 'rack'
|
5
|
+
require 'etc'
|
6
|
+
|
7
|
+
module Sorta
|
8
|
+
module Http
|
9
|
+
class Server
|
10
|
+
CPU_COUNT = Etc.nprocessors
|
11
|
+
|
12
|
+
Ractor.make_shareable(Rack::VERSION)
|
13
|
+
|
14
|
+
def initialize(app, **options)
|
15
|
+
@app = app
|
16
|
+
@options = options
|
17
|
+
@cpu_count = options[:workers] || CPU_COUNT
|
18
|
+
@port = options[:port] || 8080
|
19
|
+
@host = options[:host] || 'localhost'
|
20
|
+
@logger = options[:logger] || Logger.new
|
21
|
+
|
22
|
+
init_pipe
|
23
|
+
init_workers
|
24
|
+
# TODO: init_supervisor
|
25
|
+
end
|
26
|
+
|
27
|
+
def run
|
28
|
+
tcp_server = TCPServer.new(@host, @port)
|
29
|
+
loop { @pipe.send(tcp_server.accept, move: true) }
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def init_pipe
|
35
|
+
@pipe = Ractor.new do
|
36
|
+
loop { Ractor.yield(Ractor.receive, move: true) }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def init_workers
|
41
|
+
@workers ||= @cpu_count.times.map do
|
42
|
+
Ractor.new(@app, @pipe, @logger) do |app, pipe, logger|
|
43
|
+
app = app.compile! if app.respond_to? :compile!
|
44
|
+
Worker.new(pipe, app, logger: logger).run
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sorta
|
4
|
+
module Http
|
5
|
+
module Web
|
6
|
+
module Action
|
7
|
+
def self.included(mod)
|
8
|
+
mod.prepend PrependMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
module PrependMethods
|
12
|
+
def initialize(env)
|
13
|
+
@env = env
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(params = [])
|
17
|
+
result = params.any? ? super(params) : super()
|
18
|
+
[200, {}, [result]]
|
19
|
+
rescue => e
|
20
|
+
[500, {}, [e.message, e.backtrace.join("\n")]]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
# TODO: передавать как зависимость
|
27
|
+
def logger
|
28
|
+
Ractor.current[:logger]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative './action.rb'
|
4
|
+
require_relative './router.rb'
|
5
|
+
require_relative './params_validator.rb'
|
6
|
+
|
7
|
+
require_relative './template/app.rb'
|
8
|
+
|
9
|
+
module Sorta
|
10
|
+
module Http
|
11
|
+
module Web
|
12
|
+
class App
|
13
|
+
def self.build_from(template)
|
14
|
+
dependencies = [
|
15
|
+
Router.build_from(template.router)
|
16
|
+
]
|
17
|
+
new(*dependencies)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.build(&block)
|
21
|
+
app = new
|
22
|
+
app.instance_exec(&block)
|
23
|
+
app
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize(router = Router.new)
|
27
|
+
@router = router
|
28
|
+
end
|
29
|
+
|
30
|
+
def call(env)
|
31
|
+
result = @router.find(env)
|
32
|
+
|
33
|
+
unless result.found?
|
34
|
+
msg = "Cannot find #{env['REQUEST_METHOD']} action for route \"#{env['PATH_INFO']}\""
|
35
|
+
logger.error msg
|
36
|
+
return [404, {}, [msg]]
|
37
|
+
end
|
38
|
+
|
39
|
+
params = result.params
|
40
|
+
params = result.payload[:schema].call(params) unless result.payload[:schema].nil?
|
41
|
+
response = result.payload[:action].new(env).call(params)
|
42
|
+
logger.info "#{response[0]} #{env['REQUEST_METHOD']} \"#{env['PATH_INFO']}\" #{result.payload[:action]} #{params.inspect}: #{response[2].join}"
|
43
|
+
response
|
44
|
+
rescue ParamsValidator::ValidationError => e
|
45
|
+
logger.info "#{500} #{env['REQUEST_METHOD']} \"#{env['PATH_INFO']}\" #{result.payload[:action]} #{params.inspect}: #{e.message}"
|
46
|
+
[500, {}, [e.message]]
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def logger
|
52
|
+
Ractor.current[:logger]
|
53
|
+
end
|
54
|
+
|
55
|
+
def routes
|
56
|
+
yield @router
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sorta
|
4
|
+
module Http
|
5
|
+
module Web
|
6
|
+
class ParamsValidator
|
7
|
+
class ValidationError < StandardError; end
|
8
|
+
|
9
|
+
def self.build(&block)
|
10
|
+
obj = new
|
11
|
+
obj.instance_exec(&block)
|
12
|
+
obj
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
@rules = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def param(name, type:)
|
20
|
+
@rules[name] = type
|
21
|
+
end
|
22
|
+
|
23
|
+
def call(params)
|
24
|
+
errors = []
|
25
|
+
result = @rules.each_with_object({}) do |(key, klass), acc|
|
26
|
+
acc[key] = Kernel.send(klass.name, params[key.to_s])
|
27
|
+
rescue => e
|
28
|
+
errors << "#{e.message}"
|
29
|
+
end
|
30
|
+
raise ValidationError.new(errors.join("\n")) if errors.any?
|
31
|
+
|
32
|
+
result
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sorta
|
4
|
+
module Http
|
5
|
+
module Web
|
6
|
+
class Router
|
7
|
+
def self.build_from(template)
|
8
|
+
router = new
|
9
|
+
template.routes.each do |route|
|
10
|
+
router.send(route[0], route[1], to: route[2], schema: route[3])
|
11
|
+
end
|
12
|
+
router
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
@tree = Radix::Tree.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def find(env)
|
20
|
+
key = File.join(env['PATH_INFO'], env['REQUEST_METHOD'].downcase)
|
21
|
+
@tree.find key
|
22
|
+
end
|
23
|
+
|
24
|
+
%i[get post put delete].each do |method|
|
25
|
+
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
26
|
+
def #{method}(path, to:, schema: nil)
|
27
|
+
add_route(path, method: :#{method}, to: to, schema: schema)
|
28
|
+
end
|
29
|
+
RUBY
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def add_route(path, method:, to:, schema:)
|
35
|
+
raise 'routing error' if to.nil?
|
36
|
+
|
37
|
+
key = File.join(path, method.to_s.downcase)
|
38
|
+
@tree.add key, { action: to, schema: schema }
|
39
|
+
true
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative './router.rb'
|
4
|
+
|
5
|
+
module Sorta
|
6
|
+
module Http
|
7
|
+
module Web
|
8
|
+
module Template
|
9
|
+
class App
|
10
|
+
attr_reader :router
|
11
|
+
|
12
|
+
def self.build(&block)
|
13
|
+
app = new
|
14
|
+
app.instance_exec(&block)
|
15
|
+
app
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize
|
19
|
+
@router = Router.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def compile!
|
23
|
+
::Sorta::Http::Web::App.build_from(self)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def routes(&block)
|
29
|
+
@router.instance_exec(&block)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sorta
|
4
|
+
module Http
|
5
|
+
module Web
|
6
|
+
module Template
|
7
|
+
class Router
|
8
|
+
attr_reader :routes
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@routes = []
|
12
|
+
@prefix = ''
|
13
|
+
end
|
14
|
+
|
15
|
+
%i[get post put delete].each do |method|
|
16
|
+
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
17
|
+
def #{method}(path, to:, schema: nil)
|
18
|
+
@routes << [:#{method}, with_prefix(path), to, schema]
|
19
|
+
end
|
20
|
+
RUBY
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def with_prefix(path)
|
26
|
+
File.join('/', @prefix, path)
|
27
|
+
end
|
28
|
+
|
29
|
+
def namespace(name, &block)
|
30
|
+
old_prefix = @prefix
|
31
|
+
@prefix = with_prefix(name.to_s)
|
32
|
+
instance_exec(&block)
|
33
|
+
ensure
|
34
|
+
@prefix = old_prefix
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
module Sorta
|
6
|
+
module Http
|
7
|
+
class Worker
|
8
|
+
STATUS_CODES = { 200 => 'OK', 500 => 'Internal Server Error' }.freeze
|
9
|
+
|
10
|
+
attr_reader :pipe, :app
|
11
|
+
|
12
|
+
def initialize(pipe, app, logger:)
|
13
|
+
@pipe = pipe
|
14
|
+
@app = app
|
15
|
+
|
16
|
+
Ractor.current[:logger] = logger
|
17
|
+
end
|
18
|
+
|
19
|
+
def logger
|
20
|
+
Ractor.current[:logger]
|
21
|
+
end
|
22
|
+
|
23
|
+
def run
|
24
|
+
loop do
|
25
|
+
socket = pipe.take
|
26
|
+
request = socket.gets
|
27
|
+
|
28
|
+
if request.nil?
|
29
|
+
socket.close
|
30
|
+
next
|
31
|
+
end
|
32
|
+
|
33
|
+
env = new_env(*request.split)
|
34
|
+
# logger.info "#{env['REQUEST_METHOD']} #{env['PATH_INFO']}"
|
35
|
+
status, headers, body =
|
36
|
+
begin
|
37
|
+
app.call(env)
|
38
|
+
rescue => e
|
39
|
+
# TODO: сделать нормальную обработку ошибок
|
40
|
+
logger.error "#{e.message}\n#{e.backtrace.join("\n")}"
|
41
|
+
[500, {}, [e.message]]
|
42
|
+
end
|
43
|
+
|
44
|
+
socket.print "HTTP/1.1 #{status} #{STATUS_CODES[status]}\r\n"
|
45
|
+
headers.each do |k, v|
|
46
|
+
socket.print "#{k}: #{v}\r\n"
|
47
|
+
end
|
48
|
+
socket.print "Connection: close\r\n"
|
49
|
+
socket.print "\r\n"
|
50
|
+
|
51
|
+
if body.is_a?(String)
|
52
|
+
socket.print body
|
53
|
+
else
|
54
|
+
body.each do |chunk|
|
55
|
+
socket.print chunk
|
56
|
+
end
|
57
|
+
end
|
58
|
+
rescue => e
|
59
|
+
puts e
|
60
|
+
logger.error "#{e.message}\n#{e.backtrace.join("\n")}"
|
61
|
+
ensure
|
62
|
+
socket.close
|
63
|
+
next
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def new_env(method, location, *args)
|
70
|
+
{
|
71
|
+
'REQUEST_METHOD' => method,
|
72
|
+
'SCRIPT_NAME' => '',
|
73
|
+
'PATH_INFO' => location,
|
74
|
+
'QUERY_STRING' => location.split('?').last,
|
75
|
+
'SERVER_NAME' => 'localhost',
|
76
|
+
'SERVER_POST' => '8080',
|
77
|
+
'rack.version' => ::Rack.version.split('.'),
|
78
|
+
'rack.url_scheme' => 'http',
|
79
|
+
'rack.input' => StringIO.new(''),
|
80
|
+
'rack.errors' => StringIO.new(''),
|
81
|
+
'rack.multithread' => false,
|
82
|
+
'rack.run_once' => false
|
83
|
+
}
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
data/sorta-http.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative 'lib/sorta/http/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "sorta-http"
|
5
|
+
spec.version = Sorta::Http::VERSION
|
6
|
+
spec.authors = ["Mihail Odebe"]
|
7
|
+
spec.email = ["derpiranha@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Write a short summary, because RubyGems requires one.}
|
10
|
+
spec.description = %q{Write a longer description or delete this line.}
|
11
|
+
spec.homepage = "https://github.com/Odebe/sorta-http"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 3.0.0")
|
14
|
+
|
15
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/Odebe/sorta-http"
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/Odebe/sorta-http"
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib/sorta"]
|
29
|
+
end
|
data/test.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
r = Ractor.new do
|
4
|
+
loop do
|
5
|
+
a = Ractor.receive
|
6
|
+
puts a.inspect
|
7
|
+
puts YAML.load(a)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Actor < Ractor
|
12
|
+
def self.new()
|
13
|
+
super() do
|
14
|
+
yield
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class A
|
20
|
+
def initialize
|
21
|
+
@aaa = 1
|
22
|
+
@aab = [1,2,3]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
a = A.new
|
27
|
+
|
28
|
+
begin
|
29
|
+
puts "#send #{a}: "
|
30
|
+
r.send(YAML.dump(a), move: true)
|
31
|
+
puts 'done'
|
32
|
+
rescue => e
|
33
|
+
puts e.message
|
34
|
+
end
|
35
|
+
|
36
|
+
puts "end"
|
37
|
+
|
38
|
+
sleep
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sorta-http
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mihail Odebe
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-06-02 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Write a longer description or delete this line.
|
14
|
+
email:
|
15
|
+
- derpiranha@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".rspec"
|
22
|
+
- ".travis.yml"
|
23
|
+
- Gemfile
|
24
|
+
- Gemfile.lock
|
25
|
+
- LICENSE.txt
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- bin/console
|
29
|
+
- bin/setup
|
30
|
+
- examples/simple.rb
|
31
|
+
- lib/sorta/http.rb
|
32
|
+
- lib/sorta/http/logger.rb
|
33
|
+
- lib/sorta/http/server.rb
|
34
|
+
- lib/sorta/http/version.rb
|
35
|
+
- lib/sorta/http/web/action.rb
|
36
|
+
- lib/sorta/http/web/app.rb
|
37
|
+
- lib/sorta/http/web/params_validator.rb
|
38
|
+
- lib/sorta/http/web/router.rb
|
39
|
+
- lib/sorta/http/web/template/app.rb
|
40
|
+
- lib/sorta/http/web/template/router.rb
|
41
|
+
- lib/sorta/http/worker.rb
|
42
|
+
- sorta-http.gemspec
|
43
|
+
- test.rb
|
44
|
+
homepage: https://github.com/Odebe/sorta-http
|
45
|
+
licenses:
|
46
|
+
- MIT
|
47
|
+
metadata:
|
48
|
+
homepage_uri: https://github.com/Odebe/sorta-http
|
49
|
+
source_code_uri: https://github.com/Odebe/sorta-http
|
50
|
+
changelog_uri: https://github.com/Odebe/sorta-http
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib/sorta
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: 3.0.0
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubygems_version: 3.2.15
|
67
|
+
signing_key:
|
68
|
+
specification_version: 4
|
69
|
+
summary: Write a short summary, because RubyGems requires one.
|
70
|
+
test_files: []
|