Created
November 20, 2017 18:58
-
-
Save joewalnes/4bf3ac8abc143225fe2c75592d314840 to your computer and use it in GitHub Desktop.
Minimal embedded HTTP server in Kotlin using Java built in HttpServer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.sun.net.httpserver.HttpServer | |
import java.io.PrintWriter | |
import java.net.InetSocketAddress | |
/** | |
* Minimal embedded HTTP server in Kotlin using Java built in HttpServer | |
*/ | |
fun main(args: Array<String>) { | |
HttpServer.create(InetSocketAddress(8080), 0).apply { | |
createContext("/hello") { http -> | |
http.responseHeaders.add("Content-type", "text/plain") | |
http.sendResponseHeaders(200, 0) | |
PrintWriter(http.responseBody).use { out -> | |
out.println("Hello ${http.remoteAddress.hostName}!") | |
} | |
} | |
start() | |
} | |
} |
It is not recommended to use the package com.sun.* if you plan to move to other JDK like OpenJDK.
It's a proprietary package owned by Oracle/Sun.
Read more at https://www.oracle.com/java/technologies/faq-sun-packages.html
It is not recommended to use the package com.sun.* if you plan to move to other JDK like OpenJDK. It's a proprietary package owned by Oracle/Sun. Read more at https://www.oracle.com/java/technologies/faq-sun-packages.html
Don't be confused. "com.sun" and "sun" are 2 different packages. Some of the "com.sun" API are internal, but HttpServer is not. It's a part of standard public APIs.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🤣🤣🤣🤣🤣🤣🤣🤣