Skip to content

Commit

Permalink
serve static index page (#1648)
Browse files Browse the repository at this point in the history
API docs are moved under `api/`, and the landing page for http://localhost:5357/ is now a static `index.html` page.

This paves the way for a JS-enabled web frontend demo.
  • Loading branch information
kostmo authored Nov 21, 2023
1 parent 27f0d08 commit 724650d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Swarm/Web.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import Data.Tree (Tree (Node), drawTree)
import Network.HTTP.Types (ok200)
import Network.Wai (responseLBS)
import Network.Wai qualified
import Network.Wai.Application.Static (defaultFileServerSettings, ssIndices)
import Network.Wai.Handler.Warp qualified as Warp
import Servant
import Servant.Docs (ToCapture)
Expand All @@ -82,6 +83,7 @@ import Swarm.TUI.Model.UI
import Swarm.Util.RingBuffer
import System.Timeout (timeout)
import Text.Read (readEither)
import WaiAppStatic.Types (unsafeToPiece)
import Witch (into)

-- ------------------------------------------------------------------
Expand All @@ -108,7 +110,10 @@ type SwarmAPI =
swarmApi :: Proxy SwarmAPI
swarmApi = Proxy

type ToplevelAPI = SwarmAPI :<|> Raw
type ToplevelAPI =
SwarmAPI
:<|> "api" :> Raw
:<|> Raw

api :: Proxy ToplevelAPI
api = Proxy
Expand Down Expand Up @@ -260,7 +265,10 @@ webMain baton port appStateRef chan = catch (Warp.runSettings settings app) hand
Nothing -> id

server :: Server ToplevelAPI
server = mkApp appStateRef chan :<|> Tagged serveDocs
server =
mkApp appStateRef chan
:<|> Tagged serveDocs
:<|> serveDirectoryWith (defaultFileServerSettings "web") {ssIndices = [unsafeToPiece "index.html"]}
where
serveDocs _ resp =
resp $ responseLBS ok200 [plain] swarmApiHtml
Expand Down
1 change: 1 addition & 0 deletions swarm.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ library
prettyprinter >= 1.7.0 && < 1.8,
random >= 1.2.0 && < 1.3,
scientific >= 0.3.6 && < 0.3.8,
wai-app-static >= 3.1.8 && < 3.1.9,
servant >= 0.19 && < 0.21,
servant-docs >= 0.12 && < 0.14,
servant-server >= 0.19 && < 0.21,
Expand Down
10 changes: 10 additions & 0 deletions web/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!doctype html>
<html>
<head>
<title>Swarm server</title>
</head>
<body>
<h1>Hello Swarm player!</h1>
<p>Looking for the <a href="api">Web API docs</a>?</p>
</body>
</html>

0 comments on commit 724650d

Please sign in to comment.