Skip to content

Commit

Permalink
Merge pull request #44 from aristotelesbr/development
Browse files Browse the repository at this point in the history
Main < Development
  • Loading branch information
aristotelesbr authored May 3, 2024
2 parents 9a9eea8 + 42960de commit 3f85242
Show file tree
Hide file tree
Showing 9 changed files with 408 additions and 155 deletions.
145 changes: 119 additions & 26 deletions guides/getting-started/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,60 +44,153 @@ end
run app
```

## Use `Lennarb::Router` module
## Use `Lennarb::Application::Base` class

You can use the `Lennarb::Router` module to define routes using the `route` method:
You can also use the `Lennarb::Application::Base` class to define your app:

```ruby
# app.rb
# config.ru

require 'lennarb'

class App
include Lennarb::Router
class App < Lennarb::Application::Base
end
```

## Define routes

When you use the `Lennarb::Application::Base` class, you can use the following methods:

- `get`
- `post`
- `put`
- `delete`
- `patch`
- `head`
- `options`

```ruby
# config.ru

require 'lennarb'

route.get '/' do |_req, res|
res.status = 200
class App < Lennarb::Application::Base
get '/' do |req, res|
res.html 'Hello World'
end
end
```

The `route` method is a instance of `Lennarb`. So, you can use the following methods:
run App.new
```

- `route.get`
- `route.post`
- `route.put`
- `route.delete`
- `route.patch`
- `route.options`
- `route.head`
## Run!

Now you can use the `App` class in your `config.ru`:
You can use `run!` method to run your app to use default configurations, like:

```ruby
# config.ru

require_relative 'app'
require 'lennarb'

run App.app
class App < Lennarb::Application::Base
get '/' do |req, res|
res.html 'Hello World'
end
end

App.run!
```

The `app` method freeze the routes and return a `Lennarb` instance.
- Freeze the app routes
- Default middlewares:
- `Lennarb::Middlewares::Logger`
- `Lennarb::Middlewares::Static`
- `Lennarb::Middlewares::NotFound`
- `Lennarb::Middlewares::Lint`L
- `Lennarb::Middlewares::ShowExceptions`
- `Lennarb::Middlewares::MethodOverride`

## Parameters
After that, you can run your app with the `rackup` command:

You can get `params` from the request using `req.params`:
## Callbacks

You can use the `before` and `after` methods to define callbacks:

```ruby
# app.rb
# config.run

require 'lennarb'

class App < Lennarb::Application::Base
before do |req, res|
puts 'Before'
end

route.get '/hello/:name' do |req, res|
res.html "Hello #{req.params[:name]}"
get '/' do |req, res|
res.html 'Hello World'
end

after do |req, res|
puts 'After'
end
end

run App.run!
```

## Run
You can also use the `before` and `after` methods to define callbacks for specific routes:

```ruby
# config.ru

require 'lennarb'

class App < Lennarb::Application::Base
before '/about' do |req, res|
puts 'Before about'
end

get '/about' do |req, res|
res.html 'About'
end

after '/about' do |req, res|
puts 'After about'
end
end
```

## Middlewares

You can use the `use` method to add middlewares:

```ruby

# config.ru

require 'lennarb'

class App < Lennarb::Application::Base
use Lennarb::Middlewares::Logger
use Lennarb::Middlewares::Static

get '/' do |req, res|
res.html 'Hello World'
end
end

run App.run!
```

## Custom 404 page

If you are using `run!` method, you can use the custom 404 page just creating a `404.html` file in the `public` folder.

## Custom 500 page

TODO

## Run your app

```bash
$ rackup
Expand Down
35 changes: 7 additions & 28 deletions lib/lennarb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

# Base class for Lennarb
#
require_relative 'lennarb/application/base'
require_relative 'lennarb/request'
require_relative 'lennarb/response'
require_relative 'lennarb/route_node'
require_relative 'lennarb/router'
require_relative 'lennarb/version'

class Lennarb
Expand All @@ -26,7 +26,7 @@ class LennarbError < StandardError; end
# @attribute [r] root
# @returns [RouteNode]
#
attr_reader :root
attr_reader :_root

# Initialize the application
#
Expand All @@ -35,7 +35,7 @@ class LennarbError < StandardError; end
# @returns [Lennarb]
#
def initialize
@root = RouteNode.new
@_root = RouteNode.new
yield self if block_given?
end

Expand All @@ -58,44 +58,23 @@ def call(env)
http_method = env[Rack::REQUEST_METHOD].to_sym
parts = SplitPath[env[Rack::PATH_INFO]]

block, params = @root.match_route(parts, http_method)
block, params = @_root.match_route(parts, http_method)
return [404, { 'content-type' => 'text/plain' }, ['Not Found']] unless block

@res = Response.new
req = Request.new(env, params)

catch(:halt) do
instance_exec(req, @res, &block)
finish!
@res.finish
end
end

# Finish the request
#
# @returns [Array] Response
#
def finish! = halt(@res.finish)

# Immediately stops the request and returns `response`
# as per Rack's specification.
#
# halt([200, { "Content-Type" => "text/html" }, ["hello"]])
# halt([res.status, res.headers, res.body])
# halt(res.finish)
#
# @parameter [Array] response
#
# @returns [Array] response
#
def halt(response)
throw(:halt, response)
end

# Freeze the routes
#
# @returns [void]
#
def freeze! = @root.freeze
def freeze! = @_root.freeze

# Add a routes
#
Expand Down Expand Up @@ -124,6 +103,6 @@ def options(path, &block) = add_route(path, :OPTIONS, block)
#
def add_route(path, http_method, block)
parts = SplitPath[path]
@root.add_route(parts, http_method, block)
@_root.add_route(parts, http_method, block)
end
end
Loading

0 comments on commit 3f85242

Please sign in to comment.