forked from AdamBien/lightmetal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlmserve
More file actions
executable file
·64 lines (57 loc) · 2.18 KB
/
Copy pathlmserve
File metadata and controls
executable file
·64 lines (57 loc) · 2.18 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
#!/usr/bin/env -S java --enable-native-access=ALL-UNNAMED --source 25 -cp ./zbo/lightmetal.jar:./lightmetal.jar
import lm.catalog.boundary.ModelCatalog;
import lm.configuration.control.ZCfg;
import lm.http.boundary.HttpAPI;
import lm.version.control.Version;
String name = MethodHandles.lookup().lookupClass().getName();
String version = "2026-06-10.1";
void main(String... args) throws Exception {
IO.println(name + " " + version + " (lightmetal " + Version.VALUE + ")");
if (args.length > 0 && args[0].equals("-help")) {
IO.println("""
Usage: %s [model-fragment] [-port <n>]
-help Show this help
-version Show version
Reads model and port from ~/.lightmetal/app.properties when not given.
""".formatted(name));
return;
}
if (args.length > 0 && args[0].equals("-version")) {
return;
}
ZCfg.load("lightmetal");
String fragment = null;
var port = ZCfg.integer("port", 8080);
for (var i = 0; i < args.length; i++) {
if (args[i].equals("-port") && i + 1 < args.length) {
port = Integer.parseInt(args[++i]);
} else if (!args[i].startsWith("-")) {
fragment = args[i];
}
}
var fileName = fragment != null ? matchModel(fragment) : ZCfg.string("model");
if (fileName == null) {
System.err.println(name + ": no model configured (set model=<file> in ~/.lightmetal/app.properties)");
System.exit(1);
}
var modelPath = ModelCatalog.resolve(fileName);
if (!Files.exists(modelPath)) {
System.err.println(name + ": model not found at " + modelPath);
System.exit(1);
}
HttpAPI.serve(modelPath, port);
}
String matchModel(String fragment) {
var matches = ModelCatalog.search(fragment);
if (matches.size() == 1) {
return matches.getFirst();
}
if (matches.isEmpty()) {
System.err.println(name + ": no model matches \"" + fragment + "\"");
} else {
System.err.println(name + ": " + matches.size() + " models match \"" + fragment + "\":");
matches.forEach(m -> System.err.println(" " + m));
}
System.exit(1);
return null;
}