Skip to content

Commit

Permalink
Docs/public (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekkinox committed Jan 27, 2024
1 parent 80535c3 commit c6b073d
Show file tree
Hide file tree
Showing 6 changed files with 467 additions and 36 deletions.
26 changes: 16 additions & 10 deletions docs/modules/fxconfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
[![Deps](https://img.shields.io/badge/osi-deps-blue)](https://deps.dev/go/github.com%2Fankorstore%2Fyokai%2Ffxconfig)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/ankorstore/yokai/fxconfig)](https://pkg.go.dev/github.com/ankorstore/yokai/fxconfig)

## Overview

Yokai provides a [fxconfig](https://github.com/ankorstore/yokai/tree/main/fxconfig) module, allowing to define and retrieve configurations for your application.

It wraps the [config](https://github.com/ankorstore/yokai/tree/main/config) module, based on [Viper](https://github.com/spf13/viper).

## Installation

The [fxconfig](https://github.com/ankorstore/yokai/tree/main/fxconfig) module is automatically loaded by
the [fxcore](https://github.com/ankorstore/yokai/tree/main/fxcore).

When you use a Yokai [application template](https://ankorstore.github.io/yokai/applications/templates/), you have nothing to install, it's ready to use.

## Documentation

### Configuration files
## Configuration files

By default, the module expects the configuration files:

Expand All @@ -24,9 +28,9 @@ By default, the module expects the configuration files:
- to offer env overrides files named `config.{env}.{format}` based on the env var `APP_ENV` (ex: `config.test.yaml` if
env var `APP_ENV=test`)

The following configuration files format are supported: JSON, TOML, YAML, HCL, INI, and env file.
Supported configuration files formats: `.json`, `.toml`, `.yaml`, `.hcl`, `.ini`, and `.env`.

### Configuration usage
## Usage

For the following examples, we will be considering those configuration files:

Expand Down Expand Up @@ -55,11 +59,13 @@ config:
string_value: test
```

#### Configuration access
### Configuration access

This module makes available the [*config.Config](https://github.com/ankorstore/yokai/blob/main/config/config.go) in
This module makes available the [Config](https://github.com/ankorstore/yokai/blob/main/config/config.go) in
Yokai dependency injection system.

It is built on top of `Viper`, see its [documentation](https://github.com/spf13/viper) for more details about available methods.

To access it, you just need to inject it where needed, for example:

```go title="internal/service/example.go"
Expand Down Expand Up @@ -94,7 +100,7 @@ func (s *ExampleService) PrintConfig() {
}
```

#### Configuration dynamic env overrides
### Dynamic env overrides

This module offers the possibility to override dynamically (by merging) configuration files depending on the env
var `APP_ENV` value.
Expand Down Expand Up @@ -122,7 +128,7 @@ You can use any value for `APP_ENV` (to allow you to reflect your own envs): for
will use `config.yaml` values and override them with `config.custom.yaml` values (you just need to ensure
that `config.custom.yaml` exists).

#### Configuration env var placeholders
### Env var placeholders

This module offers the possibility to use placeholders in the config files to reference an env var value, that will be
resolved at runtime.
Expand All @@ -136,7 +142,7 @@ For example, with the env var `BAR=bar`:
fmt.Printf("placeholder: %s", s.config.GetString("config.placeholder"))
```

#### Configuration env var substitution
### Env var substitution

This module offers the possibility to perform configuration files values substitution from env var values.

Expand Down
170 changes: 170 additions & 0 deletions docs/modules/fxhealthcheck.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
# Health Check Module

[![ci](https://github.com/ankorstore/yokai/actions/workflows/fxhealthcheck-ci.yml/badge.svg)](https://github.com/ankorstore/yokai/actions/workflows/fxhealthcheck-ci.yml)
[![go report](https://goreportcard.com/badge/github.com/ankorstore/yokai/fxhealthcheck)](https://goreportcard.com/report/github.com/ankorstore/yokai/fxhealthcheck)
[![codecov](https://codecov.io/gh/ankorstore/yokai/graph/badge.svg?token=ghUBlFsjhR&flag=fxhealthcheck)](https://app.codecov.io/gh/ankorstore/yokai/tree/main/fxhealthcheck)
[![Deps](https://img.shields.io/badge/osi-deps-blue)](https://deps.dev/go/github.com%2Fankorstore%2Fyokai%2Ffxhealthcheck)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/ankorstore/yokai/fxhealthcheck)](https://pkg.go.dev/github.com/ankorstore/yokai/fxhealthcheck)

## Overview

Yokai provides a [fxhealthcheck](https://github.com/ankorstore/yokai/tree/main/fxhealthcheck) module, allowing your application to provide [K8s probes](https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/).

It wraps the [healthcheck](https://github.com/ankorstore/yokai/tree/main/healthcheck) module.

## Installation

The [fxhealthcheck](https://github.com/ankorstore/yokai/tree/main/fxhealthcheck) module is automatically loaded by
the [fxcore](https://github.com/ankorstore/yokai/tree/main/fxcore).

When you use a Yokai [application template](https://ankorstore.github.io/yokai/applications/templates/), you have nothing to install, it's ready to use.

## Usage

This module will enable Yokai to collect registered [CheckerProbe](https://github.com/ankorstore/yokai/blob/main/healthcheck/probe.go) implementations, and make available to the [Checker](https://github.com/ankorstore/yokai/blob/main/healthcheck/checker.go) in
its dependency injection system.

You can register probes for `startup`, `liveness` and / or `readiness` [checks](https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/).

The check result will be considered as success if ALL registered probes checks are successful.

Notes:

- to perform complex checks, you can inject dependencies to your probes implementation (ex: database, cache, etc)
- it is recommended to design your probes with a single responsibility (ex: one for database, one for cache, etc)


### Probes creation

You can create your probes by implementing the [CheckerProbe](https://github.com/ankorstore/yokai/blob/main/healthcheck/probe.go) interface.

For example:

```go title="internal/probe/success.go"
package probe

import (
"context"

"github.com/ankorstore/yokai/healthcheck"
)

type SuccessProbe struct{}

func NewSuccessProbe() *SuccessProbe {
return &SuccessProbe{}
}

func (p *SuccessProbe) Name() string {
return "successProbe"
}

func (p *SuccessProbe) Check(context.Context) *healthcheck.CheckerProbeResult {
return healthcheck.NewCheckerProbeResult(true, "success example message")
}
```

and

```go title="internal/probe/failure.go"
package probe

import (
"context"

"github.com/ankorstore/yokai/healthcheck"
)

type FailureProbe struct{}

func NewFailureProbe() *FailureProbe {
return &FailureProbe{}
}

func (p *FailureProbe) Name() string {
return "failureProbe"
}

func (p *FailureProbe) Check(context.Context) *healthcheck.CheckerProbeResult {
return healthcheck.NewCheckerProbeResult(false, "failure example message")
}
```

### Probes registration

You can register your probes for `startup`, `liveness` and / or `readiness` checks with the `AsCheckerProbe()` function:

```go title="internal/services.go"
package internal

import (
"github.com/ankorstore/yokai/fxhealthcheck"
"github.com/ankorstore/yokai/healthcheck"
"github.com/foo/bar/probe"
"go.uber.org/fx"
)

func ProvideServices() fx.Option {
return fx.Options(
// register the SuccessProbe probe for startup, liveness and readiness checks
fxhealthcheck.AsCheckerProbe(probe.NewSuccessProbe),
// register the FailureProbe probe for liveness checks only
fxhealthcheck.AsCheckerProbe(probe.NewFailureProbe, healthcheck.Liveness),
)
}
```

### Probes execution

The [fxcore](https://github.com/ankorstore/yokai/tree/main/fxcore) HTTP server will automatically:

- expose the configured health check endpoints
- use the [Checker](https://github.com/ankorstore/yokai/blob/main/healthcheck/checker.go) to run the registered probes

Following previous example:

- calling the `startup` endpoint will return a `200` response:

```json title="[GET] /healthz"
{
"success": true,
"probes": {
"successProbe": {
"success": true,
"message": "success example message"
}
}
}
```

- calling the `liveness` endpoint will return a `500` response:

```json title="[GET] /livez"
{
"success": false,
"probes": {
"successProbe": {
"success": true,
"message": "success example message"
},
"failureProbe": {
"success": false,
"message": "failure example message"
}
}
}
```

- calling the `readiness` endpoint will return a `200` response:

```json title="[GET] /readyz"
{
"success": true,
"probes": {
"successProbe": {
"success": true,
"message": "success example message"
}
}
}
```
56 changes: 30 additions & 26 deletions docs/modules/fxlog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,29 @@
[![Deps](https://img.shields.io/badge/osi-deps-blue)](https://deps.dev/go/github.com%2Fankorstore%2Fyokai%2Ffxlog)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/ankorstore/yokai/fxlog)](https://pkg.go.dev/github.com/ankorstore/yokai/fxlog)

## Overview

Yokai provides a [fxlog](https://github.com/ankorstore/yokai/tree/main/fxlog) module, allowing your application to produce logs.

It wraps the [log](https://github.com/ankorstore/yokai/tree/main/log) module, based on [Zerolog](https://github.com/rs/zerolog).

## Installation

The [fxlog](https://github.com/ankorstore/yokai/tree/main/fxlog) module is automatically loaded by
the [fxcore](https://github.com/ankorstore/yokai/tree/main/fxcore).

When you use a Yokai [application template](https://ankorstore.github.io/yokai/applications/templates/), you have nothing to install, it's ready to use.

## Documentation
## Usage

This module makes available the [Logger](https://github.com/ankorstore/yokai/blob/main/log/logger.go) in
Yokai dependency injection system.

It is built on top of `Zerolog`, see its [documentation](https://github.com/rs/zerolog) for more details about available methods.

### Configuration
You can inject the logger where needed, but it's recommended to use the one carried by the `context.Context` when possible (for automatic logs correlation).

## Configuration

This module provides the possibility to configure:

Expand All @@ -41,18 +54,9 @@ modules:
output: stdout # by default
```

### Usage
## Testing

This module makes available the [*log.Logger](https://github.com/ankorstore/yokai/blob/main/log/logger.go) in
Yokai dependency injection system.

It is built on top of [Zerolog](https://github.com/rs/zerolog), see its documentation for more details about available methods.

You can inject it where needed, but it's recommended to use the one carried by the `context.Context` when possible (for automatic logs correlation).

### Testing

This module provides the possibility to easily test your application logs, using the [logtest.TestLogBuffer](https://github.com/ankorstore/yokai/blob/main/log/logtest/buffer.go) with `modules.log.output=test`.
This module provides the possibility to easily test your application logs, using the [TestLogBuffer](https://github.com/ankorstore/yokai/blob/main/log/logtest/buffer.go) with `modules.log.output=test`.

```yaml title="configs/config.test.yaml"
app:
Expand All @@ -69,25 +73,21 @@ You can use the provided [test assertion helpers](https://github.com/ankorstore/
- `AssertContainLogRecord`: to assert on partial attributes match
- `AssertContainNotLogRecord`: to assert on partial attributes non match

You can then test, for example:
For example:

```go title="internal/some_test.go"
```go title="internal/example_test.go"
package internal_test

import (
"testing"

"github.com/ankorstore/yokai/config"
"github.com/ankorstore/yokai/fxconfig"
"github.com/ankorstore/yokai/fxlog"

"github.com/ankorstore/yokai/log"
"github.com/ankorstore/yokai/log/logtest"
"github.com/foo/bar/internal"
"go.uber.org/fx"
"go.uber.org/fx/fxtest"
)

func TestLogger(t *testing.T) {
func TestExample(t *testing.T) {
var logBuffer logtest.TestLogBuffer

internal.RunTest(
Expand All @@ -98,10 +98,14 @@ func TestLogger(t *testing.T) {
}),
)

// log assertion success
logtest.AssertHasLogRecord(t, buffer, map[string]interface{}{
"level": "debug",
"message": "test message",
})
// log assertion
logtest.AssertHasLogRecord(
t,
logBuffer,
map[string]interface{}{
"level": "debug",
"message": "test message",
},
)
}
```
Loading

0 comments on commit c6b073d

Please sign in to comment.