Skip to content
Snippets Groups Projects
Commit cc52022f authored by stuebinm's avatar stuebinm
Browse files

server: post map updates to exneuland's API

parent 8272b6f1
Branches
Tags
No related merge requests found
...@@ -7,6 +7,7 @@ tmpdir = "/tmp" ...@@ -7,6 +7,7 @@ tmpdir = "/tmp"
# linting interval in seconds # linting interval in seconds
interval = 30 interval = 30
exneuland = "http://localhost:4000"
[[org]] [[org]]
slug = "divoc" slug = "divoc"
......
...@@ -66,11 +66,13 @@ executables: ...@@ -66,11 +66,13 @@ executables:
- warp - warp
- wai - wai
- wai-extra - wai-extra
- lucid
- servant - servant
- servant-server - servant-server
- lucid - servant-client
- servant-lucid - servant-lucid
- http-types - http-types
- http-client
- process - process
- extra - extra
- microlens-platform - microlens-platform
......
...@@ -10,7 +10,14 @@ ...@@ -10,7 +10,14 @@
-- | simple server offering linting "as a service" -- | simple server offering linting "as a service"
module Main where module Main where
import Universum import Universum (Container (length), IO,
MVar, Monad ((>>=)),
Num ((*)), Proxy (Proxy),
Text, atomically, forM_,
forever, map, newMVar,
print, putTextLn,
readMVar, view, void,
($), (.))
import Control.Concurrent (threadDelay) import Control.Concurrent (threadDelay)
import Control.Concurrent.Async (async, link, waitEither_) import Control.Concurrent.Async (async, link, waitEither_)
...@@ -22,6 +29,8 @@ import Handlers (AdminOverview (AdminOverv ...@@ -22,6 +29,8 @@ import Handlers (AdminOverview (AdminOverv
MapService (MapService), MapService (MapService),
stateImpl, statusImpl) stateImpl, statusImpl)
import HtmlOrphans () import HtmlOrphans ()
import Network.HTTP.Client (defaultManagerSettings,
newManager)
import Network.Wai.Handler.Warp (defaultSettings, import Network.Wai.Handler.Warp (defaultSettings,
runSettings, setPort) runSettings, setPort)
import Network.Wai.Middleware.Gzip (def) import Network.Wai.Middleware.Gzip (def)
...@@ -29,32 +38,42 @@ import Network.Wai.Middleware.RequestLogger (OutputFormat (..), ...@@ -29,32 +38,42 @@ import Network.Wai.Middleware.RequestLogger (OutputFormat (..),
RequestLoggerSettings (..), RequestLoggerSettings (..),
mkRequestLogger) mkRequestLogger)
import Servant (Application, Capture, import Servant (Application, Capture,
Get, JSON, Raw, Server, EmptyAPI, Get, JSON,
serve, type (:<|>) (..), PlainText, Post, Raw,
ReqBody, Server, serve,
type (:<|>) (..),
type (:>)) type (:>))
import Servant.HTML.Lucid (HTML) import Servant.HTML.Lucid (HTML)
import Servant.Server.StaticFiles (serveDirectoryWebApp) import Servant.Server.StaticFiles (serveDirectoryWebApp)
import Server (JobStatus, Org (..), import Server (JobStatus, Org (..),
ServerState, Sha1, ServerState, Sha1,
emptyState, interval, emptyState, exneuland,
loadConfig, orgs, port, interval, loadConfig,
unState, verbose) orgs, port, unState,
verbose)
import Worker (Job (Job), linterThread) import Worker (Job (Job), linterThread)
import Servant.Client (BaseUrl (BaseUrl),
ClientM, Scheme (Http),
client, mkClientEnv,
runClientM)
-- | that thing we need to replace the hub type family PolyEndpoint method format payload where
type MapServiceAPI = "api" :> "maps" :> "list" :> Get '[JSON] MapService PolyEndpoint Get format payload = Get format payload
PolyEndpoint Post format payload = ReqBody format payload :> Post '[PlainText] Text
type MapServiceAPI method =
"api" :> "maps" :> "list" :> PolyEndpoint method '[JSON] MapService
-- | abstract api -- | abstract api
type API format = type API format =
-- "submit" :> ReqBody '[JSON] RemoteRef :> Post '[format] UUID
"status" :> Capture "org" Text :> Capture "jobid" Sha1 :> Get '[format] JobStatus "status" :> Capture "org" Text :> Capture "jobid" Sha1 :> Get '[format] JobStatus
-- :<|> "relint" :> Capture "jobid" UUID :> Get '[format] NoContent
:<|> "admin" :> "overview" :> Get '[format] AdminOverview :<|> "admin" :> "overview" :> Get '[format] AdminOverview
-- | actual set of routes: api for json & html + static pages from disk -- | actual set of routes: api for json & html + static pages from disk
type Routes = "api" :> API JSON type Routes = "api" :> API JSON
:<|> MapServiceAPI :<|> MapServiceAPI Get
:<|> API HTML -- websites mirror the API exactly :<|> API HTML -- websites mirror the API exactly
:<|> Raw :<|> Raw
...@@ -73,6 +92,9 @@ server state = jsonAPI @JSON state ...@@ -73,6 +92,9 @@ server state = jsonAPI @JSON state
app :: MVar ServerState -> Application app :: MVar ServerState -> Application
app = serve (Proxy @Routes) . server app = serve (Proxy @Routes) . server
postNewMaps :: MapService -> ClientM Text
postNewMaps = client (Proxy @(MapServiceAPI Post))
main :: IO () main :: IO ()
main = do main = do
config <- loadConfig "./config.toml" config <- loadConfig "./config.toml"
...@@ -96,10 +118,21 @@ main = do ...@@ -96,10 +118,21 @@ main = do
-- microseconds for some reason -- microseconds for some reason
threadDelay (view interval config * 1000000) threadDelay (view interval config * 1000000)
-- TODO: what about tls / https?
manager' <- newManager defaultManagerSettings
updater <- async $ forever $ do
done <- readMVar state
res <- runClientM
(postNewMaps (MapService done))
(mkClientEnv manager' (view exneuland config))
print res
threadDelay (view interval config * 1000000)
-- spawns threads for each job in the queue -- spawns threads for each job in the queue
linter <- async $ void $ linterThread config queue state linter <- async $ void $ linterThread config queue state
link linter link linter
link poker link poker
link updater
let warpsettings = let warpsettings =
setPort (view port config) setPort (view port config)
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
module Server ( loadConfig module Server ( loadConfig
, Org(..) , Org(..)
, Sha1 , Sha1
, Config, tmpdir, port, verbose, orgs, interval , Config, tmpdir, port, verbose, orgs, interval, exneuland
, RemoteRef(..) , RemoteRef(..)
, ServerState, emptyState, unState , ServerState, emptyState, unState
, JobStatus(..) , JobStatus(..)
...@@ -29,6 +29,7 @@ module Server ( loadConfig ...@@ -29,6 +29,7 @@ module Server ( loadConfig
import Universum import Universum
import CheckDir (DirResult) import CheckDir (DirResult)
import Control.Arrow ((>>>))
import Control.Concurrent (modifyMVar_, withMVar) import Control.Concurrent (modifyMVar_, withMVar)
import Crypto.Hash.SHA1 (hash) import Crypto.Hash.SHA1 (hash)
import Data.Aeson (FromJSON, ToJSON, ToJSONKey (..), import Data.Aeson (FromJSON, ToJSON, ToJSONKey (..),
...@@ -40,9 +41,14 @@ import qualified Data.Map as M ...@@ -40,9 +41,14 @@ import qualified Data.Map as M
import Lens.Micro.Platform (at, ix, makeLenses, traverseOf) import Lens.Micro.Platform (at, ix, makeLenses, traverseOf)
import LintConfig (LintConfig') import LintConfig (LintConfig')
import Servant (FromHttpApiData) import Servant (FromHttpApiData)
import Toml (TomlCodec, prettyTomlDecodeErrors, import Servant.Client (BaseUrl,
(.=)) parseBaseUrl)
import Toml (BiMap (BiMap), TomlBiMap,
TomlBiMapError (ArbitraryError),
TomlCodec,
prettyTomlDecodeErrors, (.=))
import qualified Toml as T import qualified Toml as T
import Data.Either.Extra (mapLeft)
-- | a reference in a remote git repository -- | a reference in a remote git repository
data RemoteRef = RemoteRef data RemoteRef = RemoteRef
...@@ -109,6 +115,7 @@ data Config (loaded :: Bool) = Config ...@@ -109,6 +115,7 @@ data Config (loaded :: Bool) = Config
, _verbose :: Bool , _verbose :: Bool
, _interval :: Int , _interval :: Int
-- ^ port to bind to -- ^ port to bind to
, _exneuland :: BaseUrl
, _orgs :: [Org loaded] , _orgs :: [Org loaded]
} deriving Generic } deriving Generic
...@@ -130,6 +137,11 @@ orgCodec = Org ...@@ -130,6 +137,11 @@ orgCodec = Org
<*> T.text "url" .= orgUrl <*> T.text "url" .= orgUrl
<*> T.text "webdir" .= orgWebdir <*> T.text "webdir" .= orgWebdir
-- why exactly does everything in tomland need to be invertable
urlBimap :: TomlBiMap BaseUrl String
urlBimap = BiMap
(Right . show)
(mapLeft (ArbitraryError . show) . parseBaseUrl)
configCodec :: TomlCodec (Config False) configCodec :: TomlCodec (Config False)
configCodec = Config configCodec = Config
...@@ -137,6 +149,7 @@ configCodec = Config ...@@ -137,6 +149,7 @@ configCodec = Config
<*> T.int "port" .= _port <*> T.int "port" .= _port
<*> T.bool "verbose" .= _verbose <*> T.bool "verbose" .= _verbose
<*> T.int "interval" .= _interval <*> T.int "interval" .= _interval
<*> T.match (urlBimap >>> T._String) "exneuland" .= _exneuland
<*> T.list orgCodec "org" .= _orgs <*> T.list orgCodec "org" .= _orgs
-- | a job status (of a specific uuid) -- | a job status (of a specific uuid)
......
...@@ -105,12 +105,14 @@ executable walint-server ...@@ -105,12 +105,14 @@ executable walint-server
, extra , extra
, filepath , filepath
, fmt , fmt
, http-client
, http-types , http-types
, lucid , lucid
, microlens-platform , microlens-platform
, mtl , mtl
, process , process
, servant , servant
, servant-client
, servant-lucid , servant-lucid
, servant-server , servant-server
, stm , stm
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment