From 7da37c76d1209cb4918447f2093172c3169f2355 Mon Sep 17 00:00:00 2001 From: Christian Steinert Date: Fri, 31 May 2024 21:08:40 +0200 Subject: [PATCH] Application-CE: Throw on multipe `*_router` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds a check when `use_router` is called twice, or `use_router` after `no_router` or the other way around, and fail directly. This is to prohibit setting two routers, which does not have any effect… only the last router is used, which might be confusing. Fixes #323. --- src/Saturn/Application.fs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Saturn/Application.fs b/src/Saturn/Application.fs index 72d539c3..e820329f 100644 --- a/src/Saturn/Application.fs +++ b/src/Saturn/Application.fs @@ -226,7 +226,10 @@ module Application = ///Defines top-level router used for the application [] member __.Router(state, handler) = - {state with Router = Some handler} + match state.NoRouter, state.Router with + | false, None -> {state with Router = Some handler} + | true, _ -> failwith "Cannot add a router, after `no_router` was set!" + | _, Some _ -> failwith "Cannot add a second router!" ///Defines top-level endpoint router used for the application [] @@ -236,7 +239,9 @@ module Application = ///Disable warning message about lack of `router` definition. Should be used for channels-only or gRPC applications. [] member __.NoRouter(state) = - {state with NoRouter = true} + match state.Router with + | Some _ -> failwith "Cannot set `no_router` after a router with `use_router` has been set!" + | _ -> {state with NoRouter = true} ///Disables any configuration of webhost. Could be used for generic `IHostBuilder` applications not using Kestrel/IIS []