Revision 423b2dd5 src/Ganeti/Monitoring/Server.hs

b/src/Ganeti/Monitoring/Server.hs
31 31
  , prepMain
32 32
  ) where
33 33

  
34
import Control.Applicative
35
import Control.Monad
34 36
import Snap.Core
35 37
import Snap.Http.Server
36
import Data.Text
38
import Data.ByteString.Char8
37 39
import qualified Text.JSON as J
38 40

  
39 41
import Ganeti.Daemon
......
79 81

  
80 82
-- | Reply to the supported API version numbers query.
81 83
versionQ :: Snap ()
82
versionQ = writeText . pack $ J.encode [latestAPIVersion]
84
versionQ = writeBS . pack $ J.encode [latestAPIVersion]
85

  
86
-- | Version 1 of the monitoring HTTP API.
87
version1Api :: Snap ()
88
version1Api =
89
  let returnNull = writeBS . pack $ J.encode J.JSNull :: Snap ()
90
  in ifTop returnNull <|>
91
     route
92
       [ ("list", listHandler)
93
       , ("report", reportHandler)
94
       ]
95

  
96
-- | Handler for returning lists.
97
listHandler :: Snap ()
98
listHandler =
99
  dir "collectors" $ writeText "TODO: return the list of collectors"
100

  
101
-- | Handler for returning data collector reports.
102
reportHandler :: Snap ()
103
reportHandler =
104
  route
105
    [ ("all", allReports)
106
    , (":category/:collector", oneReport)
107
    ]
108

  
109
-- | Return the report of all the available collectors
110
allReports :: Snap ()
111
allReports = writeText "TODO: return the reports of all the collectors"
112

  
113
-- | Return the report of one collector
114
oneReport :: Snap ()
115
oneReport = do
116
  category <- fmap (maybe mzero unpack) $ getParam "category"
117
  collector <- fmap (maybe mzero unpack) $ getParam "collector"
118
  writeBS . pack $
119
    "TODO: return the report for collector " ++ category
120
      ++ "/" ++ collector
83 121

  
84 122
-- | The function implementing the HTTP API of the monitoring agent.
85 123
-- TODO: Currently it only replies to the API version query: implement all the
86 124
-- missing features.
87 125
monitoringApi :: Snap ()
88 126
monitoringApi =
89
  ifTop versionQ
127
  ifTop versionQ <|>
128
  dir "1" version1Api
90 129

  
91 130
-- | Main function.
92 131
main :: MainFn CheckResult PrepResult
93 132
main _ _ httpConf =
94
  httpServe httpConf monitoringApi
133
  httpServe httpConf $ method GET monitoringApi

Also available in: Unified diff