2

我有几条必须按“公司”分组的路线,还有一些对所有人都有效的路线:

/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”。

4

2 回答 2

1

查看 Giraffe 文档中提出的内容(在精神上类似于 Suave)。他们使用 pathScan 与所有部分同时命名为subroutef

所以它会是这样的:

pathScan "/%s/customers" queryCustomers
pathScan "/%s/products" queryProducts
...
于 2019-02-20T11:33:27.920 回答
1

如果我理解正确:

  • 所有常见的 url 都采用以下形式:/something
  • 所有公司网址均采用以下格式:/companyName/something

然后你可以试试这个:

let appCompany company =
    let com x = "/" + company + x
    choose [      
        path (com "/ws") >=> handShake ws
        GET >=> choose [
            doReq(com "/customers", (fun r -> queryCustomers(r)))
            pathScan "/%s/customers/%d" (fun _ the_id -> doReqScan (fun r -> queryCustomer(r, the_id)))
        ]
    ]

let app =
    choose [
        allow_cors
        GET >=> choose [
                    _doReq( "/config", (fun r -> config(r)) )
                ]
        POST >=> choose [
                    _doReq( "/login", (fun r -> login(r)))
                    _doReq( "/logout", (fun r -> logout(r)))
                 ]
        pathScan "/%s/%s" (fun company _ -> appCompany company)
        GET >=> Files.browseHome
        jSonNotFound
    ]
于 2019-02-19T08:58:12.147 回答