Runcobo
Runcobo is an api framework in Crystal.
Philosophy
- Simple Design must be simple, both in implementation and interface.
- Intuitive Design must be intuitive.
- Consistent Design must be consistent.
Installation
- Add the dependency to your
shard.yml
:
dependencies:
runcobo:
github: runcobo/runcobo
This shard follows Semantic Versioning v2.0.0, so check releases and change the version accordingly.
- Run
shards install
Usage
require "runcobo"
class Api::V1::Add < BaseAction
get "/api/v1/add"
query NamedTuple(a: Int32, b: Int32)
call do
sum = params[:a] + params[:b]
render_plain sum.to_s
end
end
Runcobo.start
Api
class BaseAction
# Route
def self.get(url : String)
def self.post(url : String)
def self.put(url : String)
def self.patch(url : String)
def self.delete(url : String)
def self.options(url : String)
def self.head(url : String)
def self.route(method : String, url : String)
# Params Definition
macro url(named_tuple : NamedTuple.class)
macro query(named_tuple : NamedTuple.class)
macro form(named_tuple : NamedTuple.class)
macro json(named_tuple : NamedTuple.class)
# Params Call
def params : NamedTuple
# Call
macro call(&block) : HTTP::Server::Context
macro layout(filename : String)
macro before(method_name : Crystal::Macros::MacroId)
macro after(method_name : Crystal::Macros::MacroId)
macro skip(method_name : Crystal::Macros::MacroId)
# Render View
def render_plain(text : String, *, statu_code :Int32 = 200) : HTTP::Server::Context
def render_body(body : String, *, statu_code : Int32 = 200) : HTTP::Server::Context
macro render_water(filename : String, *, layout = "", status_code = Int32, dir = "src/views/", layout_dir = "src/views/layouts/") : HTTP::Server::Context
macro render_jbuilder(filename : String, *, layout = "", status_code = Int32, dir = "src/views/", layout_dir = "src/views/layouts/") : HTTP::Server::Context
end
module Runcobo
# Start Server
def self.start(*,
host : String = ENV["HOST"]? || "0.0.0.0",
port : Int32 = (ENV["PORT"]? || 3000).to_i,
reuse_port : Bool = false,
handlers : Array(HTTP::Handler) = Runcobo::Server.default_handlers,
cert : String? = ENV["CERT"]?,
key : String? = ENV["KEY"]?)
end
Contributing
- Fork it (<https://github.com/runcobo/runcobo/fork>)
- Create your feature branch (
git checkout -b my-new-feature
) - Write and execute specs (
crystal spec
) and formatting checks (crystal tool format
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- Shootingfly - creator and maintainer