-
Notifications
You must be signed in to change notification settings - Fork 0
ZooMMaX edited this page Jan 11, 2024
·
2 revisions
There are two ways to start the library:
- With annotations
- Without annotations
import ru.zoommax.SimpleServer;
import ru.zoommax.next.Request;
import ru.zoommax.next.Response;
import ru.zoommax.next.annotation.Endpoint;
import ru.zoommax.next.annotation.InitWebServer;
import ru.zoommax.next.enums.HttpMethod;
public class AnnotatedStart {
/*
* We annotate the method that will start the server using the InitWebServer annotation
* In the InitWebServer annotation, we specify the port and the number of threads
* By default, the port is 8080, and the number of threads is 1
* Inside the method, we start the server using SimpleServer.start()
* SimpleServer.start() starts reading annotations in all classes inside the package
* with the subsequent launch of the server (a singleton is created) and the addition of endpoints
* to the server's endpoint list.
*/
@InitWebServer(port = 12345, threads = 4)
public static void main(String[] args) {
SimpleServer.start();
}
/*
* We annotate the method that implements the endpoint logic using the Endpoint annotation
* In the Endpoint annotation, we specify the endpoint path, HTTP method, response code, and content length filter
* By default, the method is GET, the response code is 200, and the content length filter is -1 (do not filter).
* Inside the method, we implement the endpoint logic.
* The method should take a Request object and return a Response object.
* The Request object contains all the information about the request,
* and the Response object forms the response to the request.
*/
@Endpoint(path = "/test", httpMethod = HttpMethod.GET, statusCode = 200, filterContentLength = -1)
public Response test(Request request) {
//Endpoint logic
String body = request.getBodyAsString();
//...
//Return response
return Response.builder()
.bodyAsString(body)
.statusCode(200)
.build();
}
}- The method that starts the server should be annotated with
@InitWebServer. -
@InitWebServertakes two parameters:portandthreads. By default,portis 8080, andthreadsis 1. - The method that implements the endpoint logic should be annotated with
@Endpoint. -
@Endpointtakes four parameters:path,httpMethod,statusCode, andfilterContentLength. By default,httpMethodisHttpMethod.GET,statusCodeis 200, andfilterContentLengthis -1.
import ru.zoommax.SimpleServer;
import ru.zoommax.next.Request;
import ru.zoommax.next.Response;
import ru.zoommax.next.annotation.Endpoint;
import ru.zoommax.next.annotation.InitWebServer;
import ru.zoommax.next.enums.HttpMethod;
@InitWebServer(port = 12345, threads = 4)
public class AnnotatedStartWithExtends extends SimpleServer {
public static void main(String[] args) {
}
@Endpoint(path = "/test", httpMethod = HttpMethod.GET, statusCode = 200, filterContentLength = -1)
public Response test(Request request) {
return Response.builder()
.bodyAsString(request.getBodyAsString())
.statusCode(200)
.build();
}
}import ru.zoommax.SimpleServer;
import ru.zoommax.next.Response;
import ru.zoommax.next.handlers.GetHandlerNew;
import java.util.HashMap;
public class NotAnnotatedStart {
/*
* We start the server using SimpleServer.start()
* SimpleServer.start() starts the server (a singleton is created).
* Then you can add endpoints to the server's endpoint list.
* In SimpleServer.start() you need to specify the port and the number of threads.
*/
public static void main(String[] args) {
SimpleServer.start(12345, 4);
test();
}
/*
* We add an endpoint using SimpleServer.addEndpoint()
* SimpleServer.addEndpoint() takes two parameters: the endpoint path and the handler.
* Inside the handler, we implement the endpoint logic.
* We return a Response object.
*/
public static void test() {
SimpleServer.addEndpoint("/test", new GetHandlerNew() {
@Override
public Response response(String request, HashMap<String, String> requestHeaders, HashMap<String, String> requestParams, String clientIp) {
//Endpoint logic
String body = request;
//...
return Response.builder()
.bodyAsString(body)
.statusCode(200)
.build();
}
});
}
}If you want to help in development, you can fork the repository and create a pull request. If you want to help in development but don't know where to start, you can take one of the tasks from the list below:
- Add support for HTTP methods:
- PUT
- DELETE
- HEAD
- OPTIONS
- TRACE
- CONNECT
- PATCH
- Add what you think is necessary for the library.