Skip to content

Commit

Permalink
Add docs about Runcobo
Browse files Browse the repository at this point in the history
  • Loading branch information
shootingfly committed Aug 8, 2020
1 parent 6e3e3df commit 9d460de
Show file tree
Hide file tree
Showing 14 changed files with 577 additions and 0 deletions.
52 changes: 52 additions & 0 deletions docs/action.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Action

Runcobo use one action one file.

### Before Filter
```crystal
class BeforeExample < BaseAction
before required_login
def required_login
Runcobo::Log.info { "Required Login" }
end
get "/before_example"
call do
render_plain "Hello World!"
end
end
```

### After Filter
```crystal
class BeforeExample < BaseAction
after log_params
def log_params
Runcobo::Log.info { "#{params}" }
end
get "/before_example"
call do
render_plain "Hello World!"
end
end
```

### Skip Filter
```crystal
class BaseAction
before required_login
def required_login
Runcobo::Log.info { "Required Login" }
end
end
class SkipExample < BaseAction
skip required_login
get "/before_example"
call do
render_plain "Hello World!"
end
end
```
Binary file added docs/assets/favicon.ico
Binary file not shown.
Binary file added docs/assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions docs/getting_started.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Getting Started

1.Init a Crystal project.
```shell
crystal init app demo && cd demo
```

2.Add the dependency to your shard.yml and run `shards install`.
```yaml
dependencies:
runcobo:
github: runcobo/runcobo
```
3.Write down the following code in `src/demo.cr`.
```crystal
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
```

4.Run server.
```shell
crystal src/demo.cr
```

5.Send request.
```shell
curl "http://0.0.0.0:3000/api/v1/add?a=1&b=2"
```
89 changes: 89 additions & 0 deletions docs/html.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
### Render HTML

*src/controllers/books/index.cr*
```crystal
class Books::Index < BaseAction
get "/books"
call do
books = Book.all
render_water "books/index"
end
end
```

*src/views/books/index.water*
```crystal
table %|class="table table-hover"| {
thead {
tr {
th "ID"
th "Author"
th "Name"
th "Published At"
}
}
tbody {
books.each do |book|
tr {
td book.id
td book.author
td book.name
td book.published_at
}
end
}
}
```

Then, output a HTML string.
```json
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Author</th>
<th>Name</th>
<th>Published At</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>David</td>
<td>Crystal Programming</td>
<td>2020-08-02 14:07:41 +08:00</td>
</tr>
</tbody>
</table>
```

### Render Partial

*src/views/books/index.jbuilder*
```crystal
{{ read_file("src/views/books/table.water").id }}
```

*src/views/books/table.water*
```crystal
table %|class="table table-hover"| {
thead {
tr {
th "ID"
th "Author"
th "Name"
th "Published At"
}
}
tbody {
books.each do |book|
tr {
td book.id
td book.author
td book.name
td book.published_at
}
end
}
}
```
43 changes: 43 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Introduction

Runcobo is a general purpose framework built on Crystal.

### Commands

```shell
runcobo [<commands>...] [<arguments>...]

Commands:
routes - Print all routes of the app.
version - Print the current version of the runcobo.
help - Print usage synopsis.

Options:
-h, --help Print usage synopsis.
-v, --version Print the current version of the runcobo.
```

### Project Architecture

lib/ # Library
src/
main.cr # Entry file
actions/ # Actions Directory
...
assets/ # Assets Directory
views/ # Views Directory
layouts/ # Layouts directory
...
models/ # Models Directory
...
shards.yml # The packages congfiuration file
shards.lock # The lock file for packages congfiuration file

### Design Architecture
MVC (Model-View-Controller)

### Design Principles

+ Simple Design must be simple, both in implementation and interface.
+ Intuitive Design must be intuitive.
+ Consistent Design must be consistent.
19 changes: 19 additions & 0 deletions docs/installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Installation

### Install Crystal
Crystal is a language for humans and computers.

Crystal is a type safe, compiled language inspired by the simplicity of Ruby. Type safety means more errors are caught by the compiler during development, so you can be more confident about your code working in production.

See [https://crystal-lang.org/install/](https://crystal-lang.org/install) to install Crystal.

### Install Runcobo
You can install Runcobo from sources. Other installations are working on.

```shell
curl -L https://github.com/runcobo/runcobo/archive/stable.zip --output runcobo.zip
unzip runcobo.zip
cd runcobo-stable/
sudo make install
runcobo -v
```
51 changes: 51 additions & 0 deletions docs/json.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
### Render JSON

*src/controllers/books/index.cr*
```crystal
class Books::Index < BaseAction
get "/books"
call do
books = Book.all
render_jbuilder "books/index"
end
end
```

*src/views/books/index.jbuilder*
```crystal
json.array! "books", books do |json, book|
json.book_id book.id
json.author book.author
json.name book.name
json.published_at book.published_at
end
```

Then, output a JSON string.
```json
{
"books": [{
"book_id": 1,
"author": "David",
"name": "Crystal Programming",
"published_at": "2020-08-08T20:00:00+00:00"
}]
}
```

### Render Partial

*src/views/books/index.jbuilder*
```crystal
json.array! "books", books do |json, book|
{{ read_file("src/views/books/_base_book.jbuilder").id }}
end
```

*src/views/books/_base_book.jbuilder*
```crystal
json.book_id book.id
json.author book.author
json.name book.name
json.published_at book.published_at
```
15 changes: 15 additions & 0 deletions docs/model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Model

Here is a list of ORM from [awesome-crystal](https://github.com/veelenga/awesome-crystal#ormodm-extensions)

+ [avram](https://github.com/luckyframework/avram) - A database wrapper for reading, writing, and migrating Postgres databases. (Only Postgres)
+ [clear](https://github.com/anykeyh/clear) - ORM specialized to PostgreSQL only but with advanced features (Only Postgres)
+ [crecto](https://github.com/Crecto/crecto) - Database wrapper, based on Ecto
+ [granite](https://github.com/amberframework/granite) - ORM for Postgres, Mysql, Sqlite
+ [jennifer.cr](https://github.com/imdrasil/jennifer.cr) - Active Record pattern implementation with flexible query chainable builder and migration system
+ [ohm-crystal](https://github.com/soveran/ohm-crystal) - Object-hash mapping library for Redis (Only Redis)
+ [onyx-sql](https://github.com/onyxframework/sql) - DB-agnostic SQL ORM with beautiful DSL and type-safe Query builder
+ [rethinkdb-orm](https://github.com/spider-gazelle/rethinkdb-orm) - ORM for RethinkDB / RebirthDB (Only RethinkDB / RebirthDB)
+ [stal-crystal](https://github.com/soveran/stal-crystal) - Set algebra solver for Redis

Runcobo prefers to use [jennifer.cr](https://github.com/imdrasil/jennifer.cr), you can check its [docs](https://imdrasil.github.io/jennifer.cr/docs/) and [api](https://imdrasil.github.io/jennifer.cr/latest/).
73 changes: 73 additions & 0 deletions docs/params.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Params

### Type-safe Params
An old Chinese proverb says, "If something is important, then repeat it three times". So,

**Params in Runcobo are type-safe.**

**Params in Runcobo are type-safe.**

**Params in Runcobo are type-safe.**

### Three Steps To Use Params

+ First, declare what params you expect and what type you expect by following methods: `url`, `query`, `form` and `json`.
+ Next, Runcobo parses request and wraps params to a local variable called `params`.
+ Third, use `params` variable to get or set request params.

### Url Params
```crystal
class UrlExample < BaseAction
get "/url_example/:a/:b"
url NamedTuple(a: Int32, b: Int32)
call do
sum = params[:a] + params[:b]
render_plain sum.to_s
end
end
```

### Query Params
```crystal
class QueryExample < BaseAction
get "/query_example"
query NamedTuple(a: Int32, b: Int32)
call do
sum = params[:a] + params[:b]
render_plain sum.to_s
end
end
```

### Form Params
```crystal
class FormExample < BaseAction
post "/form_example"
form NamedTuple(a: Int32, b: Int32)
call do
sum = params[:a] + params[:b]
render_plain sum.to_s
end
end
```

### JSON Params
```crystal
class JsonExample < BaseAction
post "/json_example"
json NamedTuple(a: Int32, b: Int32)
call do
sum = params[:a] + params[:b]
render_plain sum.to_s
end
end
```

### Params Merge Order
You can declare various kinds of params in a action. If params are in same key, they will be merged in following order:

`Query Params < Form Params < JSON Params < Url Params`
Loading

0 comments on commit 9d460de

Please sign in to comment.