-
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.
- Introduces support for inline URI parameters - Rewritten path processing - Fixes websocket channels broadcast and close
- Loading branch information
Showing
15 changed files
with
717 additions
and
166 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
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
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 |
---|---|---|
@@ -1,15 +1,19 @@ | ||
import com.google.gson.JsonObject | ||
import io.netty.handler.codec.http.FullHttpResponse | ||
import net.ccbluex.netty.http.HttpServer | ||
import net.ccbluex.netty.http.model.RequestObject | ||
import net.ccbluex.netty.http.util.httpOk | ||
|
||
fun main() { | ||
val server = HttpServer() | ||
|
||
server.routeController.apply { | ||
post("/echo") { request -> | ||
httpOk(request.asJson<JsonObject>()) | ||
} | ||
post("/echo", ::postEcho) // /echo | ||
} | ||
|
||
server.start(8080) // Start the server on port 8080 | ||
} | ||
|
||
fun postEcho(requestObject: RequestObject): FullHttpResponse { | ||
return httpOk(requestObject.asJson<JsonObject>()) | ||
} |
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 @@ | ||
hello! |
File renamed without changes.
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 |
---|---|---|
@@ -1,15 +1,78 @@ | ||
import com.google.gson.JsonObject | ||
import io.netty.handler.codec.http.FullHttpResponse | ||
import net.ccbluex.netty.http.HttpServer | ||
import net.ccbluex.netty.http.model.RequestObject | ||
import net.ccbluex.netty.http.util.httpBadRequest | ||
import net.ccbluex.netty.http.util.httpOk | ||
import java.io.File | ||
|
||
const val FOLDER_NAME = "files" | ||
val folder = File(FOLDER_NAME) | ||
|
||
fun main() { | ||
val server = HttpServer() | ||
val folder = File("files") | ||
|
||
println("Serving files from: ${folder.absolutePath}") | ||
|
||
server.routeController.apply { | ||
// Serve files from the "files" directory | ||
file("/files", folder) | ||
get("/", ::getRoot) | ||
get("/conflicting", ::getConflictingPath) | ||
get("/a/b/c", ::getConflictingPath) | ||
get("/file/:name", ::getFileInformation) | ||
post("/file/:name", ::postFile) | ||
|
||
// register file serving at the bottom of the routing tree | ||
// to avoid overwriting other routes | ||
file("/", folder) | ||
} | ||
|
||
server.start(8080) // Start the server on port 8080 | ||
} | ||
|
||
@Suppress("UNUSED_PARAMETER") | ||
fun getRoot(requestObject: RequestObject): FullHttpResponse { | ||
// Count the number of files in the folder | ||
// Walk the folder and count the number of files | ||
return httpOk(JsonObject().apply { | ||
addProperty("path", folder.absolutePath) | ||
addProperty("files", folder.walk().count()) | ||
}) | ||
} | ||
|
||
@Suppress("UNUSED_PARAMETER") | ||
fun getConflictingPath(requestObject: RequestObject): FullHttpResponse { | ||
return httpOk(JsonObject().apply { | ||
addProperty("message", "This is a conflicting path") | ||
}) | ||
} | ||
|
||
@Suppress("UNUSED_PARAMETER") | ||
fun getFileInformation(requestObject: RequestObject): FullHttpResponse { | ||
val name = requestObject.params["name"] ?: return httpBadRequest("Missing name parameter") | ||
val file = File(folder, name) | ||
|
||
if (!file.exists()) { | ||
return httpBadRequest("File not found") | ||
} | ||
|
||
return httpOk(JsonObject().apply { | ||
addProperty("name", file.name) | ||
addProperty("size", file.length()) | ||
addProperty("lastModified", file.lastModified()) | ||
}) | ||
} | ||
|
||
@Suppress("UNUSED_PARAMETER") | ||
fun postFile(requestObject: RequestObject): FullHttpResponse { | ||
val name = requestObject.params["name"] ?: return httpBadRequest("Missing name parameter") | ||
val file = File(folder, name) | ||
|
||
if (file.exists()) { | ||
return httpBadRequest("File already exists") | ||
} | ||
|
||
file.writeText(requestObject.body) | ||
return httpOk(JsonObject().apply { | ||
addProperty("message", "File written") | ||
}) | ||
} |
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 |
---|---|---|
@@ -1,17 +1,41 @@ | ||
import com.google.gson.JsonObject | ||
import io.netty.handler.codec.http.FullHttpResponse | ||
import net.ccbluex.netty.http.HttpServer | ||
import net.ccbluex.netty.http.model.RequestObject | ||
import net.ccbluex.netty.http.util.httpOk | ||
|
||
fun main() { | ||
val server = HttpServer() | ||
|
||
server.routeController.apply { | ||
get("/hello") { | ||
httpOk(JsonObject().apply { | ||
addProperty("message", "Hello, World!") | ||
}) | ||
} | ||
get("/", ::getRoot) | ||
get("/hello", ::getHello) // /hello?name=World | ||
get("/hello/:name", ::getHello) // /hello/World | ||
get("/hello/:name/:age", ::getHelloWithAge) // /hello/World/20 | ||
} | ||
|
||
server.start(8080) // Start the server on port 8080 | ||
} | ||
|
||
@Suppress("UNUSED_PARAMETER") | ||
fun getRoot(requestObject: RequestObject): FullHttpResponse { | ||
return httpOk(JsonObject().apply { | ||
addProperty("root", true) | ||
}) | ||
} | ||
|
||
fun getHello(requestObject: RequestObject): FullHttpResponse { | ||
val name = requestObject.params["name"] ?: requestObject.queryParams["name"] ?: "World" | ||
return httpOk(JsonObject().apply { | ||
addProperty("message", "Hello, $name!") | ||
}) | ||
} | ||
|
||
fun getHelloWithAge(requestObject: RequestObject): FullHttpResponse { | ||
val name = requestObject.params["name"] ?: "World" | ||
val age = requestObject.params["age"] ?: "0" | ||
return httpOk(JsonObject().apply { | ||
addProperty("message", "Hello, $name! You are $age years old.") | ||
}) | ||
} | ||
|
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
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
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
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
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
Oops, something went wrong.