MarginaliaSearch/code/common/service/readme.md

58 lines
1.8 KiB
Markdown
Raw Normal View History

2023-03-06 17:32:13 +00:00
# Service
Contains the base classes for the services. This is where port configuration,
2024-02-28 14:19:31 +00:00
and common endpoints are set up. The service base class is additionally responsible for
registering the service with the service registry, and announcing its liveness.
2023-03-06 17:32:13 +00:00
## Creating a new Service
The minimal service needs a `MainClass` and a `Service` class.
2024-02-28 14:19:31 +00:00
For proper initiation, the main class should look like the below. It will
create a Guice injector, and then create an instance of the service, and take
care of loading configuration properties and setting up loggers in a sane
way.
```java
public class FoobarMain extends MainClass {
@Inject
public FoobarMain(FoobarService service) {}
public static void main(String... args) {
init(ServiceId.Foobar, args);
Injector injector = Guice.createInjector(
new FoobarModule(), /* optional custom bindings go here */
new DatabaseModule(),
new ConfigurationModule(ServiceId.Foobar));
injector.getInstance(FoobarMain.class);
// set the service as ready so that delayed tasks can be started
injector.getInstance(Initialization.class).setReady();
}
}
```
A service class has a boilerplate set-up that looks like this:
```java
@Singleton
public class FoobarService extends Service {
@Inject
public FoobarService(BaseServiceParams params) {
super(params, List.of(/* grpc services */));
2024-02-28 14:19:31 +00:00
// set up any Spark endpoints here
}
}
```
2024-02-28 14:19:31 +00:00
The service should also be given a canonical name in the `ServiceId` enum.
2023-03-06 17:32:13 +00:00
## Central Classes
* [MainClass](java/nu/marginalia/service/MainClass.java) bootstraps all executables
* [Service](java/nu/marginalia/service/server/Service.java) base class for all services.