我有几条必须按“公司”分组的路线,还有一些对所有人都有效的路线:
/login
/logout
/demo1/customers
/demo1/products
所以我尝试温文尔雅:
let doReqScan action =
warbler (fun r ->
LOG.Debug("ROUTE: {route}", r.request.url)
match r.request |> getCompany with
| Some(x) ->
let isAuth, validDb = isValidDb(x)
if validDb then
if isAuth then
action(r.request) |> _DoSet
else
jSonNoAuth
else
never
| _ -> never
)
let doReq(route:string, action) =
path route >=> doReqScan action
let appCompany company =
choose [
path "/ws" >=> handShake ws
GET >=> choose
[
doReq("/customers", (fun r -> queryCustomers(r)))
pathScan "/customers/%d" (fun the_id -> doReqScan (fun r -> queryCustomer(r, the_id)))
]
]
let app =
choose
[ allow_cors
pathScan "/%s" (fun company ->
choose
[
appCompany company
GET >=> choose
[
_doReq( "/config", (fun r -> config(r)) )
]
POST >=> choose
[
_doReq( "/login", (fun r -> login(r)))
_doReq( "/logout", (fun r -> logout(r)))
]
]
)
GET >=> Files.browseHome
jSonNotFound
]
然而 pathScan 捕获所有 url “/demo1/customers” 而不仅仅是“/demo1”。