-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* init * update * rm some log
- Loading branch information
Showing
15 changed files
with
518 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,4 @@ build/ | |
|
||
### VS Code ### | ||
.vscode/ | ||
dependency-reduced-pom.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package w; | ||
|
||
import fi.iki.elonen.NanoHTTPD; | ||
import w.core.InMemoryJavaCompiler; | ||
import w.util.PrintUtils; | ||
|
||
import java.io.*; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static fi.iki.elonen.NanoHTTPD.Response.Status.BAD_REQUEST; | ||
|
||
public class Compiler { | ||
public static final int port = 13019; | ||
|
||
public static void main(String[] args) throws IOException { | ||
try { | ||
Class.forName("com.sun.tools.javac.processing.JavacProcessingEnvironment"); | ||
} catch (ClassNotFoundException e) { | ||
System.err.println("Please use jdk instead of jre to start a compiler server"); | ||
System.exit(-1); | ||
} | ||
NanoHTTPD nanoHTTPD = new NanoHTTPD(port) { | ||
@Override | ||
public Response serve(String uri, Method method, | ||
Map<String, String> header, Map<String, String> parameters, | ||
Map<String, String> files) { | ||
if (method == Method.POST) { | ||
switch (uri) { | ||
case "/compile": | ||
String className = parameters.get("className"); | ||
|
||
String content = parameters.get("content"); | ||
content = new String(Base64.getDecoder().decode(parameters.get("content")), StandardCharsets.UTF_8); | ||
try { | ||
StringBuilder errorMessage = new StringBuilder(); | ||
byte[] bytecode = InMemoryJavaCompiler.compile(className, content, new InMemoryJavaCompiler.InMemoryDiagnosticListener(errorMessage)); | ||
Map<String, Object> _m = new HashMap<>(); | ||
if (bytecode != null) { | ||
_m.put("code", 0); | ||
_m.put("data", Base64.getEncoder().encodeToString(bytecode) ); | ||
String json = PrintUtils.getObjectMapper().writeValueAsString(_m); | ||
return newFixedLengthResponse(json); | ||
} else { | ||
_m.put("code", 1); | ||
_m.put("data", errorMessage.toString()); | ||
String json = PrintUtils.getObjectMapper().writeValueAsString(_m); | ||
return newFixedLengthResponse(json); | ||
} | ||
} catch (Exception e) { | ||
return newFixedLengthResponse(BAD_REQUEST, "", "NOT SUPPORT"); | ||
} | ||
} | ||
} | ||
|
||
return newFixedLengthResponse(BAD_REQUEST, "", "NOT SUPPORT"); | ||
} | ||
}; | ||
nanoHTTPD.start(5000, false); | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.