-
Notifications
You must be signed in to change notification settings - Fork 14
/
sim.w
83 lines (70 loc) · 2.09 KB
/
sim.w
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
bring cloud;
bring sim;
bring ui;
bring util;
bring "./types.w" as types;
inflight interface StartResponse {
inflight port(): num;
inflight close(): void;
inflight specFile(): str;
}
/**
* Starts a new TSOA service.
*/
pub class Service_sim impl types.IService {
pub url: str;
pub specFile: str;
state: sim.State;
bucket: cloud.Bucket;
service: cloud.Service;
clients: MutMap<std.Resource>;
new(props: types.ServiceProps) {
let target = util.env("WING_TARGET");
if target != "sim" {
throw "Unsupported target {target}";
}
this.state = new sim.State();
this.bucket = new cloud.Bucket();
this.url = "http://127.0.0.1:{this.state.token("port")}";
this.specFile = this.state.token("specFile");
let currentdir = Service_sim.dirname();
let entrypointDir = nodeof(this).app.entrypointDir;
let workDir = nodeof(this).app.workdir;
let homeEnv = util.tryEnv("HOME") ?? "";
let pathEnv = util.tryEnv("PATH") ?? "";
this.clients = MutMap<std.Resource>{};
this.service = new cloud.Service(inflight () => {
let lastPort = this.bucket.tryGet("lastPort.txt");
let res = Service_sim.startService(
currentdir: currentdir,
basedir: entrypointDir,
workdir: workDir,
options: props,
homeEnv: homeEnv,
pathEnv: pathEnv,
clients: this.clients,
lastPort: lastPort,
);
this.state.set("port", "{res.port()}");
this.state.set("specFile", "{res.specFile()}");
this.bucket.put("lastPort.txt", "{res.port()}");
return inflight () => {
res.close();
};
});
this.addUi();
}
addUi() {
nodeof(this.state).hidden = true;
nodeof(this.bucket).hidden = true;
new ui.Field("Url", inflight () => {
return this.url;
}, link: true);
}
pub lift(client: std.Resource, ops: types.LiftOptions) {
client.onLift(this.service, ops.allow);
this.clients.set(ops.id, client);
}
extern "./lib.js" inflight static startService(options: types.StartServiceOptions): StartResponse;
extern "./lib.js" static dirname(): str;
}