happyx icon indicating copy to clipboard operation
happyx copied to clipboard

JVM Bindings🔥

Open Ethosa opened this issue 2 years ago • 0 comments

Checklist

  • [x] HttpRequest;
  • [x] Server;
  • [x] PathParams, PathParam;
  • [x] Queries, Query;
  • [x] HttpHeaders, HttpHeader;
  • [x] HtmlResponse;
  • [x] JsonResponse;
  • [x] FileResponse;
  • [x] WebSockets;
  • [x] Static directories;
  • [x] Mounting other servers;
  • [ ] Request Models;
  • [ ] Publishing (Maven/Gradle ?);
  • [ ] Documentation;

Minimal Example (Kotlin)

val s = Server()

s.get("/") {
    // Just print path
    println(it.path)
    0
}

s.get("/user{userId:int}") {
    // Get path
    println(it.path)

    // Get any path param that you registered
    println(it.pathParams["userId"].int + 10)

    // Iterate over all queries
    println("Queries:")
    for (i in it.queries) {
        println(i)
    }

    // Iterate over all HttpHeaders
    println("HTTP Headers:")
    for (i in it.headers) {
        println(i)
    }

    // answer to request
    // now it sends just like string
    return@get "Hello, world!"
}

s.get("/base") {
    println(it.path)
    return@get BaseResponse(
        "Oops, bad request",
        401,
        listOf(HttpHeader("Programming-Language", "Kotlin"))
    )
}

s.get("/html") {
    println(it.path)
    return@get HtmlResponse(
        "<h1>Oops! Seems like page that you search is not found</h1>",
        404,
        listOf(HttpHeader("Programming-Language", "Kotlin"))
    )
}

s.get("/json") {
    println(it.path)
    return@get JsonResponse(
        Gson().toJson(listOf(1, 2, 3)),
        404,
        listOf(HttpHeader("Programming-Language", "Kotlin"))
    )
}

s.get("/file") {
    println(it.path)
    return@get FileResponse(Server::class.java.getResource("/happyx.dll")!!.file)
}

s.start()

Ethosa avatar Dec 06 '23 17:12 Ethosa