GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - BergerAPI/kbackend: A simple backend library for creating backends in Kotlin/Java

BranchesTags
Open more actions menu

Latest commit

 

History

15 Commits
15 Commits

Folders and files

NameName
Last commit message
Last commit date
src
src
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
pom.xml
pom.xml
 
 

Repository files navigation

kbackend

A simple backend library for creating backends in Kotlin/Java

Maven Setup

Add this as dependency:

<dependency>
    <groupId>de.berger</groupId>
    <artifactId>kbackend</artifactId>
    <version>1.0.2</version>
</dependency>

Usage

Lazy Routes

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

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.

class Controller : 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.
    fun myEndpoint(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?) 
    }
    
}

Middleware

Middlewares are always executed before the route is executed and decide if the route is executed or not.

class MyMiddleware : Middleware {
    override fun preRequest(request: Request): MiddlewareResponse {
        return MiddlewareResponse(true, "Failed.", HttpResponseStatus.OK)
    }
}

Example

data class Test(val name: String, val age: Int)

@Path("/animals")
@SuppressWarnings("unused")
class TestListener : Controller() {

    @GET("/dog")
    fun dog(request: Request, @Query("age") age: Int, @Query("name") name: String): Response = json(
        Test(name, age), headers = mapOf(
            Cookie.createCookie("test", "test")
        )
    )

    @GET("/cat")
    fun cat(request: Request): Response = redirect("https://google.com/")

    @POST("/parrot")
    fun parrot(request: Request): Response = json(Test(request.body, 1337))

    @GET("/parrot")
    fun parrotGet(request: Request): Response = json(Test(request.body, 17))

}

// Bootstrap
fun main() {
    val backend = BackendManager()

    backend.routing.lazyRoute("/") {
        plain("Hello World!")
    }

    backend.routing.initializeController(TestListener())

    backend.run(3000)
}

About

A simple backend library for creating backends in Kotlin/Java

Resources

Readme
Apache-2.0 license
Activity

Stars

3 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.