This package is for giving utils for web servers. We have the following features:
- database traits macros for dependency injection of DB connections and mocking of DB connections in testing
- Tokio based pub sub macros for event driven programming entirely in Tokio
- error handling for web frameworks (Actix, Axum, Rocket, Hyper)
- Config handling trait for environment variables and overwrites for testing
Data Access Layer (DAL
feature)
If you enable the dal
feature you currently get access to macros that massively reduce the amount of boilerplate code you need to write for your data access layer. The following is an example of how to use the DAL:
use ;
// define the schemas that are going to be used for database transactions
// (traits for specific databases and serialization still need to be implemented)
// Construct traits and map methods to function signatures
define_dal_transactions!;
// create an empty struct that we can pass as a handle through a function
;
// implement the `CreateUser` trait for the `PostgresHandle` struct using the `create_user_postgres` function
async
// test the function to see how it works
let new_user = NewUser ;
let outcome = create.await.unwrap;
assert_eq!;
Pub Sub Tokio event driven programming (tokio-pub-sub
feature)
If you enable the tokio-pub-sub
feature you get access to the following macros:
use subscribe_to_event;
use config_tokio_event_runtime;
use publish_event;
In our main.rs
file where we are defining the entry point for our tokio
runtime we must configure the event runtime with the following code:
config_tokio_event_runtime!;
Everything else can be done in any file, but the config_tokio_event_runtime
needs to be in the main.rs
file. We can now make subscriptions to events with the following code:
;
;
async
async
pub async
Here the #[subscribe_to_event]
macro inspects the input. If the function is a subscriber then we can only have one input which is a struct that we are subscribing to. This struct needs to implement the Serialize
and Deserialize
traits. So, if we publish an event with the AddNumbers
then the add_numbers
function will be called with the AddNumbers
struct as the input. Multiple functions can subscribe to the same struct. We can test this with the following code:
async
Which will give the following output:
Initializing function: test
Initializing function: test2
Initializing function: add_numbers
Hello, world!
calling from test function with: One
calling from test2 function with: Two
calling from test function with: One
calling from test2 function with: Two
Adding numbers: AddNumbers { num1: 1, num2: 2 }
Result: 3
calling from test2 function with: Two
We can see that our functions are initialized before we even see the Hello, world!
. This is because our subscribers are registered before the main
function is called.
Error handling
This package has errors that can be imported with the following statement:
use ;
The NanoServiceError
is the error struct that handles the message and the error status of the error. To construct an error, you can use the following:
let error = new
The NanoServiceErrorStatus
will convert to a HTTP response code that corresponds with the message. Without any feature selection, the error will just be an error. However, if you select one or more of the following features:
- axum
- actix
- rocket
- hyper
The error will be able to be converted to that framework's HTTP response. This means that your library can return NanoServiceError
structs for errors and these errors will be able to convert into HTTP responses for those webframeworks.
You can also map any expression returning a Result
to return a NanoServiceError
on error with the code below:
use nanoservices_utils::safe_eject;
let some_outcome = safe_eject!(some_function());
To see how error handling works for web frameworks, lets look at the following example where we have a function that will not allow a number more than 10 with the following code:
use ;
We can then call this function and exploit the ?
operator to return a HTTP response automatically if an error is thrown with the following frameworks:
Actix
use ;
use NanoServiceError;
use Deserialize;
pub async
Axum
use ;
use NanoServiceError;
use Deserialize;
pub async
Rocket
use Json;
use NanoServiceError;
use Status;
use Deserialize;
// Specify that Rocket's `serde` should be used
pub async
Beta Utils
I'm currently supporting the following utils:
- JWT
- Config
- runtime state
but these are not polished