Skip to content

Make the RequestHandler type generic #1

Open
@luketollefson

Description

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

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions