Skip to content

Commit

Permalink
Change "start" command to support automatically run migrations
Browse files Browse the repository at this point in the history
We will need this for instance when doing CD and migrations from CI.
  • Loading branch information
joseferben committed Aug 6, 2020
1 parent 2429faa commit 0609573
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
14 changes: 13 additions & 1 deletion src/app/app.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ module Sig = App_sig
let ( let* ) = Lwt_result.bind

module Make (Kernel : Sig.KERNEL) (App : Sig.APP) = struct
let start_cmd =
Cmd.make ~name:"start"
~help:
"Run \"start <with_migration>\" to first run migrations and then start \
the web server"
~description:"Start the Sihl app"
~fn:(fun _ ->
let ctx = Core.Ctx.empty in
Kernel.WebServer.start_server ctx |> Lwt.map Result.ok)
()

let start_app () =
(let ctx = Core.Ctx.empty in
Log.debug (fun m -> m "APP: Register services");
Expand All @@ -13,7 +24,8 @@ module Make (Kernel : Sig.KERNEL) (App : Sig.APP) = struct
Log.debug (fun m -> m "APP: Register routes");
let* () = Kernel.WebServer.register_routes ctx App.routes in
Log.debug (fun m -> m "APP: Register commands");
let* () = Kernel.Cmd.register_commands ctx App.commands in
let commands = List.cons start_cmd App.commands in
let* () = Kernel.Cmd.register_commands ctx commands in
Log.debug (fun m -> m "APP: Register schedules");
let _ = App.schedules |> List.map (Kernel.Schedule.schedule ctx) in
Log.debug (fun m -> m "APP: Start services");
Expand Down
9 changes: 1 addition & 8 deletions src/web/web_server_service.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,7 @@ module Make (CmdService : Cmd.Sig.SERVICE) : Web_server_sig.SERVICE = struct
let _ = Opium.Std.App.start app in
run_forever ()

let start_cmd =
Cmd.make ~name:"start" ~description:"Starts the web server"
~fn:(fun _ ->
let ctx = Core.Ctx.empty in
start_server ctx |> Lwt.map Result.return)
()

let on_init ctx = CmdService.register_command ctx start_cmd
let on_init _ = Lwt_result.return ()

let on_start _ = Lwt_result.return ()

Expand Down
2 changes: 2 additions & 0 deletions src/web/web_server_sig.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module type SERVICE = sig
include Core_container.SERVICE

val start_server : Core.Ctx.t -> unit Lwt.t

val register_routes :
Core_ctx.t ->
Web_server_core.stacked_routes ->
Expand Down
2 changes: 1 addition & 1 deletion src/web/web_utils.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
open Base

let externalize ?(prefix = Config.read_string_default ~default:"" "URL_PREFIX")
let externalize ?(prefix = Config.read_string_default ~default:"" "PREFIX_PATH")
path =
path |> String.split ~on:'/' |> List.cons prefix |> String.concat ~sep:"/"
|> String.substr_replace_all ~pattern:"//" ~with_:"/"

0 comments on commit 0609573

Please sign in to comment.