packages feed

wai-handler-launch 0.0.1 → 0.0.2

raw patch · 3 files changed

+26/−10 lines, 3 files

Files

Network/Wai/Handler/Launch.hs view
@@ -1,7 +1,10 @@ {-# LANGUAGE OverloadedStrings #-}
 {-# LANGUAGE ForeignFunctionInterface #-}
 {-# LANGUAGE CPP #-}
-module Network.Wai.Handler.Launch (run) where
+module Network.Wai.Handler.Launch
+    ( run
+    , runUrl
+    ) where
 
 import Network.Wai
 import Network.HTTP.Types
@@ -115,27 +118,35 @@ 
 #if WINDOWS
 foreign import ccall "launch"
-    launch :: IO ()
+    launch' :: Int -> CString -> IO ()
+#endif
+
+launch :: String -> IO ()
+
+#if WINDOWS
+launch s = withCString s $ launch' 4587
 #else
-launch :: IO ()
-launch = forkIO (rawSystem
+launch s = forkIO (rawSystem
 #if MAC
     "open"
 #else
     "xdg-open"
 #endif
-    ["http://127.0.0.1:4587/"] >> return ()) >> return ()
+    ["http://127.0.0.1:4587/" ++ s] >> return ()) >> return ()
 #endif
 
 run :: Application -> IO ()
-run app = do
+run = runUrl ""
+
+runUrl :: String -> Application -> IO ()
+runUrl url app = do
     x <- newIORef True
     forkIO $ Warp.runSettings Warp.defaultSettings
         { Warp.settingsPort = 4587
         , Warp.settingsOnException = const $ return ()
         , Warp.settingsHost = "127.0.0.1"
         } $ ping x app
-    launch
+    launch url
     loop x
 
 loop :: IORef Bool -> IO ()
wai-handler-launch.cabal view
@@ -1,5 +1,5 @@ Name:                wai-handler-launch
-Version:             0.0.1
+Version:             0.0.2
 Synopsis:            Launch a web app in the default browser.
 Description:         This handles cross-platform launching and inserts Javascript code to ping the server. When the server no longer receives pings, it shuts down.
 License:             BSD3
windows.c view
@@ -1,7 +1,12 @@ #include <windows.h>
 #include <shellapi.h>
+#include <stdio.h>
 
-void launch(void)
+void launch(int port, char *s)
 {
-    ShellExecute(NULL, "open", "http://127.0.0.1:4587/", NULL, NULL, SW_SHOWNORMAL);
+    int len = 8 + strlen("http://127.0.0.1:") + strlen(s);
+    char *buff = malloc(len);
+    snprintf(buff, len, "http://127.0.0.1:%d/%s", port, s);
+    ShellExecute(NULL, "open", buff, NULL, NULL, SW_SHOWNORMAL);
+    free(buff);
 }