|
1 | 1 | # mikros-rs
|
2 |
| -Mikros framework for rust applications |
| 2 | + |
| 3 | +## About |
| 4 | + |
| 5 | +`mikros` is rust framework for creating applications. |
| 6 | + |
| 7 | +### Introduction |
| 8 | + |
| 9 | +Mikros is an opinionated framework aiming to ease and standardize the creation |
| 10 | +of applications. It supports creating applications that need to run for long |
| 11 | +periods, usually executing indefinitely, performing some specific operation or |
| 12 | +providing some API to be consumed by other services. But it also supports |
| 13 | +standalone applications that execute its task and finishes. |
| 14 | + |
| 15 | +Currently, it has support for the following kind of applications: |
| 16 | + |
| 17 | +* gRPC: an application with an API defined from a [protobuf](https://protobuf.dev) file. |
| 18 | +* HTTP: an HTTP server-type application. |
| 19 | +* native: a general-purpose application, without a defined API, with the ability to execute any code for long periods |
| 20 | +* script: also a general-purpose application, without a defined API, but that only needs to execute a single function and stop. |
| 21 | + |
| 22 | +### Service |
| 23 | + |
| 24 | +Service, here, is considered an application that may or may not remain running |
| 25 | +indefinitely, performing some type of task or waiting for commands to activate it. |
| 26 | + |
| 27 | +The framework consists of an SDK that facilitates the creation of these applications |
| 28 | +in a way that standardizes their code, so that they all perform tasks with the |
| 29 | +same behavior and are written in a very similar manner. In addition to providing |
| 30 | +flexibility, allowing these applications to also be customized when necessary. |
| 31 | + |
| 32 | +Building a service using the framework's SDK must adhere to the following points: |
| 33 | + |
| 34 | +* Have a struct where mandatory methods according to its category must be implemented; |
| 35 | +* Initialize the SDK correctly; |
| 36 | +* Have a configuration file, called `service.toml`, containing information about itself and its functionalities. |
| 37 | + |
| 38 | +### Example of a service |
| 39 | + |
| 40 | +The following example demonstrates how to create a service of a `script` |
| 41 | +type. The `Service` structure implements the trait [ScriptService](src/service/script/mod.rs) |
| 42 | +that makes it being supported by this type of service inside the framework. |
| 43 | + |
| 44 | +```rust |
| 45 | +use mikros::errors as merrors; |
| 46 | +use mikros::service::builder::ServiceBuilder; |
| 47 | +use mikros::service::context::Context; |
| 48 | + |
| 49 | +#[derive(Clone, Default)] |
| 50 | +pub struct Service; |
| 51 | + |
| 52 | +#[async_trait::async_trait] |
| 53 | +impl mikros::service::lifecycle::Lifecycle for Service { |
| 54 | + async fn on_start(&mut self, _ctx: &Context) -> merrors::Result<()> { |
| 55 | + println!("lifecycle on_start"); |
| 56 | + Ok(()) |
| 57 | + } |
| 58 | + |
| 59 | + async fn on_finish(&self) -> merrors::Result<()> { |
| 60 | + println!("lifecycle on_finish"); |
| 61 | + Ok(()) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +#[async_trait::async_trait] |
| 66 | +impl mikros::service::script::ScriptService for Service { |
| 67 | + async fn run(&self, ctx: &Context) -> merrors::Result<()> { |
| 68 | + ctx.logger().info("Start script service"); |
| 69 | + Ok(()) |
| 70 | + } |
| 71 | + |
| 72 | + async fn cleanup(&self, ctx: &Context) { |
| 73 | + ctx.logger().info("Stop script service"); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +#[tokio::main] |
| 78 | +async fn main() { |
| 79 | + let s = Service::default(); |
| 80 | + let svc = ServiceBuilder::default() |
| 81 | + .script(Box::new(s)) |
| 82 | + .build(); |
| 83 | + |
| 84 | + match svc { |
| 85 | + Ok(mut svc) => svc.start().await, |
| 86 | + Err(e) => panic!("{}", e.to_string()), |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +It must have a `service.toml` file with the following content: |
| 92 | + |
| 93 | +```toml |
| 94 | +name = "script-example" |
| 95 | +types = ["script"] |
| 96 | +version = "v1.0.0" |
| 97 | +language = "rust" |
| 98 | +product = "Matrix" |
| 99 | +``` |
| 100 | +When executed, it outputs the following (with a different time according the execution): |
| 101 | + |
| 102 | +```bash |
| 103 | +2024-11-21 20:24:50.215638 -03:00 INFO service starting {"svc.language":"rust","local.ts":1732231490,"local.ts_ms":1732231490215,"svc.name":"native-example","svc.product":"examples","svc.version":"v0.1.0"} |
| 104 | +2024-11-21 20:24:50.215953 -03:00 INFO starting features {"svc.language":"rust","local.ts":1732231490,"local.ts_ms":1732231490215,"svc.name":"native-example","svc.product":"examples","svc.version":"v0.1.0"} |
| 105 | +lifecycle on_start |
| 106 | +2024-11-21 20:24:50.215974 -03:00 INFO service resources {"svc.language":"rust","local.ts":1732231490,"local.ts_ms":1732231490215,"svc.name":"native-example","svc.product":"examples","svc.version":"v0.1.0"} |
| 107 | +2024-11-21 20:24:50.215993 -03:00 INFO service is running {"svc.language":"rust","local.ts":1732231490,"kind":"script","local.ts_ms":1732231490215,"svc.name":"native-example","svc.product":"examples","svc.version":"v0.1.0"} |
| 108 | +2024-11-21 20:24:50.216023 -03:00 INFO Stop script service {"svc.language":"rust","local.ts":1732231490,"local.ts_ms":1732231490216,"svc.name":"native-example","svc.product":"examples","svc.version":"v0.1.0"} |
| 109 | +2024-11-21 20:24:50.216043 -03:00 DEBUG sending shutdown signal for service tasks {"svc.language":"rust","local.ts":1732231490,"local.ts_ms":1732231490216,"svc.name":"native-example","svc.product":"examples","svc.version":"v0.1.0"} |
| 110 | +2024-11-21 20:24:50.216043 -03:00 DEBUG starting service task {"svc.language":"rust","local.ts":1732231490,"task_name":"script","local.ts_ms":1732231490216,"svc.name":"native-example","svc.product":"examples","svc.version":"v0.1.0"} |
| 111 | +2024-11-21 20:24:50.216148 -03:00 INFO Start script service {"svc.language":"rust","local.ts":1732231490,"local.ts_ms":1732231490216,"svc.name":"native-example","svc.product":"examples","svc.version":"v0.1.0"} |
| 112 | +2024-11-21 20:24:50.216168 -03:00 DEBUG finishing service task {"svc.language":"rust","local.ts":1732231490,"task_name":"script","local.ts_ms":1732231490216,"svc.name":"native-example","svc.product":"examples","svc.version":"v0.1.0"} |
| 113 | +2024-11-21 20:24:50.216186 -03:00 DEBUG service task finished {"svc.language":"rust","local.ts":1732231490,"task_name":"script","local.ts_ms":1732231490216,"svc.name":"native-example","svc.product":"examples","svc.version":"v0.1.0"} |
| 114 | +lifecycle on_finish |
| 115 | +2024-11-21 20:24:50.216234 -03:00 INFO service stopped {"svc.language":"rust","local.ts":1732231490,"local.ts_ms":1732231490216,"svc.name":"native-example","svc.product":"examples","svc.version":"v0.1.0"} |
| 116 | +``` |
| 117 | + |
| 118 | +For more examples, including how to create new features and new service kind, |
| 119 | +you can check the [examples](examples) directory. |
| 120 | + |
| 121 | +## Roadmap |
| 122 | + |
| 123 | +* Improve logger to use better structured log system and to set the log level |
| 124 | +using service definitions. |
| 125 | +* Enable features and services to use the same env system that the core uses. |
| 126 | +* Improve unit tests. |
| 127 | +* Full compatibility between go and rust gRPC services. |
| 128 | + |
| 129 | +## License |
| 130 | + |
| 131 | +[MIT License](LICENSE) |
0 commit comments