首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将Yesod网络建立在具有非空路径的url上

将Yesod网络建立在具有非空路径的url上
EN

Stack Overflow用户
提问于 2012-10-02 16:13:09
回答 2查看 250关注 0票数 2

如果我在settings.yml中将基本路径追加到approot属性,如下所示

approot="http://localhost:3000/base

在浏览器中调用此URL将“未找到”显示为消息,而不是正确地提供主页。

我找不到解决这个问题的办法。

..。

我明白了,在提交给yesod任务之前,我必须重写不带基前缀的URL,并且只能在所有的页面链接前面加上审批人。

是否有正确的方法来处理基于URL的非空路径?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-02 18:11:06

对此有两种相对简单的方法:

  1. 创建一个中间件,它将从pathInfo中去掉领先的“基本”值。
  2. 重写cleanPath (在Yesod类型中)来执行同样的操作。

一般来说,如果有一些前端服务器正在调用您的Yesod应用程序(无论是通过反向代理还是通过FastCGI),具有领先路径部分的许可程序是最有用的。

票数 3
EN

Stack Overflow用户

发布于 2012-10-03 14:01:20

下面是M.Snoyman提示,下面是解决这个问题的cleanPath版本,仅用于yesod init生成的ApprootMaster构造函数的接近情况,作为生成模块Foundation.hs的一个补充:

代码语言:javascript
运行
复制
{-# LANGUAGE PackageImports #-}
import qualified Data.Text as Txt
import qualified "url" Network.URL as Url
import qualified Network.HTTP.Types as H
import qualified Data.Text.Encoding as TE
import qualified Data.List as L
import qualified "utf8-string" Data.ByteString.UTF8 as UTF8BS

-- instance Yesod App where

    -- cleanPath - A function used to clean up path segments. 
    -- It returns Right with a clean path or Left with a new set of pieces the user should be redirected to

    -- cleanPath :: Yesod a => a -> [Text] -> Either [Text] [Text]
    cleanPath master s =
        if corrected == s'
            then Right $ cutoffBasePrefix s'
            else Left $ cutoffBasePrefix corrected   -- to be redirected
      where
        -- avoid redirection on slash ended urls by junking the last piece if it's null

        s' = if not (L.null s) && Txt.null (last s) then init s else s

        corrected = filter (not . Txt.null) s'

        -- strToUtf8BS = TE.encodeUtf8 . Txt.pack  -- packs to UTF16 then encodes to UTF8
        strToUtf8BS = UTF8BS.fromString

        -- cut off "base prefix" or leave as it is

        cutoffBasePrefix segmts =
                case approot of
                     ApprootMaster f ->
                         case Url.importURL $ Txt.unpack $ f master of
                              Nothing -> segmts   -- not expected, incorrect url in settings.yml approot
                              Just url -> let basePrefixSegmts = H.decodePathSegments $ strToUtf8BS $ Url.url_path url in
                                          case basePrefixSegmts of
                                               [] -> segmts
                                               _ -> if basePrefixSegmts `L.isPrefixOf` segmts
                                                       then drop (length basePrefixSegmts) segmts
                                                       else segmts


                     _ -> segmts

对于附加的包依赖项:

代码语言:javascript
运行
复制
, url                           >= 2.1.2
, network
, http-types
, utf8-string
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12694140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档