XTCLANG GUIDES
How to Set Up a Web Server and Client in Ecstasy
xtclang client-server class keywords example
Authored by: Abby Sassel
Last edited: @May 6, 2024
About this guide
In this guide, we'll set up a basic web client and server example, highlighting some core Ecstasy language concepts along the way.
Topics:
- Implementing an HTTP Server and Client in Ecstasy
- Annotations with examples
- Class Keywords with examples
Target Audience
This guide is suitable for beginners to the Ecstasy language with a high-level familiarity with the client-server network model.
Prerequisites
Go to https://github.com/xtclang/xvm#installation and follow the appropriate steps to install the Ecstasy toolchain for your system.
To verify the installation, run the xec (Ecstasy execution command) and xcc (Ecstasy compilation command) in your terminal with no arguments. Both should display documentation and available options for their respective commands.
The HelloServer Module
Let's look at the working web server example:
https://github.com/xtclang/xvm/blob/master/xdk/src/main/resources/xdk/examples/HelloServer.x
Compile and run the example by executing xec from the directory containing a copy of the HelloServer.x file:
xec HelloServerYou should then see instructions in the terminal with a URL to the locally running server, e.g.:
To access this server, open a browser on this machine and enter this URL:
http://localhost:8080/
Or:
https://localhost:8090/
Use Ctrl-C to stop.The HelloClient Module
Now that the web server is up and running, we need to define a client to interact with it. Let's take a look at the example:
https://github.com/xtclang/xvm/blob/master/xdk/src/main/resources/xdk/examples/HelloClient.x
Compile and run the example by executing xec from the directory containing a copy of the HelloClient.x file:
xec HelloClientYou should see output in the terminal with the server response, e.g.
Accessing: "http://localhost:8080"
The server returned: Hello, World!Key Concepts
Let's review some high-level concepts referenced in the example code comments.
Annotations
An annotation is a mixin, which augments the corresponding structure with additional properties or behaviors. In the Ecstasy language, its syntax is @<AnnotationName>, written above the component it’s applied to. For example:
- The
@WebAppannotation enables theHelloServermodule to process HTTP requests and return a response. - The
@WebServiceannotation enables theSiteRootservice to declare a set of endpoints for a specific URI path. - The
@Getannotation determines the HTTP method, and therefore the behavior, of ourHelloWorldexample endpoint.
For more information about Annotations, check out the Understanding Types guide.
Modules
A module is the highest-level organizational unit in Ecstasy. It is the unit of compilation, the unit of versioning and the unit of deployment.
Learn more about Modules in the guide “Structuring your Ecstasy language project with modules”.
Class Keywords
service, package and module are three examples of class keywords, which refer to a set of properties, methods and behaviors for a given class.
For a deep dive into Classes and Class Keywords, take a look at “How to use class keywords in Ecstasy”.
EXPLORE & LEARN
Ready to learn more?
Check out our latest guides, blogs and tutorials!
Take me to the latest!