Pharos 0.7.23
Modern Web Framework & Web App Hosting Service.
Loading...
Searching...
No Matches
native_runtime_reports.c
Go to the documentation of this file.
1/**
2 * @file
3 * @brief Serializes runtime-level and app-level build and status metadata into JSON report payloads.
4 */
5
7
9#include "native_support.h"
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14
15char *build_native_runtime_build_report_json(const char *target_id, const char *conf_path, const char *apps_dir, size_t app_count) {
16 char *target_id_json = json_string_dup_native(target_id != NULL ? target_id : "");
17 char *conf_path_json = json_string_dup_native(conf_path != NULL ? conf_path : "");
18 char *apps_dir_json = json_string_dup_native(apps_dir != NULL ? apps_dir : "");
19 char *report_json = NULL;
20 if (target_id_json == NULL || conf_path_json == NULL || apps_dir_json == NULL) {
21 free(target_id_json);
22 free(conf_path_json);
23 free(apps_dir_json);
24 return NULL;
25 }
26 report_json = (char *)malloc(strlen(target_id_json) + strlen(conf_path_json) + strlen(apps_dir_json) + 256);
27 if (report_json != NULL) {
28 snprintf(report_json, strlen(target_id_json) + strlen(conf_path_json) + strlen(apps_dir_json) + 256,
29 "{\"report_id\":\"pharos-runtime-build-v1\",\"target_kind\":\"runtime\",\"target_id\":%s,\"status\":\"built\",\"app_count\":%zu,\"config_path\":%s,\"apps_dir\":%s}\n",
30 target_id_json, app_count, conf_path_json, apps_dir_json);
31 }
32 free(target_id_json);
33 free(conf_path_json);
34 free(apps_dir_json);
35 return report_json;
36}
37
38char *build_native_app_runtime_report_json(const char *report_id, const char *verb, const char *host_target, const char *host_mode, const char *artifact_dir, const AppRuntime *app) {
39 char *report_id_json;
40 char *verb_json;
41 char *host_target_json;
42 char *host_mode_json;
43 char *artifact_dir_json;
44 char *app_id_json;
45 char *app_root_json;
46 char *build_dir_json;
47 char *state_path_json;
48 char *base_path_json;
49 char *log_path_json;
50 char *log_format_json;
51 char *error_report_path_json;
52 char *debug_json;
53 char *host_profile_json;
54 char *compiled_entrypoint_json;
55 char *runtime_conf_path_json;
56 char *report_json;
57 if (app == NULL) {
58 return NULL;
59 }
60 report_id_json = json_string_dup_native(report_id != NULL ? report_id : "pharos-local-app-runtime-v1");
61 verb_json = json_string_dup_native(verb != NULL ? verb : "status");
62 host_target_json = json_string_dup_native(host_target != NULL ? host_target : "native-host");
63 host_mode_json = json_string_dup_native(host_mode != NULL ? host_mode : "native-host-target");
64 artifact_dir_json = json_string_dup_native(artifact_dir != NULL ? artifact_dir : "");
65 app_id_json = json_string_dup_native(app->app_id != NULL ? app->app_id : "");
66 app_root_json = json_string_dup_native(app->app_root != NULL ? app->app_root : "");
67 build_dir_json = json_string_dup_native(app->build_dir != NULL ? app->build_dir : "");
68 state_path_json = json_string_dup_native(app->state_path != NULL ? app->state_path : "");
69 base_path_json = json_string_dup_native(app->base_path != NULL ? app->base_path : "");
70 log_path_json = json_string_dup_native(app->log_path != NULL ? app->log_path : "");
71 log_format_json = json_string_dup_native(app->log_format != NULL ? app->log_format : "service");
72 error_report_path_json = json_string_dup_native(app->error_report_path != NULL ? app->error_report_path : "");
73 debug_json = json_string_dup_native(app->debug != NULL ? app->debug : "false");
74 host_profile_json = json_string_dup_native(app->host_profile != NULL ? app->host_profile : "dynamic-app");
75 compiled_entrypoint_json = json_string_dup_native(app->compiled_entrypoint != NULL ? app->compiled_entrypoint : "");
76 runtime_conf_path_json = json_string_dup_native(app->runtime_conf_path != NULL ? app->runtime_conf_path : "");
77 report_json = NULL;
78 if (report_id_json == NULL || verb_json == NULL || host_target_json == NULL || host_mode_json == NULL || artifact_dir_json == NULL || app_id_json == NULL || app_root_json == NULL || build_dir_json == NULL || state_path_json == NULL || base_path_json == NULL || log_path_json == NULL || log_format_json == NULL || error_report_path_json == NULL || debug_json == NULL || host_profile_json == NULL || compiled_entrypoint_json == NULL || runtime_conf_path_json == NULL) {
79 free(report_id_json);
80 free(verb_json);
81 free(host_target_json);
82 free(host_mode_json);
83 free(artifact_dir_json);
84 free(app_id_json);
85 free(app_root_json);
86 free(build_dir_json);
87 free(state_path_json);
88 free(base_path_json);
89 free(log_path_json);
90 free(log_format_json);
91 free(error_report_path_json);
92 free(debug_json);
93 free(host_profile_json);
94 free(compiled_entrypoint_json);
95 free(runtime_conf_path_json);
96 return NULL;
97 }
98 report_json = (char *)malloc(strlen(report_id_json) + strlen(verb_json) + strlen(host_target_json) + strlen(host_mode_json) + strlen(artifact_dir_json) + strlen(app_id_json) + strlen(app_root_json) + strlen(build_dir_json) + strlen(state_path_json) + strlen(base_path_json) + strlen(log_path_json) + strlen(log_format_json) + strlen(error_report_path_json) + strlen(debug_json) + strlen(host_profile_json) + strlen(compiled_entrypoint_json) + strlen(runtime_conf_path_json) + 768);
99 if (report_json != NULL) {
100 snprintf(report_json, strlen(report_id_json) + strlen(verb_json) + strlen(host_target_json) + strlen(host_mode_json) + strlen(artifact_dir_json) + strlen(app_id_json) + strlen(app_root_json) + strlen(build_dir_json) + strlen(state_path_json) + strlen(base_path_json) + strlen(log_path_json) + strlen(log_format_json) + strlen(error_report_path_json) + strlen(debug_json) + strlen(host_profile_json) + strlen(compiled_entrypoint_json) + strlen(runtime_conf_path_json) + 768,
101 "{\"report_id\":%s,\"verb\":%s,\"target_kind\":\"app\",\"target_id\":%s,\"status\":\"ok\",\"artifact_dir\":%s,\"runtime_launcher\":true,\"host_target\":%s,\"host_mode\":%s,\"generated_by\":\"pharos-native-runtime\",\"runtime_summary\":{\"app_id\":%s,\"mount_path\":%s,\"app_root\":%s,\"build_dir\":%s,\"state_path\":%s,\"log_path\":%s,\"log_format\":%s,\"error_report_path\":%s,\"debug\":%s,\"host_profile\":%s,\"compiled_entrypoint\":%s,\"runtime_conf_path\":%s}}\n",
102 report_id_json, verb_json, app_id_json, artifact_dir_json, host_target_json, host_mode_json, app_id_json, base_path_json, app_root_json, build_dir_json, state_path_json, log_path_json, log_format_json, error_report_path_json, debug_json, host_profile_json, compiled_entrypoint_json, runtime_conf_path_json);
103 }
104 free(report_id_json);
105 free(verb_json);
106 free(host_target_json);
107 free(host_mode_json);
108 free(artifact_dir_json);
109 free(app_id_json);
110 free(app_root_json);
111 free(build_dir_json);
112 free(state_path_json);
113 free(base_path_json);
114 free(log_path_json);
115 free(log_format_json);
116 free(error_report_path_json);
117 free(debug_json);
118 free(host_profile_json);
119 free(compiled_entrypoint_json);
120 free(runtime_conf_path_json);
121 return report_json;
122}
123
124char *build_native_runtime_status_report_json(const RuntimeStack *stack, const char *apps_dir) {
125 Buffer apps_json;
126 size_t index;
127 char *host_json;
128 char *port_json;
129 char *socket_json;
130 char *apps_dir_json;
131 char *report_json;
132 if (stack == NULL) {
133 return NULL;
134 }
135 buffer_init(&apps_json);
136 for (index = 0; index < stack->count; index++) {
137 const AppRuntime *app = &stack->apps[index];
138 char *app_id_json = json_string_dup_native(app->app_id != NULL ? app->app_id : "");
139 char *mount_json = json_string_dup_native(app->base_path != NULL ? app->base_path : "");
140 char *app_root_json = json_string_dup_native(app->app_root != NULL ? app->app_root : "");
141 char *build_dir_json = json_string_dup_native(app->build_dir != NULL ? app->build_dir : "");
142 char *state_path_json = json_string_dup_native(app->state_path != NULL ? app->state_path : "");
143 char *log_path_json = json_string_dup_native(app->log_path != NULL ? app->log_path : "");
144 char *log_format_json = json_string_dup_native(app->log_format != NULL ? app->log_format : "service");
145 char *error_report_path_json = json_string_dup_native(app->error_report_path != NULL ? app->error_report_path : "");
146 char *debug_json = json_string_dup_native(app->debug != NULL ? app->debug : "false");
147 char *host_profile_json = json_string_dup_native(app->host_profile != NULL ? app->host_profile : "dynamic-app");
148 char *compiled_entrypoint_json = json_string_dup_native(app->compiled_entrypoint != NULL ? app->compiled_entrypoint : "");
149 char row[8192];
150 if (app_id_json == NULL || mount_json == NULL || app_root_json == NULL || build_dir_json == NULL || state_path_json == NULL || log_path_json == NULL || log_format_json == NULL || error_report_path_json == NULL || debug_json == NULL || host_profile_json == NULL || compiled_entrypoint_json == NULL) {
151 free(app_id_json);
152 free(mount_json);
153 free(app_root_json);
154 free(build_dir_json);
155 free(state_path_json);
156 free(log_path_json);
157 free(log_format_json);
158 free(error_report_path_json);
159 free(debug_json);
160 free(host_profile_json);
161 free(compiled_entrypoint_json);
162 buffer_free(&apps_json);
163 return NULL;
164 }
165 snprintf(row, sizeof(row), "%s{\"app_id\":%s,\"mount_path\":%s,\"app_root\":%s,\"build_dir\":%s,\"state_path\":%s,\"log_path\":%s,\"log_format\":%s,\"error_report_path\":%s,\"debug\":%s,\"host_profile\":%s,\"compiled_entrypoint\":%s}",
166 index == 0 ? "" : ",", app_id_json, mount_json, app_root_json, build_dir_json, state_path_json, log_path_json, log_format_json, error_report_path_json, debug_json, host_profile_json, compiled_entrypoint_json);
167 if (buffer_append(&apps_json, row, strlen(row)) != 0) {
168 free(app_id_json);
169 free(mount_json);
170 free(app_root_json);
171 free(build_dir_json);
172 free(state_path_json);
173 free(log_path_json);
174 free(log_format_json);
175 free(error_report_path_json);
176 free(debug_json);
177 free(host_profile_json);
178 free(compiled_entrypoint_json);
179 buffer_free(&apps_json);
180 return NULL;
181 }
182 free(app_id_json);
183 free(mount_json);
184 free(app_root_json);
185 free(build_dir_json);
186 free(state_path_json);
187 free(log_path_json);
188 free(log_format_json);
189 free(error_report_path_json);
190 free(debug_json);
191 free(host_profile_json);
192 free(compiled_entrypoint_json);
193 }
194 host_json = json_string_dup_native(stack->host != NULL ? stack->host : "127.0.0.1");
195 port_json = json_string_dup_native(stack->port != NULL ? stack->port : "7323");
196 socket_json = json_string_dup_native(stack->socket_path != NULL ? stack->socket_path : "");
197 apps_dir_json = json_string_dup_native(apps_dir != NULL ? apps_dir : "");
198 if (host_json == NULL || port_json == NULL || socket_json == NULL || apps_dir_json == NULL) {
199 free(host_json);
200 free(port_json);
201 free(socket_json);
202 free(apps_dir_json);
203 buffer_free(&apps_json);
204 return NULL;
205 }
206 report_json = (char *)malloc(strlen(host_json) + strlen(port_json) + strlen(socket_json) + strlen(apps_dir_json) + apps_json.len + 512);
207 if (report_json != NULL) {
208 snprintf(report_json, strlen(host_json) + strlen(port_json) + strlen(socket_json) + strlen(apps_dir_json) + apps_json.len + 512,
209 "{\"report_id\":\"pharos-runtime-status-v1\",\"status\":\"ok\",\"host\":%s,\"port\":%s,\"socket_path\":%s,\"apps_dir\":%s,\"app_count\":%zu,\"apps\":[%s]}\n",
210 host_json, port_json, socket_json, apps_dir_json, stack->count, apps_json.data != NULL ? apps_json.data : "");
211 }
212 free(host_json);
213 free(port_json);
214 free(socket_json);
215 free(apps_dir_json);
216 buffer_free(&apps_json);
217 return report_json;
218}
char * json_string_dup_native(const char *value)
Duplicates a string while escaping it for JSON string output.
Declares helpers that build native artifact and report payload strings.
char * build_native_app_runtime_report_json(const char *report_id, const char *verb, const char *host_target, const char *host_mode, const char *artifact_dir, const AppRuntime *app)
Builds a JSON runtime report for one app artifact and host surface.
char * build_native_runtime_status_report_json(const RuntimeStack *stack, const char *apps_dir)
Builds a JSON status report for a loaded runtime stack.
char * build_native_runtime_build_report_json(const char *target_id, const char *conf_path, const char *apps_dir, size_t app_count)
Builds a JSON report describing a native runtime build result.
Declares builders for native runtime report payloads.
void buffer_init(Buffer *buffer)
Initializes a growable buffer to an empty state.
void buffer_free(Buffer *buffer)
Releases memory owned by a growable buffer.
int buffer_append(Buffer *buffer, const void *data, size_t len)
Appends bytes to a growable buffer and maintains a trailing NUL byte.
Declares shared low-level support helpers used across the Pharos native runtime.
Loaded runtime metadata for one Pharos app artifact.
Growable byte buffer used by parsing and string assembly helpers.
size_t len
char * data
In-memory collection of loaded app runtimes served by one listener.
AppRuntime apps[MAX_APPS]