on master help sign in
runUrl
commit5b32c8ebc4e2deb9b95240f03cf1b327dd674177 authorMichael <michael@snoyman.com> dateSun 10 Jul 2011, 14:04 (15 years ago) parent1d07e82
4 files changed+27−11
wai-handler-launch/Network/Wai/Handler/Launch.hs+18−7 wai-handler-launch/test.hs+1−1 wai-handler-launch/wai-handler-launch.cabal+1−1 wai-handler-launch/windows.c+7−2
wai-handler-launch/Network/Wai/Handler/Launch.hs+18−7
@@ -1,7 +1,10 @@
11{-# LANGUAGE OverloadedStrings #-}
22{-# LANGUAGE ForeignFunctionInterface #-}
33{-# LANGUAGE CPP #-}
4−module Network.Wai.Handler.Launch (run) where
4+module Network.Wai.Handler.Launch
5+ ( run
6+ , runUrl
7+ ) where
58
69import Network.Wai
710import Network.HTTP.Types
@@ -115,27 +118,35 @@ fixHeaders front (x:xs) = fixHeaders (front . (:) x) xs
115118
116119#if WINDOWS
117120foreign import ccall "launch"
118− launch :: IO ()
121+ launch' :: Int -> CString -> IO ()
122+#endif
123+
124+launch :: String -> IO ()
125+
126+#if WINDOWS
127+launch s = withCString s $ launch' 4587
119128#else
120−launch :: IO ()
121−launch = forkIO (rawSystem
129+launch s = forkIO (rawSystem
122130#if MAC
123131 "open"
124132#else
125133 "xdg-open"
126134#endif
127− ["http://127.0.0.1:4587/"] >> return ()) >> return ()
135+ ["http://127.0.0.1:4587/" ++ s] >> return ()) >> return ()
128136#endif
129137
130138run :: Application -> IO ()
131−run app = do
139+run = runUrl ""
140+
141+runUrl :: String -> Application -> IO ()
142+runUrl url app = do
132143 x <- newIORef True
133144 forkIO $ Warp.runSettings Warp.defaultSettings
134145 { Warp.settingsPort = 4587
135146 , Warp.settingsOnException = const $ return ()
136147 , Warp.settingsHost = "127.0.0.1"
137148 } $ ping x app
138− launch
149+ launch url
139150 loop x
140151
141152loop :: IORef Bool -> IO ()
wai-handler-launch/test.hs+1−1
@@ -4,4 +4,4 @@ import Network.HTTP.Types
44import Network.Wai.Handler.Launch
55import Network.Wai.Middleware.Gzip
66
7−main = run $ gzip True $ \_ -> return $ responseLBS status200 [("Content-Type", "text/html; charset=utf8")] "<html><head></head><body>HELLO THERE"
7+main = runUrl "FIXME" $ gzip True $ \_ -> return $ responseLBS status200 [("Content-Type", "text/html; charset=utf8")] "<html><head></head><body>HELLO THERE"
wai-handler-launch/wai-handler-launch.cabal+1−1
@@ -1,5 +1,5 @@
11Name: wai-handler-launch
2−Version: 0.0.1
2+Version: 0.0.2
33Synopsis: Launch a web app in the default browser.
44Description: This handles cross-platform launching and inserts Javascript code to ping the server. When the server no longer receives pings, it shuts down.
55License: BSD3
wai-handler-launch/windows.c+7−2
@@ -1,7 +1,12 @@
11#include <windows.h>
22#include <shellapi.h>
3+#include <stdio.h>
34
4−void launch(void)
5+void launch(int port, char *s)
56{
6− ShellExecute(NULL, "open", "http://127.0.0.1:4587/", NULL, NULL, SW_SHOWNORMAL);
7+ int len = 8 + strlen("http://127.0.0.1:") + strlen(s);
8+ char *buff = malloc(len);
9+ snprintf(buff, len, "http://127.0.0.1:%d/%s", port, s);
10+ ShellExecute(NULL, "open", buff, NULL, NULL, SW_SHOWNORMAL);
11+ free(buff);
712}