Skip to content

Commit

Permalink
error pages and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jwaldrip committed Jun 21, 2020
1 parent 38472d0 commit 27dfac9
Show file tree
Hide file tree
Showing 35 changed files with 450 additions and 242 deletions.
58 changes: 40 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,58 @@

Orion is minimal, Omni-Conventional, declarative web framework inspired by the ruby-on-rails router and controller components. It provides, the routing, view, and controller framework of your application in a way that can be as simple or complex as you need it to fit your use case.

## Conventions
## Simple
Orion out of the box is designed to be as simple as you want it to be. A few
lines will get you a functioning web app. Orion also ships with helpful features
such as view rendering and static content delivery.

### App Mode
```crystal
require "orion/app"
In it's simplest form you can use Orion in app mode. Here you can use a variety of verbs and helpers. Each block will create its controller and have all the methods of `Orion::BaseController` and will include an URL helpers you have defined within the application.
static "/", dir: "/public"
root do
render "views/home.slim"
end
get "/login" do
render "
end
```

## Flexible

```crystal
require "orion/app"
require "auth_handlers"
scope "/api" do
use AuthHandlers::Token.new
end
use HTTP::LogHandler.new
use SessionAuthHandler.new
root "/" do
"Hello World"
scope constraint: UnauthenticatedUser do
root do
render "views/home.slim"
end
get "/posts" do
@posts = Post.all
case format
when .html?
render "views/post.ecr"
when .json?
@posts.to_json
get "/login", helper: login do
render "views/login.slim"
end
post "/login" do
if User.authenticate(params["email"], params["password])
redirect to: root_path
else
respond_with_status 404
flash[:error] = "Invalid login"
redirect to: login_path
end
end
ws "/echo" do
websocket.on_message do |message|
websocket.send message
scope constraint: AuthenticatedUser do
root do
render "views/dashboard.slim"
end
end
```
```
4 changes: 4 additions & 0 deletions examples/base.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
require "../src/app"

root do
raise "Oops"
end

scope "/foo" do
scope "/bar" do
root do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "../../spec_helper"

module Router::ConcernsSpec
module Orion::DSL::ConcernsSpec
router SampleRouter do
concern :messagable do
get "messages/new", ->(c : Context) { c.response.print "lets send a message" }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "../../spec_helper"

module Router::ConstraintsSpec
module Orion::DSL::ConstraintsSpec
class TestConstraint
include Orion::Constraint

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "../../spec_helper"

module Router::HandlersSpec
module Orion::DSL::HandlersSpec
class AppendHandler
include HTTP::Handler

Expand Down Expand Up @@ -36,14 +36,14 @@ module Router::HandlersSpec
response.body.should eq "My name is Rocket, and I am NOT a racoon, and I am a guardian"
end

# it "should run handlers on unmatched routes" do
# response = test_route(SampleRouter.new, :get, "/unmatched")
# response.body.should eq "404 Not Found\n, and I am a guardian"
# end
it "should run handlers on unmatched routes" do
response = test_route(SampleRouter.new, :get, "/unmatched")
response.body.should eq "404 Not Found\n, and I am a guardian"
end

# it "should run handlers on unmatched group routes" do
# response = test_route(SampleRouter.new, :get, "/scoped/unmatched")
# response.body.should eq "404 Not Found\n, and I am NOT a racoon, and I am a guardian"
# end
it "should run handlers on unmatched group routes" do
response = test_route(SampleRouter.new, :get, "/scoped/unmatched")
response.body.should eq "404 Not Found\n, and I am NOT a racoon, and I am a guardian"
end
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "../../spec_helper"

module Router::HelpersSpec
module Orion::DSL::HelpersSpec
c = ->(c : HTTP::Server::Context) {}

router SampleRouter do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "../../spec_helper"

module Router::MatchSpec
module Orion::DSL::MatchSpec
router SampleRouter do
match "/callable", ->(c : Context) { c.response.print "callable match" }
match "/block" do |c|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "../../spec_helper"

module Router::MethodsSpec
module Orion::DSL::MethodsSpec
{% for method in ::Orion::DSL::RequestMethods::METHODS %}
module {{ method.capitalize.id }}
router SampleRouter do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "../../spec_helper"

module Router::Resources::Spec
module Orion::DSL::Resources::Spec
router SampleRouter do
resources :users do
get "profile", action: profile
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "../../spec_helper"

module Router::ScopeSpec
module Orion::DSL::ScopeSpec
router SampleRouter do
get "home", ->(c : Context) { c.response.print c.request.base_path }
scope "messages" do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "../../spec_helper"

module Router::WebSocketsSpec
module Orion::DSL::WebSocketsSpec
router SampleRouter do
ws "/match", ->(ws : WebSocket, c : Context) {
ws.send("Match")
Expand Down
8 changes: 0 additions & 8 deletions src/macro.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ macro router(name)
module {{ name }}
include Orion::DSL

def self.new(*args, **opts)
::Orion::Router.new(TREE, *args, **opts)
end

def self.start(*args, **opts)
::Orion::Router.start(TREE, *args, **opts)
end

{{ yield }}
end
end
4 changes: 3 additions & 1 deletion src/orion/config.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require "socket"
require "openssl"

# These are the options available when setting properties with the `config`
# method within your application.
struct Orion::Config
property socket : ::Socket::Server?
property uri : String | URI | Nil
property port : Int32? = 3000
property port : Int32? = 4000
property path : String?
property address : ::Socket::IPAddress | ::Socket::UNIXAddress | Nil
property tls : ::OpenSSL::SSL::Context::Server?
Expand Down
57 changes: 57 additions & 0 deletions src/orion/controller.cr
Original file line number Diff line number Diff line change
@@ -1 +1,58 @@
# The `Orion::Controller` module can be included in any struct or class to add
# the various helpers methods to make constructing your application easier.
module Orion::Controller
@layout_rendered = false
getter context : ::HTTP::Server::Context
getter! websocket : ::HTTP::WebSocket
delegate request, response, to: @context

private macro layout(filename)
private def render_layout(&block): Nil
if @layout_rendered
raise ::Orion::DoubleRenderError.new "cannot call render view more than once"
end
@layout_rendered = true
Kilt.embed "src/views/layouts/{{ filename.id }}"
end
end

private macro render(*, view, layout = true)
{% if layout %}
render_layout do
Kilt.embed "src/views/{{ view.id }}"
nil
end
{% else %}
Kilt.embed "src/views/{{ view.id }}"
nil
{% end %}
end

private macro render(*, partial)
Kilt.embed "src/views/partials/{{ partial.id }}"
nil
end

private macro render(*, json)
{{ json }}.to_json(response)
nil
end

private macro render(*, text)
response.puts({{ text }})
nil
end

def initialize(@context, @websocket = nil)
end

private def render_layout(&block)
yield
end

private def __kilt_io__
response
end
end

require "./controller/*"
4 changes: 2 additions & 2 deletions src/orion/controller/base.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "./helper"
# :nodoc:
abstract class Orion::Controller::Base
include Helper
include Orion::Controller
end
54 changes: 0 additions & 54 deletions src/orion/controller/helper.cr

This file was deleted.

28 changes: 17 additions & 11 deletions src/orion/dsl.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,29 @@ module Orion::DSL
TREE = Tree.new
# :nodoc:
CONTROLLER = BaseController
# :nodoc:
ORION_CONFIG = ::Orion::Config.new

{% if @type.stringify == "<Program>" %}
# :nodoc:
ORION_CONFIG = ::Orion::Config.new
def config
ORION_CONFIG
end

def config
ORION_CONFIG
end
def self.new(*args, **opts)
::Orion::Router.new(TREE, *args, **opts)
end

macro finished
def self.start(*args, **opts)
::Orion::Router.start(TREE, *args, **opts)
end

macro finished
{% if @type.stringify == "<Program>" %}
::Orion::Router.start(TREE, config: config)
end
{% end %}
{% end %}
match "*", ::Orion::Handlers::NotFound.new
end

include ::Orion::DSL::Macros

match "*", ::Orion::Handlers::NotFound.new
end

# :nodoc:
Expand Down
Loading

0 comments on commit 27dfac9

Please sign in to comment.