Skip to content

Commit 1ef5339

Browse files
authored
Doc (#111)
1 parent d05d047 commit 1ef5339

File tree

11 files changed

+222
-60
lines changed

11 files changed

+222
-60
lines changed

.github/workflows/mdbook.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Sample workflow for building and deploying a mdBook site to GitHub Pages
2+
#
3+
# To get started with mdBook see: https://rust-lang.github.io/mdBook/index.html
4+
#
5+
name: Deploy mdBook site to Pages
6+
7+
on:
8+
# Runs on pushes targeting the default branch
9+
push:
10+
branches: ["main"]
11+
12+
# Allows you to run this workflow manually from the Actions tab
13+
workflow_dispatch:
14+
15+
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
16+
permissions:
17+
contents: read
18+
pages: write
19+
id-token: write
20+
21+
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
22+
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
23+
concurrency:
24+
group: "pages"
25+
cancel-in-progress: false
26+
27+
jobs:
28+
# Build job
29+
build:
30+
runs-on: ubuntu-latest
31+
env:
32+
MDBOOK_VERSION: 0.4.21
33+
steps:
34+
- uses: actions/checkout@v3
35+
- name: Install mdBook
36+
run: |
37+
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf -y | sh
38+
rustup update
39+
cargo install --version ${MDBOOK_VERSION} mdbook
40+
- name: Setup Pages
41+
id: pages
42+
uses: actions/configure-pages@v3
43+
- name: Build with mdBook
44+
run: mdbook build ./doc
45+
- name: Upload artifact
46+
uses: actions/upload-pages-artifact@v2
47+
with:
48+
path: ./doc/book
49+
50+
# Deployment job
51+
deploy:
52+
environment:
53+
name: github-pages
54+
url: ${{ steps.deployment.outputs.page_url }}
55+
runs-on: ubuntu-latest
56+
needs: build
57+
steps:
58+
- name: Deploy to GitHub Pages
59+
id: deployment
60+
uses: actions/deploy-pages@v2

doc/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
book

doc/book.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[book]
2+
authors = ["crustagen"]
3+
language = "en"
4+
multilingual = false
5+
src = "./src"
6+
title = "crustagen"

doc/src/SUMMARY.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Summary
2+
- [Overview]()
3+
- [Introduction]()
4+
- [Motivation]()
5+
- [Prerequisites]()
6+
- [Features]()
7+
- [Generator]()
8+
- [How it works]()
9+
- [Usage]()
10+
- [How to write spec files](generator/specs.md)
11+
- [How to write your own templates](generator/templates.md)
12+
- [Microservice]()
13+
- [How it works]()
14+
- [Usage]()
15+
- [Environment Variables](microservice/environment.md)
16+
- [Working with Open Policy Agent](microservice/opa.md)
17+
- [Tracing]()
18+
- [Metadata server]()

doc/src/generator/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Microservice

doc/src/generator/environment.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Environment Variables

doc/src/generator/specs.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# How to write spec files
2+
3+
The generator is mostly* compliant with the [AsyncAPI specification](https://github.com/asyncapi/spec)
4+
5+
## *Limitations
6+
7+
- Only json payloads are currently supported for automatic deserialization
8+
- Only one server is currently supported and only nats protocol is supported
9+
- Generated microservice doesn't support authentication with NATS-broker out of the box
10+
- Only one message is currently supported per channel, payloads can be choosen freely including anyOf/oneOf/allOf
11+
- The generated rust types are required by default, if you want to use optional types, please modify the generated types after generation or use oneOf/anyOf/allOf to represent optional types
12+
- references in the specification are only suppported inside the same file, external references are not supported
13+
14+
15+
## Crustagen specific features
16+
17+
- A channel supposed to be a stream can be declared in the specification file with the `x-streamname` field.
18+
```yaml
19+
channels:
20+
{channel-name}:
21+
{channel-operation}:
22+
bindings:
23+
nats:
24+
x-streamname: testStream
25+
```

doc/src/generator/templates.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Writing your own templates
2+
3+
- Any templates that in the templates folder at compilation time will be embedded in the compiled binary.
4+
- If you only have the binary you can put templates in the folder `user-templates`.
5+
If a file from the `user-templates` folder has the same path as an embedded template, only the template from `user-template` will be rendered.
6+
- set the command line argument `--user-templates` or `-u` to set a custom folder
7+
- the last file extension will be removed e.g: `file.rs.go` will be rendered to `file.rs`.
8+
- For examples refer to the allready included [templates](https://github.com/Programmierpraktikum-MVA/AsyncAPI/tree/d05d047c5ea9dfb221f31ecbf5af03387103e342/templates)
9+
10+
11+
12+
## What fields are available inside templates?
13+
14+
Any of these fields will be accessible:
15+
```rust,noplayground
16+
pub struct TemplateContext<'a> {
17+
pub title: &'a String,
18+
pub description: &'a Option<String>,
19+
pub server: &'a Server,
20+
pub subscribe_channels: Vec<(&'a String, SimplifiedOperation)>,
21+
pub publish_channels: Vec<(&'a String, SimplifiedOperation)>,
22+
pub model: Model,
23+
}
24+
25+
pub struct Model {
26+
pub message_models: Vec<RustSchemaRepresentation>,
27+
// pub enums: Vec<MultiStructEnum>,
28+
}
29+
30+
pub struct SimplifiedOperation {
31+
pub unique_id: String,
32+
pub original_operation: Operation,
33+
// array, da es eine oder mehrere messages geben kann
34+
pub messages: Vec<SimplifiedMessage>,
35+
// pub multiple_messages_enum: Option<MultiStructEnum>,
36+
}
37+
38+
pub struct MultiStructEnum {
39+
pub unique_id: String,
40+
pub messages: Vec<SimplifiedMessage>,
41+
pub struct_definition: String,
42+
}
43+
44+
pub struct SimplifiedMessage {
45+
pub unique_id: String,
46+
pub original_message: Message,
47+
pub payload: Option<RustSchemaRepresentation>,
48+
}
49+
```
50+
- for more information about the fields available from these structs please refer to: [all rust structs](https://github.com/Programmierpraktikum-MVA/AsyncAPI/tree/main/src/asyncapi_model)
51+
52+
53+
54+
## Render to separate files
55+
56+
It is possible to generate files for each specific object in your AsyncAPI documentation. For example, you can specify a filename like `$$handler$$.rs.go` to generate a file for each `publish_channel` defined in your AsyncAPI spec.
57+
58+
This works with file templates that include the following in their name:
59+
- `$$handler$$`
60+
- `$$producer$$`
61+
- `$$model$$`
62+
- `$$schemas$$`
63+
64+
65+
66+
## Functions available inside the templates
67+
68+
- `to_lower(input: String) -> String` converts String to lowercase
69+
- `key_exists(input: String) -> String` checks if key exists
70+
- `camel_to_snake_case(input :String) -> String` converts a String in camelCase to snake_case
71+
- `replace(input: String, from: String, to: String) -> String` replaces `from` with `to` for `input`
72+
- Side Note: these functions are defined in `src/generator/template_functions.rs` feel free to extend then, if you have access to the source code.
73+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Environment Variables
2+
3+
An `.env`-file is automatically generated from the [.env template](https://github.com/Programmierpraktikum-MVA/AsyncAPI/blob/d05d047c5ea9dfb221f31ecbf5af03387103e342/templates/.env.go)
4+
If you want to extend the .env file feel free to do so in the generated code
5+
- or if you want to customize the generated .enf file before it is generated take a look at [writing your own templates](../generator/templates.md)
6+
The generated microservice uses the following environment variables (with their respective default values):
7+
```json
8+
SERVICE_PORT = "8080"
9+
SERVER_URL = "{{ .server.url }}"
10+
LOG_LEVEL = "DEBUG" # available levels are ERROR, WARN, INFO, DEBUG and TRACE
11+
OPA_RULES= "path/to/admin/policy"
12+
TRACING_ENABLED = false
13+
SCHEMA_VALIDATION_ENABLED = true
14+
```
15+
16+
Also per channel the subject will be set via an environment variable:
17+
```json
18+
{channel_name}_SUBJECT = "{subject}" # for normal pub_sub channels
19+
{channel_name}_QUEUE = "{subject}" # for nats queue channels
20+
{channel_name}_STREAM = "{subject}" # for nats jetstream channels
21+
```
22+
23+
And for OPA
24+
```json
25+
OPA_ENABLED = false # choose if OPA should be enabled
26+
#OPA_REMOTE_URL = "localhost:4042" # pick the url for an opa server
27+
#OPA_LOCAL_WASM_PATH = "some/path" # pick the path of a to wasm compiled rego file
28+
```
29+
- for more information see [Working with Open Policy Agent](./opa.md)

doc/src/microservice/opa.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Working with Open Policy Agent
2+
The microservice can utilize the function:
3+
```rust,noplayground
4+
pub async fn opa_eval<I>(input: Serialize) -> Result<serde_json::Value>
5+
```
6+
from `src/policy/policy.rs`
7+
which sends the `input` to an opa_server or uses it as input to evaluate a to `.wasm` compiled `.rego` file dependant on the set [enviornment variables](./environment.md).
8+

0 commit comments

Comments
 (0)