Skip to content

Commit

Permalink
feat: initiate tests with coverage; proper readme & contribution docu…
Browse files Browse the repository at this point in the history
…mentations
  • Loading branch information
aldy505 committed May 20, 2023
1 parent b0bc89c commit 4514fbe
Show file tree
Hide file tree
Showing 13 changed files with 309 additions and 49 deletions.
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = false
max_line_length = 120
tab_width = 4

[*.rs]
max_line_length = 100

[{*.har,*.jsb2,*.jsb3,*.json,.babelrc,.eslintrc,.prettierrc,.stylelintrc,bowerrc,composer.lock,jest.config}]
indent_size = 2

[{*.yaml,*.yml}]
indent_size = 2
4 changes: 4 additions & 0 deletions .github/CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ For answers to common questions about this code of conduct, see the FAQ at
[https://www.contributor-covenant.org/translations][translations].

[homepage]: https://www.contributor-covenant.org

[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html

[Mozilla CoC]: https://github.com/mozilla/diversity

[FAQ]: https://www.contributor-covenant.org/faq

[translations]: https://www.contributor-covenant.org/translations
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github: teknologi-umum
custom: ["https://saweria.co/teknologiumum"]
custom: [ "https://saweria.co/teknologiumum" ]
5 changes: 4 additions & 1 deletion .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ jobs:
base: ${{ github.event.repository.default_branch }}
head: HEAD
extra_args: --debug --only-verified
- name: Setup tarpaulin
run: cargo install cargo-tarpaulin
- name: Build
run: cargo build
- name: Test
run: cargo test
run: cargo tarpaulin --out Lcov
- uses: codecov/codecov-action@v3
5 changes: 4 additions & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ jobs:
base: ${{ github.event.repository.default_branch }}
head: HEAD
extra_args: --debug --only-verified
- name: Setup tarpaulin
run: cargo install cargo-tarpaulin
- name: Build
run: cargo build
- name: Test
run: cargo test
run: cargo tarpaulin --out Lcov
- uses: codecov/codecov-action@v3
56 changes: 56 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Contributing Guide

Hello! We'd love to see your contribution on this repository soon, even if it's just a typo fix!

Contributing means anything from reporting bugs, ideas, suggestion, code fix, even new feature.

Bear in mind to keep your contributions under the [Code of Conduct](./.github/CODE_OF_CONDUCT.md) for the community.

## Bug report, ideas, and suggestion

The [issues](https://github.com/teknologi-umum/polarite/issues) page is a great way to communicate to us.
Other than that, we have a [Telegram group](https://t.me/teknologi_umum_v2) that you can discuss your ideas into.
If you're not an Indonesian speaker, it's 100% fine to talk in English there.

Please make sure that the issue you're creating is in as much detail as possible. Poor communication might lead to a big mistake, we're trying to avoid that.

## Pull request

**A big heads up before you're writing a breaking change code or a new feature: Please open up an
[issue](https://github.com/teknologi-umum/polarite/issues) regarding what you're working on, or just talk in the
[Telegram group](https://t.me/teknologi_umum_v2).**

### Prerequisites

You will need a few things to get things working:

1. Latest stable version of [Rust](https://www.rust-lang.org/tools/install).
2. An IDE or text editor with Rust plugin installed.

### Getting Started

1. [Fork](https://help.github.com/articles/fork-a-repo/) this repository to your own Github account and [clone](https://help.github.com/articles/cloning-a-repository/) it to your local machine.
2. Run `cargo update` to install the dependencies needed.
3. Run `cargo run` to start the development application.
4. Have fun!

You are encouraged to use [Conventional Commit](https://www.conventionalcommits.org/en/v1.0.0-beta.2/)
for your commit message. But it's not really compulsory.

### Testing your change

Creating tests are not necessary, but it's always great if you can provide tests!

```sh
$ cargo test
```

### Before creating a PR

Please test (command above) and format your code accordingly to pass the CI.

```sh
$ cargo fmt
```

And you're set!
28 changes: 14 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "kuma-relay"
name = "roselite"
version = "0.1.0"
edition = "2021"

Expand Down
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM rust:1.69.0-bullseye AS builder
WORKDIR /app
COPY . .
RUN cargo build --release

FROM debian:bullseye-slim AS runtime
WORKDIR /app
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/*
COPY --from=builder /app/target/release/roselite roselite
CMD ["/app/roselite"]
68 changes: 66 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,67 @@
# Kuma Relay
# Roselite

Nothing to see here yet
Roselite is a simple application to relay your [Uptime Kuma](https://github.com/louislam/uptime-kuma) push monitor type
(falls within the passive monitor category) for multiple applications.

## Usage

Create a file using TOML, YAML, or JSON (or JSON5) containing the configuration for Roselite. The configuration in JSON
should looks like:

```json5
{
"monitors": [
{
// This is the endpoint that you can acquire from your Uptime Kuma instance
"push_url": "https://your-uptime-kuma.com/api/push/Eq15E23yc3",
// This is the endpoint to your private/secluded server within an internal network
"monitor_url": "https://your-internal-endpoint.com"
},
// ...
]
}
```

Put the path to the configuration file on `CONFIGURATION_FILE_PATH` environment variable. It will be read by Roselite
and be processed. It only accepts file with extensions of `toml`, `yml`, `yaml`, `json`, and `json5`.

That's it. Now you can run your own Roselite and looks at moving heartbeats on your Uptime Kuma instance.

## Contributing

See [contributing guide](./CONTRIBUTING.md)

## Where do "Roselite" comes from?

We have a convention of having project names being taken from
the [list of minerals on Wikipedia](https://en.wikipedia.org/wiki/List_of_minerals).
As this one happens to be some project that's probably a bit niche, I'd like to think of having it named after
our convention. You can read more about Roselite [on Wikipedia](https://en.wikipedia.org/wiki/Roselite).

## License

```
MIT License
Copyright (c) 2023 Teknologi Umum <opensource@teknologiumum.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```

See [LICENSE](./LICENSE)
79 changes: 75 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::collections::BTreeMap;
use std::fs::File;
use std::io::Read;
use serde::Deserialize;

use anyhow::{Error, Result};
use serde::Deserialize;

/// Monitor defines a single monitor configuration
#[derive(Deserialize, Clone)]
pub struct Monitor {
pub push_url: String,
Expand All @@ -12,6 +14,7 @@ pub struct Monitor {
pub request_headers: Option<BTreeMap<String, String>>,
}

/// Configuration sets a global configuration for the application.
#[derive(Deserialize, Clone)]
pub struct Configuration {
pub monitors: Vec<Monitor>,
Expand All @@ -30,6 +33,10 @@ impl Configuration {
toml::from_str::<Self>(value).unwrap()
}

/// Supported file extensions (from the given path):
/// - json, json5
/// - toml
/// - yaml, yml
pub fn from_file(path: &String) -> Result<Configuration> {
match File::open(path) {
Ok(mut file) => {
Expand All @@ -49,8 +56,72 @@ impl Configuration {
}

Err(Error::msg("Invalid file type"))
},
Err(_) => Err(Error::msg("Failed opening configuration file"))
}
Err(_) => Err(Error::msg("Failed opening configuration file")),
}
}
}

#[cfg(test)]
mod tests {
use crate::config::Configuration;

#[test]
fn parse_toml_configuration() {
let configuration = r#"[[monitors]]
push_url = "https://your-uptime-kuma.com/api/push/Eq15E23yc3"
monitor_url = "https://github.com/healthz""#;

let parsed_configuration = Configuration::from_toml(configuration);

assert_eq!(parsed_configuration.monitors.len(), 1);
if let Some(first_monitor) = parsed_configuration.monitors.first() {
assert_eq!(first_monitor.monitor_url, "https://github.com/healthz");
assert_eq!(
first_monitor.push_url,
"https://your-uptime-kuma.com/api/push/Eq15E23yc3"
);
}
}

#[test]
fn parse_yaml_configuration() {
let configuration = r#"monitors:
- push_url: "https://your-uptime-kuma.com/api/push/Eq15E23yc3"
monitor_url: "https://github.com/healthz""#;

let parsed_configuration = Configuration::from_yaml(configuration);

assert_eq!(parsed_configuration.monitors.len(), 1);
if let Some(first_monitor) = parsed_configuration.monitors.first() {
assert_eq!(first_monitor.monitor_url, "https://github.com/healthz");
assert_eq!(
first_monitor.push_url,
"https://your-uptime-kuma.com/api/push/Eq15E23yc3"
);
}
}
}

#[test]
fn parse_json_configuration() {
let configuration = r#"{
"monitors": [
{
"push_url": "https://your-uptime-kuma.com/api/push/Eq15E23yc3",
"monitor_url": "https://github.com/healthz"
}
]
}"#;

let parsed_configuration = Configuration::from_json(configuration);

assert_eq!(parsed_configuration.monitors.len(), 1);
if let Some(first_monitor) = parsed_configuration.monitors.first() {
assert_eq!(first_monitor.monitor_url, "https://github.com/healthz");
assert_eq!(
first_monitor.push_url,
"https://your-uptime-kuma.com/api/push/Eq15E23yc3"
);
}
}
}
Loading

0 comments on commit 4514fbe

Please sign in to comment.