initial working version

This commit is contained in:
Daniella / Tove 2022-07-03 11:43:26 +02:00
parent a5e8126976
commit 487a7b001c
5 changed files with 66 additions and 19 deletions

View file

@ -1,9 +1,15 @@
"builder.isbpl" include "builder.isbpl" include
func dependencies { func dependencies {
string! {
"https://github.com/TudbuT/tuddylib/raw/master/TuddyLIB.jar" download "https://github.com/TudbuT/tuddylib/raw/master/TuddyLIB.jar" download
"https://github.com/TudbuT/tuddylib/raw/master/TuddyLIB-javadoc.zip" download "https://github.com/TudbuT/tuddylib/raw/master/TuddyLIB-javadoc.zip" download
} #
"../tuddylib/TuddyLIB.jar" file
"../tuddylib/TuddyLIB-javadoc.zip" file
"https://github.com/TudbuT/isbpl-random-stuff/raw/master/ISBPL.jar" download "https://github.com/TudbuT/isbpl-random-stuff/raw/master/ISBPL.jar" download
"https://github.com/jtidy/jtidy/releases/download/1.0.2-SNAPSHOT/jtidy-1.0.2-SNAPSHOT.jar" download
"https://github.com/jtidy/jtidy/releases/download/1.0.2-SNAPSHOT/jtidy-1.0.2-SNAPSHOT-sources.jar" download
} }
"Tryumph" =name "Tryumph" =name

View file

@ -1,7 +1,5 @@
example { example {
main class: de.tudbut.tryumph.example.Main main class: de.tudbut.tryumph.example.Main
defaults {
port: 8080 port: 8080
} }
}

View file

@ -2,20 +2,37 @@ package de.tudbut.tryumph;
import java.util.Arrays; import java.util.Arrays;
import de.tudbut.tryumph.config.IRequestCatcher;
import de.tudbut.tryumph.config.RequestCatcherConfig;
import de.tudbut.tryumph.config.TryConfig; import de.tudbut.tryumph.config.TryConfig;
import de.tudbut.tryumph.err.ProjectException; import de.tudbut.tryumph.err.ProjectException;
import de.tudbut.tryumph.server.http.Server;
import de.tudbut.tryumph.util.Bug;
public class Launch { public class Launch {
private static TryConfig config;
private static RequestCatcherConfig[] catchers;
public static void main(String[] args) throws ProjectException, InterruptedException { public static void main(String[] args) throws ProjectException, InterruptedException {
try { try {
TryConfig config = new TryConfig(args, Launch.class.getClassLoader().getResourceAsStream("config.try")); config = new TryConfig(args, Launch.class.getClassLoader().getResourceAsStream("config.try"));
config.getCatchers().then(resp -> { catchers = config.getCatchers().ok().await();
System.out.println(Arrays.toString(resp));
}).ok();
} catch(Exception e) { } catch(Exception e) {
throw new ProjectException("Error loading project", e); throw new ProjectException("Error loading project", e);
} }
System.out.println(Arrays.toString(catchers));
for(int i = 0; i < catchers.length; i++) {
try {
RequestCatcherConfig catcher = catchers[i];
Server server = new Server(catcher.getPort().ok().await());
IRequestCatcher requestCatcher = catcher.load().ok().await();
server.listen(requestCatcher);
} catch(Throwable e) {
throw new Bug("HTTP server died, but all errors from HTTP should usually be catched", e);
}
}
Thread.sleep(1000); Thread.sleep(1000);
System.exit(0); System.exit(0);
} }

View file

@ -5,11 +5,12 @@ import tudbut.parsing.JSON;
import de.tudbut.async.*; import de.tudbut.async.*;
import static de.tudbut.async.Async.*; import static de.tudbut.async.Async.*;
import de.tudbut.tryumph.err.ProjectException;
import de.tudbut.tryumph.util.Compose; import de.tudbut.tryumph.util.Compose;
public class RequestCatcherConfig { public class RequestCatcherConfig {
private TCN configHolder; public TCN configHolder;
private String name; private String name;
private RequestCatcherConfig() {} private RequestCatcherConfig() {}
@ -23,20 +24,10 @@ public class RequestCatcherConfig {
RequestCatcherConfig r = new RequestCatcherConfig(); RequestCatcherConfig r = new RequestCatcherConfig();
r.name = name; r.name = name;
r.configHolder = resp; r.configHolder = resp;
r.build().err(rej).ok().await();
res.call(r); res.call(r);
}); });
} }
private Task<RequestCatcherConfig> build() {
return t((res, rej) -> {
if(this.name == null || this.configHolder == null) {
rej.call(new IllegalStateException("RequestCatcherConfig is not correctly initialized but was used"));
}
res.call(this);
});
}
public Task<String> getName() { public Task<String> getName() {
return t((res, rej) -> { return t((res, rej) -> {
if(name == null) if(name == null)
@ -45,7 +36,36 @@ public class RequestCatcherConfig {
}); });
} }
public Task<IRequestCatcher> load() {
return Async
.<Class<?>>t((res, rej) -> {
try {
res.call(Class.forName(configHolder.getString("main class")));
} catch (ClassNotFoundException e) {
rej.call(new ProjectException("Main class of RequestCatcher does not exist (" + getName().err(rej).ok().await() + ")", e));
}
})
.<Class<? extends IRequestCatcher>>compose((resp, res, rej) -> {
try {
res.call(resp.asSubclass(IRequestCatcher.class));
} catch (ClassCastException e) {
rej.call(new ProjectException("Main class of RequestCatcher does not implement RequestCatcher (" + getName().err(rej).ok().await() + ")", e));
}
})
.compose((resp, res, rej) -> {
try {
res.call(resp.newInstance());
} catch (Exception e) {
rej.call(new ProjectException("Main class of RequestCatcher is not instantiable (" + getName().err(rej).ok().await() + ")", e));
}
});
}
public String toString() { public String toString() {
return "RequestCatcherConfig{name=" + name + ",configHolder=" + JSON.write(configHolder) + "}"; return "RequestCatcherConfig{name=" + name + ",configHolder=" + JSON.write(configHolder) + "}";
} }
public Task<Integer> getPort() {
return t((res, rej) -> res.call(configHolder.getInteger("port")));
}
} }

View file

@ -45,5 +45,11 @@ public class Compose {
} }
}; };
} }
public static ComposeCallback<String, String> cleanString() {
return (resp, res, rej) -> {
res.call(resp.replace("\\", "\\\\").replace("\"", "\\\"").replace("\'", "\\\'"));
};
}
} }