on master help sign in
runUrl
5b32c8eMichael
wai-handler-launch/Network/Wai/Handler/Launch.hs+18−7
@@ -1,7 +1,10 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE ForeignFunctionInterface #-}
2{-# LANGUAGE ForeignFunctionInterface #-}
3{-# LANGUAGE CPP #-}
3{-# LANGUAGE CPP #-}
4−module Network.Wai.Handler.Launch (run) where
4+module Network.Wai.Handler.Launch
5+ ( run
6+ , runUrl
7+ ) where
5
8
6import Network.Wai
9import Network.Wai
7import Network.HTTP.Types
10import Network.HTTP.Types
@@ -115,27 +118,35 @@ fixHeaders front (x:xs) = fixHeaders (front . (:) x) xs
115
118
116#if WINDOWS
119#if WINDOWS
117foreign import ccall "launch"
120foreign 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
119#else
128#else
120−launch :: IO ()
121−launch = forkIO (rawSystem
129+launch s = forkIO (rawSystem
122#if MAC
130#if MAC
123 "open"
131 "open"
124#else
132#else
125 "xdg-open"
133 "xdg-open"
126#endif
134#endif
127− ["http://127.0.0.1:4587/"] >> return ()) >> return ()
135+ ["http://127.0.0.1:4587/" ++ s] >> return ()) >> return ()
128#endif
136#endif
129
137
130run :: Application -> IO ()
138run :: Application -> IO ()
139+run = runUrl ""
140+
141+runUrl :: String -> Application -> IO ()
131−run app = do
142+runUrl url app = do
132 x <- newIORef True
143 x <- newIORef True
133 forkIO $ Warp.runSettings Warp.defaultSettings
144 forkIO $ Warp.runSettings Warp.defaultSettings
134 { Warp.settingsPort = 4587
145 { Warp.settingsPort = 4587
135 , Warp.settingsOnException = const $ return ()
146 , Warp.settingsOnException = const $ return ()
136 , Warp.settingsHost = "127.0.0.1"
147 , Warp.settingsHost = "127.0.0.1"
137 } $ ping x app
148 } $ ping x app
138− launch
149+ launch url
139 loop x
150 loop x
140
151
141loop :: IORef Bool -> IO ()
152loop :: IORef Bool -> IO ()
wai-handler-launch/test.hs+1−1
@@ -4,4 +4,4 @@ import Network.HTTP.Types
4import Network.Wai.Handler.Launch
4import Network.Wai.Handler.Launch
5import Network.Wai.Middleware.Gzip
5import Network.Wai.Middleware.Gzip
6
6
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 @@
1Name: wai-handler-launch
1Name: wai-handler-launch
2−Version: 0.0.1
2+Version: 0.0.2
3Synopsis: Launch a web app in the default browser.
3Synopsis: Launch a web app in the default browser.
4Description: This handles cross-platform launching and inserts Javascript code to ping the server. When the server no longer receives pings, it shuts down.
4Description: This handles cross-platform launching and inserts Javascript code to ping the server. When the server no longer receives pings, it shuts down.
5License: BSD3
5License: BSD3
wai-handler-launch/windows.c+7−2
@@ -1,7 +1,12 @@
1#include <windows.h>
1#include <windows.h>
2#include <shellapi.h>
2#include <shellapi.h>
3+#include <stdio.h>
3
4
4−void launch(void)
5+void launch(int port, char *s)
5{
6{
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);
6− ShellExecute(NULL, "open", "http://127.0.0.1:4587/", NULL, NULL, SW_SHOWNORMAL);
10+ ShellExecute(NULL, "open", buff, NULL, NULL, SW_SHOWNORMAL);
11+ free(buff);
7}
12}