-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea/ | ||
micro-server.iml | ||
out/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import scala.util.matching.Regex | ||
|
||
/** | ||
* @author René Vennik | ||
* @version 1.0 | ||
* @since 22-12-2016 | ||
*/ | ||
object Main { | ||
|
||
val contact: Regex = """\/contact\/?(.*)?""".r | ||
|
||
def main(args: Array[String]): Unit = { | ||
new Server({ | ||
case contact(Int(id)) => "Contact int " + id | ||
case contact(String(name)) => "Contact string " + name | ||
case contact(_*) => "Contact nothing" | ||
case "/" => "Home" | ||
}, 80) | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import java.net.InetSocketAddress | ||
|
||
import com.sun.net.httpserver.{HttpExchange, HttpHandler, HttpServer} | ||
|
||
import scala.util.Try | ||
|
||
class Server(routes: String => Any, val port: Int = 80) { | ||
|
||
val server: HttpServer = HttpServer.create(new InetSocketAddress(port), 0) | ||
val handler = new Handler(routes) | ||
server.createContext("/", handler) | ||
server.setExecutor(null) | ||
server.start() | ||
|
||
} | ||
|
||
class Handler(val routes: String => Any) extends HttpHandler { | ||
|
||
def handle(t: HttpExchange): Unit = { | ||
Try(routes(t.getRequestURI.getPath).toString).toOption match { | ||
case Some(response) => handleResponse(t, 200, response) | ||
case _ => handleResponse(t, 404, "404 - Not Found") | ||
} | ||
|
||
} | ||
|
||
def handleResponse(t: HttpExchange, rCode: Int, response: String): Unit = { | ||
t.sendResponseHeaders(rCode, response.length()) | ||
val os = t.getResponseBody | ||
os.write(response.getBytes) | ||
os.close() | ||
} | ||
|
||
} | ||
|
||
object Int { | ||
def unapply(i: String): Option[Int] = Try(i.toInt).toOption | ||
} | ||
|
||
object String { | ||
def unapply(s: String): Option[String] = Option(s) | ||
} |