-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparkServer.java
More file actions
94 lines (70 loc) · 2.69 KB
/
SparkServer.java
File metadata and controls
94 lines (70 loc) · 2.69 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.io.BufferedReader; // for inputsteam to string
import java.nio.charset.StandardCharsets;
import static spark.Spark.*;
import org.json.*;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
public class SparkServer {
public static void main(String[] args){
port(8000);
staticFiles.location("/Web Stuff");
get("/hello", (req, res) -> "Hello World");
post("/ocr", (req, res) -> {
String response;
Files.write(Paths.get("temp.png"), req.bodyAsBytes());
Tesseract tesseract = new Tesseract();
try {
response = tesseract.doOCR(new File("temp.png"));
} catch(TesseractException e) {
response = e.toString();
}
res.header("Access-Control-Allow-Origin", "*");
return response;
});
post("/format", (req, res) -> {
Format formatter = new Format();
res.header("Access-Control-Allow-Origin", "*");
return formatter.formatText(req.body());
});
post("/dbinput", (req, res) -> {
// extract from req
String user = req.queryParams("email");
String input = req.queryParams("code");
Database database = new Database();
database.getDatabase();
database.addEntry(user, input);
database.saveDatabase();
res.header("Access-Control-Allow-Origin", "*");
return "Successfully saved file!";
});
post("/dboutput", (req, res) -> {
String user = req.queryParams("email");
Database database = new Database();
database.getDatabase();
JSONArray ja = new JSONArray(database.getAllFiles(user));
res.header("Access-Control-Allow-Origin", "*");
return ja.toString();
});
post("/compiler", (req, res) -> {
String input = req.queryParams("code");
Compiler compiler = new Compiler();
String fileName = compiler.FindFileName(input);
Exporter.exportAs(input, fileName);
compiler.compile(fileName);
String response = compiler.run(fileName);
compiler.deleteExecutable(fileName);
res.header("Access-Control-Allow-Origin", "*");
return response;
//return input;
});
}
}