Skip to content

osyoyu/nuhttp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NuHttp

A new (nu) and compact HTTP server library for Ruby.

Warning

I'm still playing around with very basic API design. Everything is subject to change without notice.

Features

  • Compact API. Good for small-sized apps on the edge or micro-servers embedded in tools.
  • Some typing support. NuHttp API is (or should be) mostly typed. params also recieve types based on path patterns.
  • Ractor mode. Explore true concurrency!

To be implemented: Performance, profiling, benchmarking

Usage

NuHttp apps are defined in NuHttp.app blocks.

require 'nuhttp'
require 'nuhttp/server'

world = "World"

App = NuHttp.app do |b|
  b.get '/' do |c|
    c.res.body = "Hello, #{world}!\n"
  end

  b.get '/json' do |c|
    c.json({ message: "Hello, #{world}!" })
  end

  b.get '/users/:id' do |c|
    user_id = c.req.params[:id]
    c.json({ user_id: user_id })
  end
  
  b.get '/html' do |c|
    c.html("<!DOCTYPE html><html> ... </html>")
  end
  
  b.get '/erb' do |c|
    # HTML+ERB support by Herb (https://github.com/marcoroth/herb)
    # `herb` gem must be installed to use c.erb
    c.erb("/path/to/view.html.erb", { my_variable: 1 })
  end
end

NuHttp::Server.new(App).start

As an Rack app

While NuHttp ships its own HTTP server, NuHttp apps may be mounted on other servers that implement the Rack spec. Common choices are Puma, Unicorn, and Falcon.

Define a config.ru file:

# config.ru

App = NuHttp.app do |b|
  b.get '/' do |c|
    # ...
  end
end

run NuHttp::RackAdapter.new(App)

... then run the server of your choice.

$ puma

Typing support on c.req.params

Steep users may benefit from generated type signatures for c.req.params.

Run nuhttp-typegen to scan *.rb files and generate types.

nuhttp-typegen > sig/app.rbs

Ractor Mode! (Ruby 4.0+)

Accept the challenge of making your app Ractor shareable! The HTTP server shipped with NuHttp spawns a new Ractor for each request, allowing requests to be served in a truly parallel manner.

Note: Ractor mode requires Ractor.shareable_proc, a Ruby 4.0+ API (Feature #21157).

Define your app usingNuHttp.ractor_app and start the server with NuHttp::Server.new(app, ractor_mode: true).start:

require 'nuhttp'
require 'nuhttp/server'

App = NuHttp.ractor_app do |b|
  # Think of each handlerbeing executed in Ractor.new { }
  b.get '/' do |c|
    # ...
  end
end

NuHttp::Server.new(App, ractor_mode: true).start

Unfortunately, many restrictions are applied on what can be done in Ractor-mode handlers, which are run inside Ractor.new { }. You should read ractor.md in the Ruby docs for a good understanding of Ractors.

Most libraries can't be used in Ractor mode. If you need a MySQL client, Trilogy is known to be Ractor-safe.

FAQ

How is this different from Sinatra?

One key difference is the context where the handler is evaluated.

In Sinatra, this code raises:

x = "world"
class App < Sinatra::Base
  get '/' do
    "hello, #{x}"
  end
end
NameError: undefined local variable or method 'x' for an instance of App (NameError)

    "hello, #{x}"
              ^

Installation

Install the gem and add to the application's Gemfile by executing:

bundle add nuhttp

If bundler is not being used to manage dependencies, install the gem by executing:

gem install nuhttp

Development

After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.

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 the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/osyoyu/nuhttp.

License

The gem is available as open source under the terms of the MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published