A simple backend library for creating backends in Kotlin/Java
Add this as dependency:
<dependency>
<groupId>de.berger</groupId>
<artifactId>kbackend</artifactId>
<version>1.0.2</version>
</dependency>A lazy route is basically a route, which is required to only listen to GET request and makes it easier to debug things.
backend.routing.lazyRoute("/") {
return plain("Hello World!")
}Controller exists, so you can easily create multiple routes as functions and use annotations for information passed by the user like the queries, or the body.
classController : Controller() {
@GET("/path") // There are also @POST, @PUT, @DELETE, @PATCH, @HEAD, @OPTIONS
@Protect("de.berger.MiddleWareTest") // This applies a middleware, which is executed before the route is executed and decides if the route is executed or not.funmyEndpoint(request:Request, @Body(MyBodyObject::class) myBody:MyBodyObject, // This is the body of the request, which is parsed by the body parser. It accepts your custom class.
@Query("intQuery") testQuery:Int, // The type of the query is decided by the type of the parameter, so this has the type int
@Query("stringQuery") testQuery:String// and this has the type string
): Response {
return plain("Hello World!") // We also have json(myObject, status?) and redirect(route, status?)
}
}Middlewares are always executed before the route is executed and decide if the route is executed or not.
classMyMiddleware : Middleware {
overridefunpreRequest(request:Request): MiddlewareResponse {
returnMiddlewareResponse(true, "Failed.", HttpResponseStatus.OK)
}
}data classTest(valname:String, valage:Int)
@Path("/animals")
@SuppressWarnings("unused")
classTestListener : Controller() {
@GET("/dog")
fundog(request:Request, @Query("age") age:Int, @Query("name") name:String): Response= json(
Test(name, age), headers =mapOf(
Cookie.createCookie("test", "test")
)
)
@GET("/cat")
funcat(request:Request): Response= redirect("https://google.com/")
@POST("/parrot")
funparrot(request:Request): Response= json(Test(request.body, 1337))
@GET("/parrot")
funparrotGet(request:Request): Response= json(Test(request.body, 17))
}
// Bootstrapfunmain() {
val backend =BackendManager()
backend.routing.lazyRoute("/") {
plain("Hello World!")
}
backend.routing.initializeController(TestListener())
backend.run(3000)
}