Code Monkey home page Code Monkey logo

Comments (6)

chshersh avatar chshersh commented on May 23, 2024 1

@nixorn The idea looks legit. You indeed need to use hoistServerWithContext function. But instead of nt you should implement function like:

runAppAsHandler :: Env -> App a -> Handler a

And use it instead. Some example from our work:

server :: AppEnv -> Server API
server env = hoistServerWithContext
    (Proxy @API)
    (Proxy @AuthContextHandlers)
    (runAppAsHandler env)
    (toServant mpowerServer)

application :: AppEnv -> Application
application env = serveWithContext (Proxy @API) (authServerContext env) (server env)

authHandler :: AppEnv -> AuthHandler Request ()
authHandler env = mkAuthHandler (runAppAsHandler env . handler)
  where
    handler :: Request -> App ()

-- the custom monad here is called App and the environment from ReaderT is called AppEnv

from hintman.

cronokirby avatar cronokirby commented on May 23, 2024

Couldn't we already integrate the existing context into a monad for servant, and then add the Config to that context in another issue?

from hintman.

chshersh avatar chshersh commented on May 23, 2024

@cronokirby Well, I don't think that existing Context should be used as application environment. For example, port is not useful in the environment. So usually CLI arguments are called Options, and then there's function that takes CLI options, configuration file options and combines them all into application Env.

from hintman.

cronokirby avatar cronokirby commented on May 23, 2024

Ok I see

from hintman.

nixorn avatar nixorn commented on May 23, 2024

@chshersh got stuck with merging auth context + custom monad + generic servant https://github.com/kowainik/hintman/blob/10-custom-monad-in-servant/src/Hintman/Server.hs#L50
used mostly this doc https://haskell-servant.readthedocs.io/en/master/cookbook/hoist-server-with-context/HoistServerWithContext.html

Two errors. One:

Expected type: Handler x -> Handler x
Actual type: HintmanAppM x -> Handler x

As I understood it is about how to make generic servant return custom monad server instead of regular server.

Second:

    • Couldn't match kind ‘*’ with ‘[*]’                                                                                                                        
      When matching types                                                                                                                                       
        context0 :: [*]                                                                                                                                         
        HintmanAuthContext :: *                                                                                                                                 
      Expected type: Proxy context0                                                                                                                             
        Actual type: Proxy HintmanAuthContext                                                                                                                   
    • In the second argument of ‘hoistServerWithContext’, namely                                                                                                
        ‘(Proxy @HintmanAuthContext)’                                                                                                                           
      In the second argument of ‘($)’, namely                                                                                                                   
        ‘hoistServerWithContext                                                                                                                                 
           hintmanApi                                                                                                                                           
           (Proxy @HintmanAuthContext)                                                                                                                          
           (nt s)                                                                                                                                               
           (toServant hintmanServer)’                                                                                                                           
      In the expression:                                                                                                                                        
        serveWithContext hintmanApi hintmanAuthServerContext                                                                                                    
          $ hoistServerWithContext                                                                                                                              
              hintmanApi                                                                                                                                        
              (Proxy @HintmanAuthContext)                                       
              (nt s)                    
              (toServant hintmanServer) 
   |                                    
52 |              $ hoistServerWithContext hintmanApi (Proxy @HintmanAuthContext) (nt s) (toServant hintmanServer)

No idea what is it, because I don't understand what is kind yet.

from hintman.

nixorn avatar nixorn commented on May 23, 2024

@chshersh still in trouble https://github.com/kowainik/hintman/blob/10-custom-monad-in-servant/src/Hintman/Server.hs

/home/nixorn/work/kowaink/hintman/src/Hintman/Server.hs:51:21: error:
    • Couldn't match kind ‘*’ with ‘[*]’
      When matching types
        context0 :: [*]
        HintmanAuthContext :: *
      Expected type: Proxy context0
        Actual type: Proxy HintmanAuthContext
    • In the second argument of ‘hoistServerWithContext’, namely
        ‘(Proxy @HintmanAuthContext)’
      In the second argument of ‘($)’, namely
        ‘hoistServerWithContext
           (Proxy @HintmanAPI)
           (Proxy @HintmanAuthContext)
           (runAppAsHandler s)
           (toServant hintmanServer)’
      In the expression:
        serveWithContext (Proxy @HintmanAPI) hintmanAuthServerContext
          $ hoistServerWithContext
              (Proxy @HintmanAPI)
              (Proxy @HintmanAuthContext)
              (runAppAsHandler s)
              (toServant hintmanServer)
   |
51 |                    (Proxy @HintmanAuthContext)
   |                     ^^^^^^^^^^^^^^^^^^^^^^^^^

Can you explain why kinds different here? I can not get what ghc want from me)

from hintman.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.