Chapter 37: the smallest web service, built

A web service in EK9 is a construct, not annotations on a class. Chapter 37 builds one a step at a time, starting with the smallest: one route that greets by name, registered in an application and served by a program that listens on the loopback address only, and stops cleanly when it is sent TERM. This lesson builds that first service. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-hello-notes.

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.notesservice.hello

  defines constant
    HELLO_PORT <- 8083

    STOP_SIGNAL <- "TERM"

  defines service

    Greeting for :/greeting open

      hello() as GET for :/{name}
        -> personName as String :=: PATH "name"
        <- response as HTTPResponse: (who: personName) with trait of HTTPResponse
          override status() as pure
            <- rtn as Integer: 200
          override content()
            <- rtn as Bytes: Bytes().withUtf8(`hello ${who}, the notes service is running`)
          override contentType() as pure
            <- rtn as String: "text/plain"
          default operator ?

  defines application

    GreetingApp
      register Greeting()

  defines program

    ServeGreeting() with application of GreetingApp
      stdout <- Stdout()

      try
        -> server <- HTTP(NetworkProperties(port: HELLO_PORT, localOnly: true))

        stopOnTerm <- (server, stdout) is SignalHandler as function
          result: Integer()
          stdout.println(`received ${signal}, stopping`)
          server.stop()

        Signals().register(STOP_SIGNAL, stopOnTerm)
        stdout.println(`serving on port ${HELLO_PORT}`)
        if server.serve()
          stdout.println("stopped, in-flight requests drained")
      stdout.println("the try has closed the server")

//EOF

Step 1: Constants

Choose Code > New Construct > Constant…

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

HELLO_PORT is the constant's name and 8083 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: HELLO_PORT is the constant's name and 8083 its value - a literal in the Value field, from which the type is inferred

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

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

Choose Code > New Construct > Constant…

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

STOP_SIGNAL is the constant's name and "TERM" 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: STOP_SIGNAL is the constant's name and "TERM" its value - a literal in the Value field, from which the type is inferred

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

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

Step 2: Greeting — a service

Choose Code > New Construct > Service…

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

Greeting is the service's name and /greeting the base URI every route sits under. The route's parameter personName takes its value from the path variable name. The Routes table holds hello, GET /{name}, taking personName String PATH name. Choose Preview.

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

The preview shows Greeting about to be written, with its route, answering with an HTTPResponse the tool wrote. Choose Apply.

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

Select the Greeting 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 Greeting line and, above it, type the blank line the book opens the block with

The tool wrote a response that echoes the parameter. What the route SAYS is yours: select the response, from its first line down to the content line, and type the book's - the capture named who, a greeting, and a text/plain content type. Saves when typed.

The EK9 IDE once this is done: The tool wrote a response that echoes the parameter

Step 3: GreetingApp — an application

Choose Code > New Construct > Application…

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

GreetingApp is the application's name. It registers the service. There is no scanning: a service that is not registered is not served. The Registrations table holds Greeting. Choose Preview.

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

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

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

Select the GreetingApp 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 GreetingApp line and, above it, type the blank line the book opens the block with

Step 4: ServeGreeting — a program

Choose Code > New Construct > Program…

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

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

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

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

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

Select the ServeGreeting 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 ServeGreeting 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

HelloNotes.ek9 now reads exactly as chapter 37 of EK9 in Depth shows it. The route's parameter has its own name, personName, and takes the value from the path variable name; the server is created localOnly, so it can only be reached from this machine. The program is built, not run: it serves until sent TERM.

Where this lesson comes from