From 492ba39739c869d3f2b9766112232d384a145e38 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Tue, 31 Mar 2020 17:50:07 -0700 Subject: Cleanup index and _ ns handling So now _ is the special namespace that only I can post to. Unfortunately I think the guardIP function is still broken. --- Run/Que/Server.hs | 29 +++++++---------------------- Run/Que/Website.hs | 10 ++++++---- Run/Que/index.md | 4 ++-- 3 files changed, 15 insertions(+), 28 deletions(-) diff --git a/Run/Que/Server.hs b/Run/Que/Server.hs index e30a96a..ba7ac86 100644 --- a/Run/Que/Server.hs +++ b/Run/Que/Server.hs @@ -80,28 +80,12 @@ routes :: Scotty.ScottyT Text App () routes = do Scotty.middleware logStdout - let quepath = "^/([[:alnum:]_]*)/([[:alnum:]._/]*)$" - let namespace = "^/([[:alnum:]_]*)/?$" -- matches '/ns' and '/ns/' but not '/ns/path' - let index = "^(/|/index.html)$" + let quepath = "^/([[:alnum:]_]+)/([[:alnum:]._/]*)$" + let namespace = "^/([[:alnum:]_]+)/?$" -- matches '/ns' and '/ns/' but not '/ns/path' - -- | GET homepage - Scotty.get (Scotty.regex index) <| do - let (ns, qp) = ("_", ["index"]) - app . modify <| upsertNamespace ns - q <- app <| que ns qp - r <- liftIO <| takeQue q - Scotty.html <| fromStrict <| Encoding.decodeUtf8 r - - -- | POST homepage - Scotty.post (Scotty.regex index) <| do - r <- Scotty.request - guardIP r - let (ns, qp) = ("_", ["index"]) - app . modify <| upsertNamespace ns - q <- app <| que ns qp - qdata <- Scotty.body - liftIO <| pushQue (BSL.toStrict qdata) q - return () + -- | GET /index.html + Scotty.get (Scotty.literal "/index.html") <| Scotty.redirect "/_/index" + Scotty.get (Scotty.literal "/") <| Scotty.redirect "/_/index" -- | Namespace management Scotty.matchAny (Scotty.regex namespace) <| do @@ -130,7 +114,8 @@ routes = do Scotty.post (Scotty.regex quepath) <| do r <- Scotty.request (ns, qp) <- extract - when (Text.isPrefixOf "_" ns) <| guardIP r + -- "_" is a special, internal namespace that only I can use + when ("_" == ns) <| guardIP r app . modify <| upsertNamespace ns q <- app <| que ns qp qdata <- Scotty.body diff --git a/Run/Que/Website.hs b/Run/Que/Website.hs index e6b458c..8681678 100644 --- a/Run/Que/Website.hs +++ b/Run/Que/Website.hs @@ -1,4 +1,5 @@ {-# LANGUAGE RecordWildCards #-} +{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE LambdaCase #-} @@ -18,7 +19,7 @@ import qualified System.Exit as Exit main :: IO () main = do (src, ns) <- Environment.getArgs >>= \case - [src] -> return (src, "/") + [src] -> return (src, "_") -- default to _ ns which is special [src, ns] -> return (src, ns) _ -> Exit.die "usage: que-website [namespace]" @@ -42,14 +43,14 @@ loop ns pages@Sources {..} = do Async.runConcurrently $ (,,,) <$> Async.Concurrently - (toHtml style index >>= serve (https "que.run" /: ns /: "index.html")) + (toHtml style index >>= serve (https "que.run" /: ns /: "index")) <*> Async.Concurrently ( BS.readFile client - >>= serve (https "que.run" /: ns /: "_client" /: "python") + >>= serve (https "que.run" /: ns /: "client") ) <*> Async.Concurrently ( toHtml style quescripts - >>= serve (https "que.run" /: ns /: "_page" /: "quescripts") + >>= serve (https "que.run" /: ns /: "quescripts") ) loop ns pages @@ -71,6 +72,7 @@ toHtml style md = ] [] +-- TODO: recover from 502 errors serve :: Url scheme -> ByteString -> IO () serve path content = runReq defaultHttpConfig $ do _ <- req POST path (ReqBodyBs content) ignoreResponse mempty diff --git a/Run/Que/index.md b/Run/Que/index.md index 59a7ad8..ca5c7ef 100644 --- a/Run/Que/index.md +++ b/Run/Que/index.md @@ -19,7 +19,7 @@ interact with the `que.run` service. Download it to somewhere on your `$PATH` and make it executable: - curl https://que.run/_client/python > ~/bin/que + curl https://que.run/_/client > ~/bin/que chmod +x ~/bin/que que --help @@ -34,4 +34,4 @@ We are collecting a repository of scripts that make awesome use of que: - ephemeral, serverless chat rooms - collaborative jukebox -See the examples +See the examples -- cgit v1.2.3