Chapter 37: two routes that could both match, built

Which route answers /words/search - the one with a variable or the one with the fixed path? Chapter 37's RouteOrder asks the question with two routes. This lesson builds the service and the program that serves it. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-route-order.

The EK9 IDE as the lesson opens

What you'll build

The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.

#!ek9
defines module book.routeorder

  defines constant
    ORDER_PORT <- 8082

  defines function

    plain()
      -> text as String
      <- response as HTTPResponse: (text) with trait of HTTPResponse
        override status() as pure
          <- rtn as Integer: 200
        override content()
          <- rtn as Bytes: Bytes().withUtf8(text)
        override contentType() as pure
          <- rtn as String: "text/plain"
        default operator ?

  defines service

    Words for :/words open

      byNumber() as GET for :/{number}
        -> number as Integer :=: PATH "number"
        <- response as HTTPResponse: plain(`word number ${number}`)

      search() as GET for :/search
        -> query as String :=: QUERY "query"
        <- response as HTTPResponse: plain(`searching for ${query}`)

  defines application

    WordsApp
      register Words()

  defines program

    ServeWords() with application of WordsApp
      try
        -> server <- HTTP(NetworkProperties(port: ORDER_PORT, localOnly: true))
        server.serve()

//EOF

Step 1: A constant

Choose Code > New Construct > Constant…

The EK9 IDE once this is done: Choose Code > New Construct > Constant…

ORDER_PORT is the constant's name and 8082 its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: ORDER_PORT is the constant's name and 8082 its value - a literal in the Value field, from which the type is inferred

The preview shows ORDER_PORT about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows ORDER_PORT about to be written

Step 2: plain — a function

Choose Code > New Construct > Function…

The EK9 IDE once this is done: Choose Code > New Construct > Function…

plain is the function's name. Add one parameter, text of type String, set Returns to String. Choose Preview.

The EK9 IDE just before choosing Preview: plain is the function's name

The preview shows plain about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows plain about to be written

Select the plain() line and, above it, type the blank line the book opens its first function with. Saves when typed.

The EK9 IDE once this is done: Select the plain() line and, above it, type the blank line the book opens its first function with

Select the stub, from <- rtn as String? down to its //AI line, and type plain's own return and work, as the book writes them. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as String? down to its //AI line, and type plain's own return and work, as the book writes them

Step 3: Words — a service

Choose Code > New Construct > Service…

The EK9 IDE once this is done: Choose Code > New Construct > Service…

Words is the service's name and /words the base URI every route sits under. Two routes: one with a path variable, one with a fixed path. Both could match /words/search; the order is the question. The Routes table holds byNumber, GET /{number}, taking number Integer PATH number; search, GET /search, taking query String QUERY query. Choose Preview.

The EK9 IDE just before choosing Preview: Words is the service's name and /words the base URI every route sits under

The preview shows Words about to be written, with its routes, each answering with an HTTPResponse the tool wrote. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Words about to be written, with its routes, each answering with an HTTPResponse the tool wrote

Select the Words line and, above it, type the blank line the book opens the block with. Saves when typed.

The EK9 IDE once this is done: Select the Words line and, above it, type the blank line the book opens the block with

The tool wrote a response for each route; the book has both answer through a plain function you write next. Select byNumber's response, from its first line down to the default operator, and type the one line. Saves when typed.

The EK9 IDE once this is done: The tool wrote a response for each route; the book has both answer through a plain function you write next

And the same for search. Saves when typed.

The EK9 IDE once this is done: And the same for search

Step 4: WordsApp — an application

Choose Code > New Construct > Application…

The EK9 IDE once this is done: Choose Code > New Construct > Application…

WordsApp is the application's name. It registers the service. The Registrations table holds Words. Choose Preview.

The EK9 IDE just before choosing Preview: WordsApp is the application's name

The preview shows WordsApp about to be written, with its register line. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows WordsApp about to be written, with its register line

Select the WordsApp line and, above it, type the blank line the book opens the block with. Saves when typed.

The EK9 IDE once this is done: Select the WordsApp line and, above it, type the blank line the book opens the block with

Step 5: ServeWords — a program

Choose Code > New Construct > Program…

The EK9 IDE once this is done: Choose Code > New Construct > Program…

ServeWords is the program's name. Leave Application empty - a later step says why. Choose Preview.

The EK9 IDE just before choosing Preview: ServeWords is the program's name

The preview shows ServeWords about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows ServeWords about to be written

Select the ServeWords line and, above it, type the blank line the book opens the block with. Saves when typed.

The EK9 IDE once this is done: Select the ServeWords line and, above it, type the blank line the book opens the block with

The tool cannot write a program bound to an application that registers a service - the program must create the HTTP server, which no stub can - so it wrote it unbound. Select the stub, from the program's header down to its //AI line, and type the header with its application and the body that serves, on this machine only. Saves when typed.

The EK9 IDE once this is done: The tool cannot write a program bound to an application that registers a service - the program must create the HTTP server, which no stub can - so it wrote it unbound

The result

RouteOrder.ek9 now reads exactly as chapter 37 of EK9 in Depth shows it: two routes answering through one small function. The program is built, not run: it serves until sent TERM.

Where this lesson comes from