Skip to content

Commit

Permalink
updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ndortega committed Jan 12, 2024
1 parent fe02183 commit 29aa143
Showing 1 changed file with 120 additions and 2 deletions.
122 changes: 120 additions & 2 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ Breathe easy knowing you can quickly spin up a web server with abstractions you'
## Features

- Straightforward routing
- Real-time Metrics Dashboard
- Auto-generated swagger documentation
- Out-of-the-box JSON serialization & deserialization (customizable)
- Type definition support for path parameters
- Built-in multithreading support
- Built-in Cron Scheduling (on endpoints & functions)
- Multithreading support
- Cron Scheduling (on endpoints & functions)
- Middleware chaining (at the application, router, and route levels)
- Static & Dynamic file hosting
- Templating Support
- Route tagging
- Repeat tasks

Expand Down Expand Up @@ -363,6 +365,105 @@ end
serve()
```


## Templating

Rather than building an internal engine for templating or adding additional dependencies, Oxygen
provides two package extensions to support `Mustache.jl` and `OteraEngine.jl` templates.

Oxygen provides a simple wrapper api around both packages that makes it easy to render templates from strings,
templates, and files. This wrapper api returns a `render` function which accepts a dictionary of inputs to fill out the
template.

In all scenarios, the rendered template is returned inside a HTTP.Response object ready to get served by the api.
By default, the mime types are auto-detected either by looking at the content of the template or the extension name on the file.
If you know the mime type you can pass it directly through the `mime_type` keyword argument to skip the detection process.

### Mustache Templating
Please take a look at the [Mustache.jl](https://jverzani.github.io/Mustache.jl/dev/) documentation to learn the full capabilities of the package

Example 1: Rendering a Mustache Template from a File

```julia
using Oxygen

# Load the Mustache template from a file and create a render function
render = mustache("./templates/greeting.txt", from_file=false)

@get "/mustache/file" function()
data = Dict("name" => "Chris")
return render(data) # This will return an HTML.Response with the rendered template
end
```

Example 2: Specifying MIME Type for a plain string Mustache Template
```julia
using Oxygen

# Define a Mustache template (both plain strings and mustache templates are supported)
template_str = "Hello, {{name}}!"

# Create a render function, specifying the MIME type as text/plain
render = mustache(template_str, mime_type="text/plain") # mime_type keyword arg is optional

@get "/plain/text" function()
data = Dict("name" => "Chris")
return render(data) # This will return a plain text response with the rendered template
end
```

### Otera Templating
Please take a look at the [OteraEngine.jl](https://mommawatasu.github.io/OteraEngine.jl/dev/tutorial/#API) documentation to learn the full capabilities of the package

Example 1: Rendering an Otera Template with Logic and Loops

```julia
using Oxygen

# Define an Otera template
template_str = """
<html>
<head><title>{{ title }}</title></head>
<body>
{% for name in names %}
Hello {{ name }}<br>
{% end %}
</body>
</html>
"""

# Create a render function for the Otera template
render = otera(template_str)

@get "/otera/loop" function()
data = Dict("title" => "Greetings", "names" => ["Alice", "Bob", "Chris"])
return render(data) # This will return an HTML.Response with the rendered template
end
```

In this example, an Otera template is defined with a for-loop that iterates over a list of names, greeting each name.

Example 2: Running Julia Code in Otera Template
```julia
using Oxygen

# Define an Otera template with embedded Julia code
template_str = """
The square of {{ number }} is {< number^2 >}.
"""

# Create a render function for the Otera template
render = otera(template_str)

@get "/otera/square" function()
data = Dict("number" => 5)
return render(data) # This will return an HTML.Response with the rendered template
end

```

In this example, an Otera template is defined with embedded Julia code that calculates the square of a given number.

## Mounting Static Files

You can mount static files using this handy function which recursively searches a folder for files and mounts everything. All files are
Expand Down Expand Up @@ -705,10 +806,27 @@ Low-level macro that allows a route to be handle multiple request types
| :-------- | :------- | :------------------------- |
| `folder` | `string` | **Required**. The folder to serve files from |
| `mountdir` | `string` | The root endpoint to mount files under (default is "static")|
| `set_headers` | `function` | Customize the http response headers when returning these files |
| `loadfile` | `function` | Customize behavior when loading files |

Serve all static files within a folder. This function recursively searches a directory
and mounts all files under the mount directory using their relative paths.

#### dynamicfiles

```julia
dynamicfiles(folder, mount)
```
| Parameter | Type | Description |
| :-------- | :------- | :------------------------- |
| `folder` | `string` | **Required**. The folder to serve files from |
| `mountdir` | `string` | The root endpoint to mount files under (default is "static")|
| `set_headers` | `function` | Customize the http response headers when returning these files |
| `loadfile` | `function` | Customize behavior when loading files |

Serve all static files within a folder. This function recursively searches a directory
and mounts all files under the mount directory using their relative paths. The file is loaded
on each request, potentially picking up any file changes.

### Request helper functions

Expand Down

0 comments on commit 29aa143

Please sign in to comment.