Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the RequestHandler type generic #1

Open
luketollefson opened this issue Jul 7, 2022 · 0 comments
Open

Make the RequestHandler type generic #1

luketollefson opened this issue Jul 7, 2022 · 0 comments

Comments

@luketollefson
Copy link

luketollefson commented Jul 7, 2022

Just turning structural type RequestHandler = RequestHandler (SimpleHttpRequest ->{IO, Exception} SimpleHttpResponse) into
structural type RequestHandler g = RequestHandler (SimpleHttpRequest ->{g, IO, Exception} SimpleHttpResponse) so that the handler can include other abilities.

Then you can write a program that will count visits to each URL. The following stores the number of visits to each URL.

serveWeb : '{IO, Exception} ()
serveWeb _ =
 run =
   host = HostName "127.0.0.1"
   port = ServiceName "5560"
   requestHandler = RequestHandler storeTrial
   server host port requestHandler
 unsafeRun! ('handle (Store.withInitialValue (Map.empty) run) with Log.default "Example Http Server")

storeTrial : SimpleHttpRequest -> {IO, base.Exception, Store (Map Text Nat)} SimpleHttpResponse
storeTrial req =
 (RequestURI (Path p) oqs ourif) = requestURI req
 Store.modify (incMap p)
 okText ("hello " Text.++ p ++ " " ++ Nat.toText ((Map.getOrElse 0 p) Store.get))

incMap : a -> Map a Nat -> Map a Nat
incMap i m = Map.insert i ((Map.getOrElse 0 i m) + 1) m

Output of the PR command:

  The changes summarized below are available for you to review, using the following command:
  
    pull-request.load https://github.com/unisonweb/share:.unison.http https://github.com/luketollefson/allmyunisoncode:.prs._genericRequestHandler
  
  Updates:
  
     structural type RequestHandler
       
     ↓
     structural type RequestHandler g
       
    
     docs.example : '{IO} ()
     ↓
     docs.example : '{IO} ()
     +  #av070q37il         : Author
     +  unisoncomputing2021 : License
    
     README : Doc
     ↓
     README : Doc
    
     RequestHandler.RequestHandler : (SimpleHttpRequest ->{IO, Exception} SimpleHttpResponse)
     -> http.RequestHandler
     ↓
     RequestHandler.RequestHandler : (SimpleHttpRequest ->{g, IO, Exception} SimpleHttpResponse)
     -> RequestHandler g
    
     server : HostName -> ServiceName -> http.RequestHandler -> '{IO, Exception, Log Text} ()
     ↓
     server : HostName -> ServiceName -> RequestHandler g -> '{g, IO, Exception, Log Text} ()
     +  #av070q37il         : Author
     +  unisoncomputing2021 : License
    
     server.loop : http.RequestHandler -> Socket ->{IO, Exception, Log Text} r
     ↓
     server.loop : RequestHandler g -> Socket ->{g, IO, Exception, Log Text} r
    
     server.sendReceive : http.RequestHandler -> Socket ->{IO, Exception, Log Text} ()
     ↓
     server.sendReceive : RequestHandler g -> Socket ->{g, IO, Exception, Log Text} ()
    
     server.start : http.RequestHandler -> Socket ->{IO, Exception, Log Text} ()
     ↓
     server.start : RequestHandler g -> Socket ->{g, IO, Exception, Log Text} ()
  
    There were 1 auto-propagated updates.
  
     patch patch (added 9 updates,
    deleted 9)
  
  Added definitions:
  
     metadata.authors.lukeTollefson.guid     : GUID
     metadata.authors.lukeTollefson          : Author
     metadata.copyrightHolders.lukeTollefson : CopyrightHolder
     metadata.licenses.lukeTollefson2022     : License
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant