module Villefort.Server where
import Web.Scotty
import Control.Monad.Reader
import Control.Monad.IO.Class
import Control.Concurrent
import Data.List.Split
import Data.Text.Lazy hiding (splitOn,map,concat,head)
import Villefort.Database
import Villefort.Todo
import Villefort.Stats
import Villefort.Definitions
import Villefort.Weekly
import Paths_Villefort
import Villefort.Time
import Villefort.Daily
import System.IO.Strict as S
import System.Environment
import Control.Monad
import System.Process
import System.Directory
import System.Posix.Process
getWeeks ::(MonadReader VConfig m,MonadIO m) => m [[[Char]]]
getWeeks = do
rawSql <- makeQuery "select id, Title from weeks where state = 1 order by Title"
return $ Prelude.mapM (\x -> [Prelude.head x ,( Prelude.tail (Prelude.last x))]) rawSql
getIndex :: [[Char]] -> Int -> [Char]
getIndex str i = (Data.List.Split.splitOn "=" (str !! i)) !! 1
convDate :: String -> String
convDate date = newDate
where splitDate = Data.List.Split.splitOn "%2F" date
newDate = (splitDate !! 2) ++ "-" ++ (splitDate !! 0) ++ "-" ++ (splitDate !! 1)
makeRadio :: String -> String
makeRadio x = "<dd><input type='radio' name='subject' value='"++ x ++ "'> " ++ x ++ "</br> \n"
makeNewPage :: (MonadReader VConfig m, MonadIO m) => m String
makeNewPage = do
headerPath <-liftIO $ getDataFileName "templates/header"
htmlHeader <- liftIO $ S.readFile headerPath
addPath <- liftIO $ getDataFileName "templates/add.html"
add <- liftIO $ S.readFile addPath
let splitWeeks = splitOn "?" add
subjects <- getSubjects
let radiobuttons = map makeRadio subjects
return (htmlHeader ++ (splitWeeks !! 0) ++ (concat radiobuttons) ++ (splitWeeks !! 1))
villefort :: VConfig -> IO ()
villefort conf = do
args <- getArgs
case args of
["--custom",_] -> putStrLn "custom" >> launch conf
["--recompile"] -> putStrLn "recompiling" >> recompile
_ -> putStrLn "straight starting " >> do
if noCustom conf
then launch conf >> putStrLn "overload"
else checkCustomBuild >> launch conf
recompile :: IO ()
recompile = do
dir <- getAppUserDataDirectory "villefort"
let execPath = dir ++ "/villefort"
sourcePath = dir ++"/villefort.hs"
(_,_,_,pid) <- createProcess (proc "/usr/bin/ghc" ["-o",execPath,sourcePath])
_ <-
waitForProcess pid
return ()
checkCustomBuild :: IO ()
checkCustomBuild = do
dir <- getAppUserDataDirectory "villefort"
let path = dir ++ "/villefort"
putStrLn path
isBuild <- doesFileExist path
dataDir <- getDataDir
if isBuild
then putStrLn "custom buil detected" >> executeFile path True ["--custom",dataDir] Nothing
else putStrLn "no custom build :("
launch :: VConfig -> IO ()
launch conf = do
_ <- forkIO $ dailyCheck conf
scotty ( port conf) $ do
get "/" $ do
todos <- liftIO $ runReaderT getTodos conf
html $ pack $ todos
get "/new" $ do
page <- liftIO $ runReaderT makeNewPage conf
html $ pack page
post "/delete" $ do
rawHtml <- body
runReaderT (deleteTodo rawHtml) conf
redirect "/"
post "/update" $ do
rawHtml <- body
let da = Data.List.Split.splitOn "&" (show rawHtml)
do liftIO $ print $ show da
let rawid = Data.List.Split.splitOn "=" $ (Prelude.init (da !! 1))
let sqlId = read (rawid!! 1) :: Int
let rawtime = Data.List.Split.splitOn "=" $ (da !! 0)
do liftIO $ print rawtime
let insertTime = read (rawtime !! 1) :: Int
do liftIO $ runReaderT (updateTask sqlId insertTime) conf
redirect "/"
post "/add" $ do
rawBody <-body
let parse = Data.List.Split.splitOn "&" (show rawBody)
do liftIO $ print parse
let rep y = map (\x -> if x == '+' then ' ' else x) y
let summary = rep $ getIndex parse 0
let date = convDate $ getIndex parse 3
let todoTitle = rep $ getIndex parse 1
let todoSubject = rep $ getIndex parse 2
liftIO $ runReaderT (addTask todoTitle summary date todoSubject) conf
redirect "/"
get "/time" $ do
dat <-liftIO $ runReaderT getSummary conf
html $ pack dat
get "/js-chart-widgets.min.js" $ do
jsPath <- liftIO $ getDataFileName "templates/js.js"
file jsPath
get "/templates/:asset" $ do
asset <- param "asset"
path <- liftIO $ getDataFileName $ "templates/" ++ asset
file path
get "/weekly" $ do
to <- liftIO $ runReaderT weeklyStats conf
html $ pack to
get "/stat" $ do
page <- liftIO $runReaderT genStats conf
html $ pack page