{-# LANGUAGE StandaloneKindSignatures #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE AllowAmbiguousTypes #-} -- why? module Lib where import Data.Kind (Type) type GetContext :: Type -> Type type family GetContext route class Route route where type GetRequest route type GetResponse route -- how we know which route to use requestKey :: route -> String -- parse the route into object, should be an either -- to signifiy this failed to deserialize for some reason requestParse :: route -> String -> GetRequest route -- transform request into response (will later return a new context as well) handle :: route -> GetRequest route -> GetResponse route -- handle :: GetContext route -> GetRequest route -> (GetContext route, GetResponse route) -- turn response object into string (will be json irl) responseSerialize :: route -> GetResponse route -> String data InitRoute = InitRoute data InitRequest = InitRequest data InitResponse = InitResponse instance Route InitRoute where type GetRequest InitRoute = InitRequest type GetResponse InitRoute = InitResponse requestKey _ = "init" requestParse _ _ = InitRequest handle _ InitRequest = InitResponse responseSerialize _ InitResponse = "init_ok" data ReadRoute = ReadRoute data ReadRequest = ReadRequest data ReadResponse = ReadResponse instance Route ReadRoute where type GetRequest ReadRoute = ReadRequest type GetResponse ReadRoute = ReadResponse requestKey _ = "read" requestParse _ _ = ReadRequest handle _ ReadRequest = ReadResponse responseSerialize _ ReadResponse = "read_ok" -- go through all the routes, see which ones match -- once we find a matching one return that type routeHandler :: [RouteHolder] -> (String, String) -> IO String routeHandler routes (reqType, reqPayload) = do -- irl add error handling case filter (\(RouteHolder route) -> requestKey route == reqType) routes of RouteHolder routeMatch:_ -> do let parsedRequest = requestParse routeMatch reqPayload let response = handle routeMatch parsedRequest let serializedResponse = responseSerialize routeMatch response pure serializedResponse _ -> pure "" -- how to solve this?? {- Lists in haskell have to homogenous so we can't make this we can create a sum type to contain all the routes but do I want that? I tried creating a routeholder but that didn't work -} data RouteHolder where RouteHolder :: forall r . Route r => r -> RouteHolder myRoutes :: [RouteHolder] myRoutes = [RouteHolder InitRoute, RouteHolder ReadRoute] fakeLines :: [String] fakeLines = ["init", "read", "something"] -- fake reading server :: Int -> IO () server lineNum = do let req = fakeLines !! lineNum resp <- routeHandler myRoutes (req, "") -- would be json not empty string irl print resp server (lineNum + 1) someFunc :: IO () someFunc = putStrLn "someFunc"