{- Copyright (C) 2022 Yuchen Pei. This file is part of librejserver. librejserver is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. librejserver is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with librejserver. If not, see . -} {-# LANGUAGE DataKinds #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TypeOperators #-} module Main where import Data.Proxy ( Proxy(..) ) import Data.Text ( Text ) import qualified Data.Text as T import Network.Wai.Handler.Warp ( run ) import Paths_librejserver import Servant ( (:>) , Application , CaptureAll , Get , Handler , JSON , Server , serve ) import System.IO ( hGetContents ) import System.Process ( CreateProcess(..) , createProcess , proc , readCreateProcess , readProcess ) type API = GetPageCompliance type GetPageCompliance = CaptureAll "url" Text :> Get '[JSON] Text server :: Server API server = getPageCompliance -- TODO: use runCompliance once that function is fixed. getPageCompliance :: [Text] -> Handler Text getPageCompliance urlPieces = return $ "You have requested librejs-compliance info for " <> T.intercalate "/" urlPieces -- FIXME: not working: selenium webdriver says -- Error: Server terminated early with status 127 runCompliance :: Text -> IO Text runCompliance url = do dataDir <- getDataDir T.pack <$> readCreateProcess (proc "node" ["./utilities/compliance.js", T.unpack url]) { cwd = Just (dataDir ++ "/librejs") , env = Just [("PATH", "$PATH:./node_modules/.bin")] } [] app :: Application app = serve api server api :: Proxy API api = Proxy main :: IO () main = run 5678 app