This project is a lightweight HTTP server framework implemented in Kotlin, providing developers with an easy-to-use platform to create RESTful APIs. It simplifies the process of setting up HTTP servers and defining routes for handling various HTTP methods and request types.
- Simple Routing: Define routes effortlessly using a fluent API. || DONE
- Status Code Handling: Automatically handle common HTTP status codes. || PENDING
- Content Type Validation: Validate incoming request content types. || PENDING
- Error Handling: Handle method not allowed, content type not supported, and route not found errors gracefully. || WORKING
- Customizable: Easily customize port number and server configurations. || DONE
-
Clone this repository.
git clone https://github.com/pankaj046/RestApiServer.git
-
Import the project into your preferred Kotlin IDE.
-
Create a new HTTP server instance using the
apiServer
function, specifying the port number.val server = apiServer(port = 8080) { // Define server routes here }
-
Define routes using the
route
function within the server instance.server.route("/example") { // Define route handling logic here }
-
Implement route handling logic within the provided handler function, specifying the HTTP method(s) and desired behavior.
route(path = "login" , request = LoginRequest::class.java, methods = "POST") { exchange, req-> // Handle GET requests for the "/example" route }
-
Start the server by calling the
start
method.server.start()
Here's a simple example demonstrating how to create a basic HTTP server using this framework:
apiServer(8080) {
route("/api") {
route(path = "login" , request = LoginRequest::class.java, methods = "POST") { exchange, req->
exchange.response(200, req)
}
route(path = "2", request = HashMap(), methods = "POST") { exchange, req->
val response = "${req?.get("Email")}"
exchange.response(200, response)
}
}
}.start()
- Kotlin
- JDK (Java Development Kit)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- Pankaj
For any inquiries or support, please contact pankaj\dev.pankaj046@gmail.com.