From abac581cf16425b4643e4f5196e5b8fd7d903cff Mon Sep 17 00:00:00 2001 From: Igor Pashev Date: Wed, 11 Oct 2017 22:04:30 +0300 Subject: Use TH for Process Factor it out. --- mywatch.cabal | 1 + src/Application.hs | 17 +---------------- src/Application/Types/Process.hs | 23 +++++++++++++++++++++++ 3 files changed, 25 insertions(+), 16 deletions(-) create mode 100644 src/Application/Types/Process.hs diff --git a/mywatch.cabal b/mywatch.cabal index 8b1cc5f..cc525d8 100644 --- a/mywatch.cabal +++ b/mywatch.cabal @@ -35,6 +35,7 @@ executable mywatch main-is: Main.hs other-modules: Application + Application.Types.Process LogFormat Server build-depends: diff --git a/src/Application.hs b/src/Application.hs index 5624aec..b19d116 100644 --- a/src/Application.hs +++ b/src/Application.hs @@ -1,4 +1,3 @@ -{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RecordWildCards #-} @@ -11,7 +10,6 @@ module Application import Prelude hiding (id) import Control.Monad.Trans (liftIO) -import Data.Aeson (ToJSON) import Data.Default.Class (def) import qualified Data.HashMap.Lazy as HM import Data.List (sort) @@ -19,7 +17,6 @@ import Data.Pool (Pool, withResource) import Data.Text.Lazy (Text) import Database.MySQL.Simple (Connection, Only(..), execute, query_) -import GHC.Generics (Generic) import Network.HTTP.Types (StdMethod(HEAD), notFound404, notImplemented501, ok200) import Network.Wai (Application, Middleware) @@ -35,6 +32,7 @@ import Web.Scotty (ActionM, ScottyM, addroute, delete, file, get, json, middleware, param, scottyApp, status, text) +import Application.Types.Process (Process(..)) import LogFormat (logFormat) type Pools = HM.HashMap Text (Pool Connection) @@ -59,19 +57,6 @@ myProcess ps logger dataDir = do addroute HEAD "/server/:server/processlist.json" $ apiCanProcessList ps delete "/server/:server/process/:id" $ apiKill ps -data Process = Process - { id :: Int - , user :: Text - , host :: Text - , db :: Maybe Text - , command :: Text - , time :: Int - , state :: Maybe Text - , info :: Text - } deriving (Generic) - -instance ToJSON Process - apiCanProcessList :: Pools -> ActionM () apiCanProcessList ps = do server <- param "server" diff --git a/src/Application/Types/Process.hs b/src/Application/Types/Process.hs new file mode 100644 index 0000000..2156241 --- /dev/null +++ b/src/Application/Types/Process.hs @@ -0,0 +1,23 @@ +{-# LANGUAGE TemplateHaskell #-} + +module Application.Types.Process + ( Process(..) + ) where + +import Prelude hiding (id) + +import Data.Aeson.TH (defaultOptions, deriveToJSON) +import Data.Text.Lazy (Text) + +data Process = Process + { id :: Int + , user :: Text + , host :: Text + , db :: Maybe Text + , command :: Text + , time :: Int + , state :: Maybe Text + , info :: Text + } + +$(deriveToJSON defaultOptions ''Process) -- cgit v1.2.3