add FileServer, fix an issue in style
This commit is contained in:
parent
9959d33290
commit
8eca83854d
4 changed files with 83 additions and 2 deletions
|
@ -54,7 +54,12 @@ public class RequestCatcherConfig {
|
|||
})
|
||||
.compose((resp, res, rej) -> {
|
||||
try {
|
||||
if(resp.getConstructors()[0].getParameterCount() == 1) {
|
||||
res.call((IRequestCatcher) resp.getConstructors()[0].newInstance(this.configHolder));
|
||||
}
|
||||
else {
|
||||
res.call(resp.newInstance());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
rej.call(new ProjectException("Main class of RequestCatcher is not instantiable (" + getName().err(rej).ok().await() + ")", e));
|
||||
}
|
||||
|
|
73
src/de/tudbut/tryumph/example/FileServer.java
Normal file
73
src/de/tudbut/tryumph/example/FileServer.java
Normal file
|
@ -0,0 +1,73 @@
|
|||
package de.tudbut.tryumph.example;
|
||||
|
||||
import java.net.Socket;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import de.tudbut.async.Callback;
|
||||
import de.tudbut.async.ComposeCallback;
|
||||
import de.tudbut.async.TaskCallable;
|
||||
import de.tudbut.tryumph.config.IRequestCatcher;
|
||||
import de.tudbut.tryumph.events.GET;
|
||||
import de.tudbut.tryumph.events.POST;
|
||||
import de.tudbut.tryumph.events.Path;
|
||||
import de.tudbut.tryumph.events.RequestHandler;
|
||||
import de.tudbut.tryumph.server.HTMLParsing;
|
||||
import de.tudbut.tryumph.server.Request;
|
||||
import de.tudbut.tryumph.server.Response;
|
||||
import tudbut.parsing.TCN;
|
||||
|
||||
public class FileServer implements IRequestCatcher, RequestHandler.Listener {
|
||||
|
||||
RequestHandler listener = new RequestHandler(this);
|
||||
TCN data;
|
||||
|
||||
public FileServer(TCN data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TaskCallable<ComposeCallback<Request, Response>> onConnect(Socket socket) {
|
||||
return (tres, trej) -> tres.call((resp, res, rej) -> {
|
||||
listener.handle(resp, r -> {
|
||||
if(r.isHTML) {
|
||||
Document html = r.getHTML();
|
||||
Element element;
|
||||
element = html.createElement("meta");
|
||||
element.setAttribute("name", "viewport");
|
||||
element.setAttribute("content", "width=device-width height=device-height");
|
||||
html.getElementsByTagName("head").item(0).appendChild(element);
|
||||
element = html.createElement("link");
|
||||
element.setAttribute("rel", "stylesheet");
|
||||
element.setAttribute("href", "/style.css");
|
||||
html.getElementsByTagName("head").item(0).appendChild(element);
|
||||
r.updateHTMLData();
|
||||
}
|
||||
res.call(r);
|
||||
}, rej);
|
||||
if(!resp.hasResponse()) {
|
||||
res.call(new Response(resp, "<h1>Error: 404 Not found " + resp.realPath + "</h1>", 404, "Not Found"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/style.css")
|
||||
public void style(Request request, Callback<Response> res, Callback<Throwable> rej) {
|
||||
res.call(new Response(request, request.context.file("style.css"), 200, "OK", "text/css"));
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/.*")
|
||||
public void onIndex(Request request, Callback<Response> res, Callback<Throwable> rej) {
|
||||
res.call(new Response(request, request.context.file("." + request.path.replace("..", "")), 200, "OK"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleError(Request request, Throwable error, Callback<Response> res, Callback<Throwable> rej) {
|
||||
rej.call(error);
|
||||
}
|
||||
|
||||
}
|
|
@ -2,6 +2,7 @@ package de.tudbut.tryumph.server;
|
|||
|
||||
import static de.tudbut.async.Async.*;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
|
@ -127,6 +128,8 @@ public class BrowserContext {
|
|||
StringBuilder builder = new StringBuilder();
|
||||
try {
|
||||
InputStream stream = requestCatcher.getClass().getClassLoader().getResourceAsStream(file);
|
||||
if(stream == null)
|
||||
stream = new FileInputStream(file);
|
||||
|
||||
int i = 0;
|
||||
while((i = stream.read()) != -1) {
|
||||
|
|
|
@ -6,5 +6,5 @@ center {
|
|||
margin: auto;
|
||||
display: block;
|
||||
max-width: 700px;
|
||||
word-break: break-all;
|
||||
word-wrap: break-word;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue