[mod] server
Christian Fraß

Christian Fraß commited on 2021-03-12 00:48:04
Zeige 5 geänderte Dateien mit 115 Einfügungen und 36 Löschungen.

... ...
@@ -32,42 +32,12 @@ async function main(args : Array<string>) : Promise<void>
32 32
 		}
33 33
 		case "run":
34 34
 		{
35
-			const server : lib_comm.interface_server<string, lib_comm.type_response_server_http> = new lib_comm.class_server_http
35
+			const server : lib_comm.class_server_common = new lib_comm.class_server_common
36 36
 			(
37
-				{
38
-					"port": 7777,
39
-				}
40
-			);
41
-			server.setup
42
-			(
43
-				async (input_raw) =>
44
-				{
45
-					const stuff : any = JSON.parse(input_raw);
46
-					try
47
-					{
48
-						const output : any = await api.query(stuff.action, stuff.input);
49
-						return Promise.resolve<lib_comm.type_response_server_http>
50
-						(
51
-							{
52
-								"code": 200,
53
-								"text": JSON.stringify(output),
54
-							}
55
-						);
56
-					}
57
-					catch (exception)
58
-					{
59
-						console.error(exception);
60
-						return Promise.resolve<lib_comm.type_response_server_http>
61
-						(
62
-							{
63
-								"code": 500,
64
-								"text": 'error',
65
-							}
66
-						);
67
-					}
68
-				}
37
+				7777,
38
+				ns_server.query_http_raw
69 39
 			);
70
-			server.run();
40
+			server.start();
71 41
 			break;
72 42
 		}
73 43
 		case "show":
... ...
@@ -151,7 +121,7 @@ async function main(args : Array<string>) : Promise<void>
151 121
 							{
152 122
 								"id": entry["id"].toFixed(),
153 123
 								"type": entry["type"],
154
-								"description": (entry["description"] ?? '-'),
124
+								"description": (entry["description"] || '-'),
155 125
 								"tags": entry["tags"].join(","),
156 126
 								"expressions": (
157 127
 									() =>
... ...
@@ -0,0 +1,101 @@
1
+namespace ns_server
2
+{
3
+	
4
+	/**
5
+	 */
6
+	async function query
7
+	(
8
+		data_in : any
9
+	) : Promise<any>
10
+	{
11
+		console.info("[>>] query |", data_in);
12
+		const data_out : any = await api.query(data_in["action"], data_in["input"]);
13
+		console.info("[<<] query |", data_out);
14
+		return Promise.resolve<any>(data_out);
15
+	}
16
+	
17
+	
18
+	/**
19
+	 */
20
+	async function query_http
21
+	(
22
+		http_request : lib_http.type_request
23
+	) : Promise<lib_http.type_response>
24
+	{
25
+		// console.info("[>>] query_http |", http_request);
26
+		let http_response : lib_http.type_response;
27
+		switch (http_request.method)
28
+		{
29
+			case lib_http.enum_method.options:
30
+			{
31
+				http_response = {
32
+					"statuscode": 200,
33
+					"headers": {
34
+						"Allow": "OPTIONS, POST",
35
+						"Access-Control-Allow-Origin": "*",
36
+						"Access-Control-Allow-Methods": "OPTIONS,POST",
37
+						"Access-Control-Allow-Headers": "Content-Type",
38
+					},
39
+					"body": "",
40
+				};
41
+				break;
42
+			}
43
+			case lib_http.enum_method.post:
44
+			{
45
+				const data_in : any = lib_code.json_decode(http_request.body);
46
+				try
47
+				{
48
+					const data_out : any = await query(data_in);
49
+					http_response = {
50
+						"statuscode": 200,
51
+						"headers": {
52
+							"Allow": "OPTIONS, POST",
53
+							"Access-Control-Allow-Origin": "*",
54
+							"Access-Control-Allow-Methods": "OPTIONS,POST",
55
+							"Access-Control-Allow-Headers": "Content-Type",
56
+							"Content-Type": "application/json",
57
+						},
58
+						"body": lib_code.json_encode(data_out),
59
+					};
60
+				}
61
+				catch (exception)
62
+				{
63
+					console.error(exception);
64
+					const data_out : any = {};
65
+					http_response = {
66
+						"statuscode": 500,
67
+						"headers": {
68
+							"Allow": "OPTIONS, POST",
69
+							"Access-Control-Allow-Origin": "*",
70
+							"Access-Control-Allow-Methods": "OPTIONS,POST",
71
+							"Access-Control-Allow-Headers": "Content-Type",
72
+							"Content-Type": "application/json",
73
+						},
74
+						"body": lib_code.json_encode(data_out),
75
+					};
76
+				}
77
+				break;
78
+			}
79
+		}
80
+		// console.info("[<<] query_http |", http_response);
81
+		return Promise.resolve<lib_http.type_response>(http_response);
82
+	}
83
+	
84
+	
85
+	/**
86
+	 */
87
+	export async function query_http_raw
88
+	(
89
+		input_raw : string
90
+	) : Promise<string>
91
+	{
92
+		// console.info("[>>] query_http_raw |", input_raw);
93
+		const http_request : lib_http.type_request = lib_http.decode_request(input_raw);
94
+		const http_response : lib_http.type_response = await query_http(http_request);
95
+		const output_raw : string = lib_http.encode_response(http_response);
96
+		// console.info("[<<] query_http_raw |", output_raw);
97
+		return Promise.resolve<string>(output_raw);
98
+	}
99
+	
100
+}
101
+
... ...
@@ -1,4 +1,5 @@
1 1
 #!/usr/bin/env bash
2 2
 
3 3
 rm -rf build
4
+rm -rf temp
4 5
 
... ...
@@ -8,7 +8,7 @@ dir_lib := lib
8 8
 cmd_concatenate := cat
9 9
 cmd_dir_make := mkdir --parents
10 10
 cmd_copy := cp --recursive --update --verbose
11
-cmd_tsc := tsc --lib es2015,dom --target es6
11
+cmd_tsc := tsc --lib es2017,dom --target es2020
12 12
 cmd_remove := rm --force
13 13
 cmd_link := ln --symbolic
14 14
 cmd_log := echo "--"
... ...
@@ -58,6 +58,7 @@ ${dir_temp}/manage-core.js: \
58 58
 	${dir_source}/api/master.ts \
59 59
 	${dir_source}/api/actions/concept_add.ts \
60 60
 	${dir_source}/api/actions/translate.ts \
61
+	${dir_source}/server.ts \
61 62
 	${dir_source}/main.ts
62 63
 	@ ${cmd_log} "compiling …"
63 64
 	@ ${cmd_dir_make} ${dir_temp}
... ...
@@ -0,0 +1,6 @@
1
+#!/usr/bin/env bash
2
+
3
+cd build
4
+node manage run
5
+cd ..
6
+
0 7