我希望我的应用程序的客户端ID和密码ID不属于我的源代码。因此,如果我在运行时从环境变量中读取这些设置,它们将始终包含在IO中。这导致了一个问题,因为它们将无法构成:
let menu = NSMenuItem(title: "Test", action: #selector(AppDelegate.test), keyEquivalent: "K")
menu.attributedTitle = NSAttributedString(string: "Testinggg", attributes: [NSFontAttributeName: NSFont.systemFontOfSize(20), NSForegroundColorAttributeName: NSColor.redColor()])
mainMenu?.itemAtIndex(0)?.submenu?.addItem(menu)//You can add whichever submenu
提出此问题的另一种方法是:如何在http://www.yesodweb.com/book/authentication-and-authorization
中给出的第一个示例代码中从环境中阅读authPlugins :: master -> [AuthPlugin master]
authGoogleEmail :: YesodAuth m => Text -> Text -> AuthPlugin m
getEnv :: String -> IO String
和clientId
答案 0 :(得分:5)
未经测试,但这应该有效:完整示例:http://lpaste.net/167997
在您的App记录中为客户端ID和密码添加字段:
data App = App { ...
, gmailClientId :: Text
, gmailClientSecret :: Text
}
修改authPlugins方法以从App记录中查找客户端ID和机密值:
instance YesodAuth App where
...
authPlugins app = [ ...
, authGoogleEmail (gmailClientId app) (gmailClientSecret app)
]
在致电main
之前,在warp
初始化应用记录:
main = do
clientId <- getEnv "CLIENT_ID"
clientSecret <- getEnv "CLIENT_SECRET"
...
let app = App { ..., gmailClientId = clientId, gmailClientSecret = clientSecret }
warp 3000 app