Quick Start

Simple HTTP service

In this guide, we are going to create a sample HTTP service. You can read the Core or HTTP Server modules documentation for more information.

You can start by cloning a starter project (Gradle Starter or Maven Starter). Or you can create a project from scratch following these steps:

  1. Configure Kotlin in Gradle or Maven.
  2. Setup the JCenter and Hexagon repositories (follow the links and click on the Set me up! button).
1
2
3
repositories {
    maven { url  "https://dl.bintray.com/hexagonkt/hexagon" }
}
1
2
3
4
5
6
<repositories>
  <repository>
    <id>hexagon</id>
    <url>https://dl.bintray.com/hexagonkt/hexagon</url>
  </repository>
</repositories>
  1. Add the dependency in Gradle or Maven:
1
implementation("com.hexagonkt:http_server_jetty:$hexagonVersion")
1
2
3
4
5
<dependency>
  <groupId>com.hexagonkt</groupId>
  <artifactId>http_server_jetty</artifactId>
  <version>$hexagonVersion</version>
</dependency>
  1. Write the code in the src/main/kotlin/Hello.kt file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.hexagonkt.starters

import com.hexagonkt.helpers.logger
import com.hexagonkt.http.httpDate
import com.hexagonkt.http.server.Server
import com.hexagonkt.http.server.ServerPort
import com.hexagonkt.http.server.jetty.JettyServletAdapter
import com.hexagonkt.injection.InjectionManager

val injector = InjectionManager.apply {
    bindObject<ServerPort>(JettyServletAdapter()) // Bind Jetty server to HTTP Server Port
}

/**
 * Service server. Adapter is injected.
 */
val server: Server = Server {
    before {
        response.headers["Date"] = httpDate()
    }

    get("/hello/{name}") {
        ok("Hello, ${pathParameters["name"]}!", "text/plain")
    }
}

/**
 * Start the service from the command line.
 */
fun main() {
    logger.info { injector }
    server.start()
}
  1. Run the service and view the results at: http://localhost:2010/hello/world

Next Steps

To continue learning about this toolkit, you can:

Comments