-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
120 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# The Kos Context | ||
`kos.api.KosContext` is the backbone of the system, containing all the basic components | ||
in which Kos will interact with. To avoid misconfiguration, there will be | ||
only one instance of this object (managed by Kos) in the whole application. The | ||
only way to mutate its content is by creating a [Plugin](../kos-plugins/). | ||
|
||
## Things you can do with it | ||
- **Programmatically access injectable dependencies** - By invoking `KosContext.getImplementationLoader()` you will be able to directly interact | ||
with all [injectable dependencies found at compile time](../implementation-loaders/). If you have access to a `MutableKosContext` | ||
instance, you will also be able to define a customised dependency injection framework - replacing _Injector_ completely. | ||
- **Use a different serialization strategy** - By default, just as any other web server, Kos will always respect the HTTP Headers to infer which | ||
type of serialization to use when handling a Http Request. You can change this by passing your own `PayloadSerialisationStrategy` | ||
implementation to `MutableKosContext.setPayloadSerializationStrategy`. | ||
- **Change the default payload serializer** - When using the default serialization strategy, Kos will use JSON as default serializer whenever the response Content-Type is | ||
not defined or the Context-Type header is not present in the request. You can use the `MutableKosContext.setDefaultSerializer` | ||
to modify the serialization type. | ||
|
||
For more details, check the `KosContext` and `MutableKosContext` javadoc. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,64 @@ | ||
# Reading Configuration File | ||
Kos will use Vert.x's core api to read Yaml configuration files available in the class path. | ||
Once the configuration is read, you will have a `JsonObject` which you can interact with | ||
and read the desired configuration property. | ||
As stated by its [documentation](https://vertx.io/docs/vertx-config/java/), Vert.x provides the | ||
`vertx-config` module to efficiently interact with configuration files. Internally, | ||
this module relies on the **Config Retriever** and **Configuration store** concepts, defining | ||
"a location from where the configuration data is read and also a format (JSON by default)." | ||
|
||
By default, Kos will for a file called `application.yml` in the classpath. In case | ||
more than one is found, all `application.yml` found in the classpath will be merged | ||
before being used. | ||
As an attempt to simplify this process, Kos made the following design choices: | ||
|
||
## Reading the configuration object | ||
Reading the configuration file and transforming it into an object that can be accessed | ||
globally in the application is a common pattern nowadays. Kos provides a different approach | ||
to tackle this problem: event-driven configuration. | ||
|
||
All you will need do is to expose an implementation of the `ConfigurationLoadedListener.Event` interface. | ||
- it only looks after files named `application.yml` in the classpath. In case | ||
more than one is found, they will be merged before being used. | ||
- it fully executes the above by default, but allows one to change the default behaviour (e.g. using a | ||
different _Configuration Retriever_) | ||
- Once the configuration is read, you will have access to an `JsonObject` - just like you'd have on a | ||
typical Vert.x application. | ||
|
||
!!! note | ||
As your class is annotated with `@Exposed` annotation, you can inject other components. | ||
Check the [Injector](https://skullabs.github.io/injector) for more details. | ||
## Reading the configuration object | ||
The easiest way to interaction with the read configuration would be through Dependency Injection. | ||
You will have full access to [Kos Context](../../architecture/kos-context/), which will expose the | ||
read configuration file (Vert.x's `JsonObject`). | ||
|
||
=== "Kotlin" | ||
```kotlin | ||
@Exposed | ||
class MyAppConfigPlugin: ConfigurationLoadedEventListener { | ||
|
||
fun on(event: ConfigurationLoadedEvent) { | ||
val vertxConf = event.applicationConfig | ||
val remoteUrl = URL(vertxConf.getString("myapp.remote.url")) | ||
// do something with the `remoteUrl` | ||
```kotlin | ||
@Singleton | ||
class MyServerConfiguration( | ||
private val kosContext: KosContext | ||
) { | ||
|
||
val dbHost = kosContext.applicationConfig.getString("db.host", "localhost") | ||
val dbPort = kosContext.applicationConfig.getString("db.port", "5432") | ||
val dbUser = kosContext.applicationConfig.getString("db.user", "postgres") | ||
val dbPass = kosContext.applicationConfig.getString("db.pass", "postgres") | ||
} | ||
} | ||
``` | ||
```java | ||
@Exposed | ||
class MyAppConfigPlugin implements ConfigurationLoadedEventListener { | ||
|
||
@Override | ||
public void on(ConfigurationLoadedEvent event) { | ||
try { | ||
JsonObject vertxConf = event.getApplicationConfig(); | ||
URL remoteUrl = new URL(vertxConf.getString("myapp.remote.url")); | ||
// do something with the `remoteUrl` | ||
} catch (MalformedURLException cause) { | ||
cause.printStackTrace(); | ||
``` | ||
|
||
=== "Java" | ||
```java | ||
@Singleton | ||
class MyServerConfiguration { | ||
|
||
private final KosContext kosContext; | ||
|
||
public MyServerConfiguration(KosContext kosContext){ | ||
this.kosContext = kosContext; | ||
} | ||
|
||
public String getDbHost() { | ||
return kosContext.getApplicationConfig().getString("db.host", "localhost"); | ||
} | ||
|
||
public String getDbPort() { | ||
return kosContext.getApplicationConfig().getString("db.port", "5432"); | ||
} | ||
|
||
public String getDbUser() { | ||
return kosContext.getApplicationConfig().getString("db.user", "postgres"); | ||
} | ||
|
||
public String getDbPass() { | ||
return kosContext.getApplicationConfig().getString("db.pass", "postgres"); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
``` | ||
Another option would be listening to Kos' [internal events](../architecture/internal-events/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Event Listeners | ||
The [event bus](https://vertx.io/docs/vertx-core/java/#event_bus) is the nervous system of Vert.x. | ||
It acts as broker, where **messages** are sent on the event bus to an **address**. Listeners on | ||
these addresses can react to the incoming messages and perform bespoke code (the so-called **handlers**). | ||
|
||
Vert.x supports the following messaging patterns: | ||
|
||
- [Publish/Subscribe](https://vertx.io/docs/vertx-core/java/#_publish_subscribe_messaging) - | ||
This notification pattern allows one to have multiple listeners for each subscription address. | ||
The communication happens in uni-directionally from the publisher to all the listeners. | ||
- [Point-to-point](https://vertx.io/docs/vertx-core/java/#_point_to_point_and_request_response_messaging) - | ||
Here messages will be delivered to only one subscriber per address. If multiple listeners are registered, | ||
_"one will be chosen using a non-strict round-robin algorithm. When a message is received by a | ||
recipient, and has been handled, the recipient can optionally decide to reply to the message. | ||
If they do so, the reply handler will be called._ | ||
|
||
It is clear from the description above that the communication coordinated by the topic producer. Listeners | ||
can send replies to producers, even though they will only receive the reply | ||
|
||
## How Kos Listeners work? | ||
Kos abstracts this workflow using the `@Listener` annotation on a listener method. The method signature | ||
will define how the communication will be performed between |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters