com.gitee.rabbitnoteeth.bedrock.http.server.IHttpServer Maven / Gradle / Ivy
The newest version!
package com.gitee.rabbitnoteeth.bedrock.http.server;
import com.gitee.rabbitnoteeth.bedrock.http.json.JsonResult;
import com.gitee.rabbitnoteeth.bedrock.http.server.constant.HttpConstants;
import io.vertx.ext.web.RoutingContext;
import javax.validation.ConstraintViolation;
import java.util.Set;
import java.util.stream.Collectors;
public interface IHttpServer {
int port();
default int instanceNum() {
return 1;
}
default boolean asyncMode() {
return false;
}
default String staticRoot() {
return null;
}
default String staticPath() {
return "/static/*";
}
default String apiPath() {
return "/api";
}
void onStart();
void onStartError(Throwable e);
void onError(RoutingContext context, Throwable e);
default void onRequest(RoutingContext context) {
context.next();
}
default void onParamValidFailed(RoutingContext context, Set> violationSet) {
String message = violationSet.stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(";"));
context.response().putHeader("Content-Type", "application/json;charset=UTF-8");
context.response().end(JsonResult.fail(message, HttpConstants.HTTP_RESPONSE_STATUS_400).encode());
}
default void onResponse(RoutingContext context, Class> returnType, Object result) {
if (returnType == void.class) {
context.response().end();
} else {
context.response().putHeader("Content-Type", "application/json;charset=UTF-8");
context.response().end((result instanceof JsonResult) ? ((JsonResult) result).encode() : JsonResult.success("SUCCESS", result).encode());
}
}
void onClose();
}