aboutsummaryrefslogtreecommitdiff
path: root/app/Main.hs
blob: 8dd0f47119212e2c2191dd969a96c40a18d2137d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
{-
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 <https://www.gnu.org/licenses/>.

-}

{-# 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