Skip to content

pankaj046/RestApiServer

Repository files navigation

Kotlin Coroutine-Based HTTP Server (Fun Project)

This project is a lightweight HTTP server library built using Kotlin Coroutines. It allows you to define RESTful routes easily and handle JSON-based HTTP requests/responses in a clean, coroutine-first style.

🚀 Features

  • Lightweight and fast server
  • Clean routing DSL
  • JSON request/response handling
  • Coroutine-based request processing
  • Dynamic routes and path parameters
  • Easy to extend and customize

🧰 Tech Stack

  • Kotlin (JVM)
  • Coroutines
  • kotlinx.serialization (for JSON handling)
  • Java’s built-in HttpServer

🚀 Getting Started

Follow these steps to get the server up and running:

1. Clone the Repository

git clone https://github.com/pankaj046/RestApiServer.git
cd RestApiServer

2. Build the Project

./gradlew build

3. Run the Server

./gradlew run

📌 Example API Endpoints

✅ Health Check

URL: /api

Method: GET

Response:

{
  "status": "healthy",
  "timestamp": 1715018245000
}

🔐 Login

URL: /api

Method: POST

Request Body:

{
  "email": "[email protected]",
  "password": "password123"
}

Response:

{
  "success": true,
  "message": "Login successful",
  "user": {
    "email": "[email protected]"
  }
}

🔄 Dynamic JSON

URL: /api/dynamic

Method: POST

Request Body: Any JSON object

Response:

{
  "received": { "key": "value" },
  "message": "Data received successfully"
}

👤 User Routes

GET User by ID

URL: /api/users/{id}

Method: GET

PUT Update User

URL: /api/users/{id}

Method: PUT

Request Body:

{
  "name": "John Doe",
  "email": "[email protected]"
}

DELETE User

URL: /api/users/{id}

Method: DELETE

🧩 Extending the Server

You can define your own routes using the route, get, post, put, and delete functions:

route("/myroute") {
    get { ctx ->
        ctx.respond(200, mapOf("message" to "Hello from /myroute"))
    }
}

🧪 Test with curl

curl -X POST http://localhost:8080/api -H "Content-Type: application/json" \
  -d '{"email": "[email protected]", "password": "123456"}'

👨‍💻 Author

Developed with ❤️ by Pankaj

📄 License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages