oxidizing / sihl

A modular functional web framework
https://v3.ocaml.org/p/sihl
MIT License
360 stars 13 forks source link

Rework service setup API #240

Closed joseferben closed 4 years ago

joseferben commented 4 years ago

Goal

There is a dynamic service setup after the static functor service setup. The API should be more modular and more ergonomic.

API

This is how the usage should look like:

module Service = struct
  module Database = Sihl.Data.Db.Service.Default
  module MigrationRepo = Sihl.Data.Migration.Service.Repo.MakeMariaDb (Database)
  module Migration = Sihl.Data.Migration.Service.Make (Database) (MigrationRepo)
  module WebServer = Sihl.Web.Server.Service.Opium
  module Schedule = Sihl.Schedule.Service.Default
end

let middlewares = [...]

let jobs = [...]

let schedules = [...]

let email_config = [("SMTP_PORT", "..."); ("SMTP_USER", "...")]

let commands = [...]

(* Some global configuration *)
let configuration = [...]

(* Custom log reporter streaming to logstash *)
let reporter = ....

(* Domain services *)
let pizza_delivery_service = Pizza_delivery.Service.configure [("DEFAULT_PIZZA": "margherita")]

let services = [
  Service.Log.configure ~reporter [("LOG_LEVEL", "debug")];
  Service.User.configure [];
  Service.Database.configure [("DATABASE_URL", "...")];
  Service.WebServer.configure
    ~middlewares
    (* Register own service so it will be started when starting the web server *)
    ~dependencies:[ pizza_delivery_service ]
    [("PORT", "3000"), ("MAX_BODY_SIZE", "10MB")];
  Service.Queue.configure ~jobs [];
  Service.Scheduler.configure ~schedules [];
  Service.Email.configure email_config;
]

let () = Sihl.App.start ~commands ~configuration services

This API has several implications:

Todo

joseferben commented 4 years ago

Fixed.