-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
66 lines (55 loc) · 2.29 KB
/
Main.java
File metadata and controls
66 lines (55 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package org.web1;
import com.fastcgi.*;
import java.util.HashMap;
import java.util.function.Function;
import org.validator.ValidatedRecordFactory;
import org.validator.validation.exceptions.ValidationException;
import org.web1.DTOs.RequestDTO;
import org.web1.checkers.Checker;
import org.web1.checkers.CheckerFunction;
import org.web1.utils.mappers.JsonBuilder;
import org.web1.utils.mappers.QueryStringToHashmap;
import org.web1.utils.responce.ResponseFactory;
import org.web1.utils.responce.ResponseStatus;
import org.web1.utils.timer.Timer;
public class Main {
private static final Function<String, HashMap<String,String>> parseQuery = new QueryStringToHashmap();
private static final CheckerFunction checker = new Checker();
public static void main(String[] args) {
FCGIInterface fastCGI = new FCGIInterface();
Timer timer = new Timer();
while (fastCGI.FCGIaccept() >= 0) {
timer.start();
final HashMap<String, String> queryParams = parseQuery.apply(
FCGIInterface.request.params.getProperty("QUERY_STRING")
);
try {
RequestDTO requestData = ValidatedRecordFactory.create(
RequestDTO.class,
queryParams.get("x"),
queryParams.get("r"),
queryParams.get("y")
);
boolean checkResult = checker.test(
Integer.parseInt(requestData.X()),
Float.parseFloat(requestData.Y()),
Float.parseFloat(requestData.R())
);
String result = ResponseFactory.create(
new JsonBuilder()
.add("result", checkResult)
.add("elapsedTimeNs", timer.stop()),
ResponseStatus.OK
);
System.out.println(result);
} catch (ValidationException e) {
String result = ResponseFactory.create(
new JsonBuilder()
.add("error", '"'+e.getMessage()+'"'),
ResponseStatus.BAD_REQUEST
);
System.out.println(result);
}
}
}
}