From 2b30d27669ed6d758b2b4cd04dada4ef8ab3caee Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Sun, 7 Aug 2022 21:26:17 -0700
Subject: [PATCH] Rename pandoc-cgi -> pandoc-server.

---
 .github/workflows/release-candidate.yml       |  2 +-
 pandoc-cgi/Main.hs                            |  8 ----
 pandoc.cabal                                  | 16 +++----
 server/Main.hs                                | 42 +++++++++++++++++++
 .../PandocCGI.hs => server/PandocServer.hs    |  2 +-
 5 files changed, 53 insertions(+), 17 deletions(-)
 delete mode 100644 pandoc-cgi/Main.hs
 create mode 100644 server/Main.hs
 rename pandoc-cgi/PandocCGI.hs => server/PandocServer.hs (99%)

diff --git a/.github/workflows/release-candidate.yml b/.github/workflows/release-candidate.yml
index 6c4c6cf97..a8e829a11 100644
--- a/.github/workflows/release-candidate.yml
+++ b/.github/workflows/release-candidate.yml
@@ -20,7 +20,7 @@ jobs:
           mkdir linux-release-candidate
           cp linux/artifacts/*.deb linux-release-candidate/
           cp linux/artifacts/*.tar.gz linux-release-candidate/
-          cp linux/artifacts/pandoc-cgi linux-release-candidate/
+          cp linux/artifacts/pandoc-server linux-release-candidate/
     - uses: actions/upload-artifact@v2
       with:
         name: linux-release-candidate
diff --git a/pandoc-cgi/Main.hs b/pandoc-cgi/Main.hs
deleted file mode 100644
index d83edd2a3..000000000
--- a/pandoc-cgi/Main.hs
+++ /dev/null
@@ -1,8 +0,0 @@
-module Main where
-
-import PandocCGI (app)
-import Network.Wai.Handler.CGI (run)
-import Network.Wai.Middleware.Timeout (timeout)
-
-main :: IO ()
-main = run $ timeout 2 app
diff --git a/pandoc.cabal b/pandoc.cabal
index 44ec59510..cab4e375c 100644
--- a/pandoc.cabal
+++ b/pandoc.cabal
@@ -428,8 +428,8 @@ flag lua53
   Description:   Embed Lua 5.3 instead of 5.4.
   Default:       False
 
-flag pandoc-cgi
-  Description:   Build pandoc-cgi executable.
+flag server
+  Description:   Build pandoc-server executable.
   Default:       False
 
 flag nightly
@@ -789,19 +789,21 @@ executable pandoc
   buildable:       True
   other-modules:   Paths_pandoc
 
-executable pandoc-cgi
+executable pandoc-server
   import:          common-executable
   main-is:         Main.hs
-  other-modules:   PandocCGI
-  hs-source-dirs:  pandoc-cgi
-  if flag(pandoc-cgi)
+  other-modules:   PandocServer
+  hs-source-dirs:  server
+  if flag(server)
     build-depends: base,
                    pandoc,
                    aeson,
                    text,
                    servant-server,
                    wai >= 0.3,
-                   wai-extra >= 3.0.24
+                   wai-extra >= 3.0.24,
+                   warp,
+                   optparse-applicative
 
     buildable:     True
   else
diff --git a/server/Main.hs b/server/Main.hs
new file mode 100644
index 000000000..4266519c7
--- /dev/null
+++ b/server/Main.hs
@@ -0,0 +1,42 @@
+module Main where
+
+import PandocServer (app)
+import qualified Network.Wai.Handler.CGI as CGI
+import qualified Network.Wai.Handler.Warp as Warp
+import Network.Wai.Middleware.Timeout (timeout)
+import System.Environment (getProgName)
+import Options.Applicative
+
+data Opts = Opts
+  { optPort :: Warp.Port,
+    optTimeout :: Int }  -- in seconds
+
+options :: Parser Opts
+options = Opts
+  <$> option auto
+         ( long "port"
+         <> value 3030
+         <> metavar "PORT"
+         <> help "Port to serve on" )
+  <*> option auto
+         ( long "timeout"
+         <> value 2
+         <> metavar "SECONDS"
+         <> help "Seconds timeout" )
+
+main :: IO ()
+main = do
+  progname <- getProgName
+  let optspec = info (options <**> helper)
+       ( fullDesc
+       <> progDesc "Run a pandoc server"
+       <> header "pandoc-server - text conversion server" )
+  opts <- execParser optspec
+
+  let port = optPort opts
+  let app' = timeout (optTimeout opts) app
+  if progname == "pandoc-server.cgi"
+     then -- operate as a CGI script
+       CGI.run app'
+     else -- operate as a persistent server
+       Warp.run port app'
diff --git a/pandoc-cgi/PandocCGI.hs b/server/PandocServer.hs
similarity index 99%
rename from pandoc-cgi/PandocCGI.hs
rename to server/PandocServer.hs
index 1789f34a5..5b02c4ea3 100644
--- a/pandoc-cgi/PandocCGI.hs
+++ b/server/PandocServer.hs
@@ -3,7 +3,7 @@
 {-# LANGUAGE TypeOperators   #-}
 {-# LANGUAGE FlexibleContexts #-}
 {-# LANGUAGE OverloadedStrings #-}
-module PandocCGI
+module PandocServer
     ( app
     , Params(..)
     ) where